ordering union results with CTEs - postgresql

How can I use ORDER BY when I have couple of CTEs followed by UNION of SELECTs. My query is like this:
WITH
cte1 AS (SELECT * FROM table1),
cte2 AS (SELECT * FROM table2),
cte3 AS (SELECT * FROM table3)
SELECT cte1.column1,cte1.column2 FROM cte1
UNION
SELECT cte2.column1,cte2.column2 FROM cte2
UNION
SELECT cte3.column1,cte3.column2 FROM cte3
and I need to order the results by column1 which in all CTEs is an integer number.

Just add an order by:
WITH cte1 AS (SELECT * FROM table1),
cte2 AS (SELECT * FROM table2),
cte3 AS (SELECT * FROM table3)
SELECT cte1.column1,cte1.column2 FROM cte1
UNION
SELECT cte2.column1,cte2.column2 FROM cte2
UNION
SELECT cte3.column1,cte3.column2 FROM cte3
order by column1 --<< here
An order by on a union always orders the complete union, not just the last select.
Btw: you might also want to read up on the difference between union and union all. If you know you don't have duplicates between the individual select (or don't care), union all will be faster.

Related

postgresql How to share cte among different tables in plain sql?

Say select id from some_expensive_query is the cte I want to share. Currently I write two sql in a transaction:
with t as (select id from some_expensive_query) select * from t1 join t on t.id =t1.id;
with t as (select id from some_expensive_query) select * from t2 join t on t.id =t2.id;
As you can see, the cte is executed twice but I want something like:
t = select id from some_expensive_query;
select * from t1 join t on t.id =t1.id;
select * from t2 join t on t.id =t2.id;
for portability, I don't want to use pgsql or functions, anyway to solve this?
Why don't you use union all ?
with t as (select id from some_expensive_query)
select * from t1 join t on t.id =t1.id
union all
select * from t2 join t on t.id =t2.id;

SQLWorkbenchJ and Redshift execute this query forever

I am trying to create a temporary table (using a CTE) to contain a list of all possible locales. I am executing this query in SQLWorkbenchJ build 125 on MacOS JVM 1.8
This query doesn't query any table in the database. If you copy paste this query it will execute forever. it will never print any results.
It has something to do with the size of the query. If I remove locales and only have upto the line 'en-GB' it works fine. but as you add more locales to the query, suddenly SQLWorkbenchJ will go blank and never return any results.
with locales(locale) as (
select 'af-ZA'
union
select 'am-ET'
union
select 'ar-AE'
union
select 'ar-BH'
union
select 'ar-DZ'
union
select 'ar-EG'
union
select 'ar-IQ'
union
select 'ar-JO'
union
select 'ar-KW'
union
select 'ar-LB'
union
select 'ar-LY'
union
select 'ar-MA'
union
select 'arn-CL'
union
select 'ar-OM'
union
select 'ar-QA'
union
select 'ar-SA'
union
select 'ar-SY'
union
select 'ar-TN'
union
select 'ar-YE'
union
select 'as-IN'
union
select 'az-Cyrl-AZ'
union
select 'az-Latn-AZ'
union
select 'ba-RU'
union
select 'be-BY'
union
select 'bg-BG'
union
select 'bn-BD'
union
select 'bn-IN'
union
select 'bo-CN'
union
select 'br-FR'
union
select 'bs-Cyrl-BA'
union
select 'bs-Latn-BA'
union
select 'ca-ES'
union
select 'co-FR'
union
select 'cs-CZ'
union
select 'cy-GB'
union
select 'da-DK'
union
select 'de-AT'
union
select 'de-CH'
union
select 'de-DE'
union
select 'de-LI'
union
select 'de-LU'
union
select 'dsb-DE'
union
select 'dv-MV'
union
select 'el-GR'
union
select 'en-029'
union
select 'en-AU'
union
select 'en-BZ'
union
select 'en-CA'
union
select 'en-GB'
union
select 'en-IE'
union
select 'en-IN'
union
select 'en-JM'
union
select 'en-MY'
union
select 'en-NZ'
union
select 'en-PH'
union
select 'en-SG'
union
select 'en-TT'
union
select 'en-US'
union
select 'en-ZA'
union
select 'en-ZW'
union
select 'es-AR'
union
select 'es-BO'
union
select 'es-CL'
union
select 'es-CO'
union
select 'es-CR'
union
select 'es-DO'
union
select 'es-EC'
union
select 'es-ES'
union
select 'es-GT'
union
select 'es-HN'
union
select 'es-MX'
union
select 'es-NI'
union
select 'es-PA'
union
select 'es-PE'
union
select 'es-PR'
union
select 'es-PY'
union
select 'es-SV'
union
select 'es-US'
union
select 'es-UY'
union
select 'es-VE'
union
select 'et-EE'
union
select 'eu-ES'
union
select 'fa-IR'
union
select 'fi-FI'
union
select 'fil-PH'
union
select 'fo-FO'
union
select 'fr-BE'
union
select 'fr-CA'
union
select 'fr-CH'
union
select 'fr-FR'
union
select 'fr-LU'
union
select 'fr-MC'
union
select 'fy-NL'
union
select 'ga-IE'
union
select 'gd-GB'
union
select 'gl-ES'
union
select 'gsw-FR'
union
select 'gu-IN'
union
select 'ha-Latn-NG'
union
select 'he-IL'
union
select 'hi-IN'
union
select 'hr-BA'
union
select 'hr-HR'
union
select 'hsb-DE'
union
select 'hu-HU'
union
select 'hy-AM'
union
select 'id-ID'
union
select 'ig-NG'
union
select 'ii-CN'
union
select 'is-IS'
union
select 'it-CH'
union
select 'it-IT'
union
select 'iu-Cans-CA'
union
select 'iu-Latn-CA'
union
select 'ja-JP'
union
select 'ka-GE'
union
select 'kk-KZ'
union
select 'kl-GL'
union
select 'km-KH'
union
select 'kn-IN'
)
select locale from locales limit 100;

