dbTalk Databases Forums  

ORDER BY with UNION

comp.databases.postgresql comp.databases.postgresql


Discuss ORDER BY with UNION in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
gargoyle60
 
Posts: n/a

Default ORDER BY with UNION - 07-22-2010 , 02:51 PM






Having trouble with the following union query...

SELECT
table_catalog AS "databaseName",
table_schema AS "schemaName",
table_name AS "tableName",
'' AS "primaryKeyName",
column_name AS "columnMappings"
FROM information_schema.columns
WHERE table_schema NOT IN ('information_schema','pg_catalog')
UNION ALL
SELECT
table_catalog AS "databaseName",
table_schema AS "schemaName",
table_name AS "tableName",
constraint_name AS "primaryKeyName",
column_name AS "columnMappings"
FROM information_schema.key_column_usage
WHERE constraint_name LIKE 'pk_%'
-- ORDER BY
-- table_catalog,
-- table_schema,
-- table_name,
-- constraint_name,
-- ordinal_position,
-- column_name
;

This works fine as above but as soon as I reintroduce the ORDER BY clause I get the syntax error...
ERROR: column "table_catalog" does not exist
LINE 19: table_catalog,
^
********** Error **********
ERROR: column "table_catalog" does not exist
SQL state: 42703
Character: 667

From the documentation I infer that ORDER BY should work with UNION, so where am I going wrong?
Any help please...

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: ORDER BY with UNION - 07-22-2010 , 11:11 PM






On 2010-07-22 21:51, gargoyle60 wrote:
[...]
Quote:
This works fine as above but as soon as I reintroduce the ORDER BY clause I get the syntax error...
ERROR: column "table_catalog" does not exist
LINE 19: table_catalog,
^
********** Error **********
ERROR: column "table_catalog" does not exist
SQL state: 42703
Character: 667

From the documentation I infer that ORDER BY should work with UNION, so where am I going wrong?
Any help please...
Try order by databaseName. Also, the order by clause applies to the
whole query (your indentation indicates otherwise)


/Lennart

Reply With Quote
  #3  
Old   
gargoyle60
 
Posts: n/a

Default Re: ORDER BY with UNION - 07-23-2010 , 02:41 AM



On Fri, 23 Jul 2010 06:11:33 +0200, Lennart Jonsson <erik.lennart.jonsson (AT) gmail (DOT) com> wrote:

Quote:
Try order by databaseName. Also, the order by clause applies to the
whole query (your indentation indicates otherwise)
Yes, that worked, thanks.
From my old Oracle days I don't recall ORDER BY working on aliases, unless my memory is blurred.

(BTW. the indentation was just for formatting to aid clarity in my question)

Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: ORDER BY with UNION - 07-23-2010 , 02:54 AM



On 2010-07-23 09:41, gargoyle60 wrote:
Quote:
On Fri, 23 Jul 2010 06:11:33 +0200, Lennart Jonsson <erik.lennart.jonsson (AT) gmail (DOT) com> wrote:

Try order by databaseName. Also, the order by clause applies to the
whole query (your indentation indicates otherwise)

Yes, that worked, thanks.
From my old Oracle days I don't recall ORDER BY working on aliases, unless my memory is blurred.
In this case:

SELECT
table_catalog AS "databaseName",
table_schema AS "schemaName",
table_name AS "tableName",
'' AS "primaryKeyName",
column_name AS "columnMappings"
FROM information_schema.columns
WHERE table_schema NOT IN ('information_schema','pg_catalog')
UNION ALL
SELECT
table_catalog AS "databaseName",
table_schema AS "schemaName",
table_name AS "tableName",
constraint_name AS "primaryKeyName",
column_name AS "columnMappings"
FROM information_schema.key_column_usage
WHERE constraint_name LIKE 'pk_%'

is a derived table so the only thing that's visible in the order by
clause is the renamed attributes

Quote:
(BTW. the indentation was just for formatting to aid clarity in my question)
I thought so, it was just to be sure. In recent versions of the standard
one is actually allowed to order a sub-select (whether we like it or not :-)

Reply With Quote
  #5  
Old   
gargoyle60
 
Posts: n/a

Default Re: ORDER BY with UNION - 07-23-2010 , 03:23 AM



On Fri, 23 Jul 2010 09:54:52 +0200, Lennart Jonsson <erik.lennart.jonsson (AT) gmail (DOT) com> wrote:

Quote:
is a derived table so the only thing that's visible in the order by
clause is the renamed attributes
Yes, that makes sense. Thanks

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.