Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id E56A8B5DC5A for ; Sat, 17 Sep 2011 10:32:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 29129-05 for ; Sat, 17 Sep 2011 13:32:28 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-iy0-f174.google.com (mail-iy0-f174.google.com [209.85.210.174]) by mail.postgresql.org (Postfix) with ESMTP id 6B04CB5DC5B for ; Sat, 17 Sep 2011 10:32:28 -0300 (ADT) Received: by iaqq3 with SMTP id q3so3278234iaq.19 for ; Sat, 17 Sep 2011 06:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=ZdPQ7ftwN2CCZ5+jCo/hxlO7fHeKYhNWyWeFrfqCZCE=; b=cCjnP0Isr1ajDHfdmJwLPgj/UvJVzVupDXuA6i3fsoM3EKq1J8TMytScGjPU1cZafl R9MAGvOoadmtTocav/JEDrCiXZS8DSs+C4/2P+1sJgJNLhLMd1VKv4+4pSAF1kB53btD gYiE8IkWWb6AVJVEqDHAWueS7vivXZEDXY/1o= MIME-Version: 1.0 Received: by 10.231.45.73 with SMTP id d9mr808028ibf.64.1316266348075; Sat, 17 Sep 2011 06:32:28 -0700 (PDT) Received: by 10.231.39.75 with HTTP; Sat, 17 Sep 2011 06:32:28 -0700 (PDT) Date: Sat, 17 Sep 2011 14:32:28 +0100 Message-ID: Subject: Sorting of data from two tables From: "R. Smith" To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.889 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201109/39 X-Sequence-Number: 35839 Greetings folks, This is a follow up to my initial message some time ago. Now I have got all the details together. I have two tables. Table A - Which contains one row per entry Table B - Which contains multiple rows per entry Table B relates to Table A by a field say called ID. Table A in this case containers all the order header information while Table B contains the order lines. What I want to do is do a query joining table A with B and sorting firstly on a field in Table A then on several fields in Table B. The issue is in doing the table A sort is simple enough but as soon as you do as sort on table B fields all the data gets mixed up. So while below gives me the required data is does not work on the sorting front.. SELECT a.gdn_gdn, a.gdn_custref, a.gdn_date, a.gdn_address_name, a.gdn_method, b.gdn_stockref, b.gdn_row, b.gdn_bay, b.gdn_shelf FROM a LEFT JOIN b ON a.gdn_gdn = b.gdn_gdn ORDER BY a.gdn_method,b.gdn_row, b.gdn_bay, b.gdn_shelf Subsequent tries at using union querys, etc all failed with the same problem when it comes to the sorting. What I want to do is to do a sort group the sorted data together and then sort each group again to keep it all in order. Simple solution would be to create a dynamic select using a crosstab function, pivot table or something like this. However after reading up on this it appears this is impossible as you cannot have unknowns in a SELECT query. I then read this post "Using cursors for generating cross tables" from "Pavel Stehule's blog" which would seem to get the data in a form I could use. However while I can get the examples to work trying to apply it to my needs fails. Presumably because my data is more complicated. So has anyone any ideas how to achieve this. It seems so simple from a human sorting perspective I cannot believe it can not be done by a database, but after playing around with it for a very long time I am just getting nowhere. Regards Richard