Join 2 Alias Table from Union Select Table with same numbers of row

I hava 2 table from alias table result of select and union with have same number of row, how or can i make table 2 in right side of table 1? There are dont have same record
Thank you
First query:
SELECT * FROM (
SELECT COUNT(*) “DATA 220” FROM istros_sls_store.sales_store_220)—CEK
UNION ALL
SELECT COUNT(*) FROM istros_sls_item_sales_item_220)—CEK
UNION ALL
SELECT COUNT(*) FROM istros_sls_scat_sales_small_cat_220—CEK
UNION ALL
SELECT COUNT(*) FROM istros_inventory_hstr.inventory_hstr_dtl_220)—CEK
UNION ALL
SELECT COUNT(*) FROM istros_sos.stock_out_supplier_220—CEK
) a
Output from first query:
DATA 220
41
236633
11509
187174
1132
Second query:
SELECT * FROM (
SELECT COUNT(*) “DATA 226” FROM istros_sls_store.sales_store_226—CEK
UNION ALL
SELECT COUNT(*) FROM istros_sls_item_sales_item_226—CEK
UNION ALL
SELECT COUNT(*) FROM istros_sls_scat_sales_small_cat_226—CEK
UNION ALL
SELECT COUNT(*) FROM istros_inventory_hstr.inventory_hstr_dtl_226—CEK
UNION ALL
SELECT COUNT(*) FROM istros_sos.stock_out_supplier_226—CEK
) b
Output from second query:
DATA 226
41
243053
11437
193549
960
The desired output combines these two columns:
DATA 220 | DATA 226
41 | 41
236633 | 243053
11509 | 11437
187174 | 193549
1132 | 960
You may try simple selecting both counts in the union query, so that each one appears as a separate column in the output. Note that below I introduce a computed column, pos, which keeps track of which count queries should appear first in the result set.
SELECT "DATA 220", "DATA 226"
FROM
(
SELECT
1 AS pos,
(SELECT COUNT(*) FROM istros_sls_store.sales_store_220) AS “DATA 220”,
(SELECT COUNT(*) FROM istros_sls_store.sales_store_226) AS “DATA 226”
UNION ALL
SELECT
2,
(SELECT COUNT(*) FROM istros_sls_item_sales_item_220),
(SELECT COUNT(*) FROM istros_sls_item_sales_item_226)
UNION ALL
SELECT
3,
(SELECT COUNT(*) FROM istros_sls_scat_sales_small_cat_220),
(SELECT COUNT(*) FROM istros_sls_scat_sales_small_cat_226)
UNION ALL
SELECT
4,
(SELECT COUNT(*) FROM istros_inventory_hstr.inventory_hstr_dtl_220),
(SELECT COUNT(*) FROM istros_inventory_hstr.inventory_hstr_dtl_226)
UNION ALL
SELECT
5,
(SELECT COUNT(*) FROM istros_sos.stock_out_supplier_220),
(SELECT COUNT(*) FROM istros_sos.stock_out_supplier_226)
) t
ORDER BY
pos;

what's the outcome of select *, count(*) from aTable

Wonder what would happen if i select both * and an aggregate together.
Will it be just one row or multiple rows?
example:
select *, count(*) from employee

Is it possible to reference the same CTE in more than one unrelated query?

I have this CTE1 that I'd like to reference it in 2 or more unrelated queries
WITH CTE1
AS
(
SELECT col1, col2, col3
FROM Table1
WHERE condition
)
SELECT * FROM CTE1;
SELECT * FROM CTE1 c // This is not working -- Invalid object name 'CTE1'.
JOIN TABLE2 t
ON c.colx = t.xolx;
Is there a way to accomplish this?
Thanks for helping