SQLWorkbenchJ and Redshift execute this query forever - amazon-redshift

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;

Related

Use min function without grouping

How can I retrieve the min of a date without group by?
declare #table table
(
SaleDate date
)
insert into #table
select '7/8/2021' union
select '7/21/2021'
declare #dimdate table
(
fulldate date,
WeekNumberOfYear int
)
insert into #dimdate
select '7/4/2021', 28 union
select '7/5/2021', 28 union
select '7/6/2021', 28 union
select '7/7/2021', 28 union
select '7/8/2021', 28 union
select '7/9/2021', 28 union
select '7/10/2021', 28 union
select '7/11/2021', 29 union
select '7/18/2021', 30 union
select '7/19/2021', 30 union
select '7/20/2021', 30 union
select '7/21/2021', 30 union
select '7/22/2021', 30 union
select '7/23/2021', 30 union
select '7/24/2021', 30
select datepart(week, saledate) 'wk',
min(fulldate) as 'Beginning_Week'
from #table t inner join #dimdate d on
datepart(week, saledate) = WeekNumberOfYear
group by datepart(week, saledate), WeekNumberOfYear
How can I retrieve the same result as above without a group by?
Do you mean something like this?
select dt,
(select min(WeekNumberOfYear) from DimDate) as minWeekNumberOfYear
from #table
Just use a windowed aggregate:
SELECT dt,
MIN(dt) OVER () AS MinDt
FROM #table;
You can use MIN() window function if you partition by WeekNumberOfYear and use DISTINCT in the SELECT statement so that there are no duplicates:
SELECT DISTINCT
d.WeekNumberOfYear wk,
MIN(fulldate) OVER (PARTITION BY d.WeekNumberOfYear) Beginning_Week
FROM #table t INNER JOIN #dimdate d
ON DATEPART(week, t.saledate) = d.WeekNumberOfYear;
See the demo.

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;

How to create a table with dates in sequence between range in Hive?

I'm trying to Create a table with column date, And I want to insert date in sequence between Range.
Here's what I have tried:
SET StartDate = '2009-01-01';
SET EndDate = '2016-06-31';
CREATE TABLE DateRangeTable(mydate DATE, qty INT);
INSERT INTO DateRangeTable VALUES (select a.Date, 0
from (
select current_date - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) AS a where a.Date between '2019-01-01' and '2016-06-30');
This is the similar one:
select date_add(t.f1, t.start_r - pe.i) as date_range from (select '2022-01-01' as f1,datediff('2022-01-07','2022-01-01') as start_r,0 as end_r) t lateral view posexplode(split(space(start_r - end_r),' ')) pe as i,s;
You do not need VALUES keyword when using INSERT ... SELECT.
Working example:
set hivevar:start_date=2009-01-01;
set hivevar:end_date=2016-06-31;
CREATE TABLE DateRangeTable(mydate DATE, qty INT);
with date_range as
(--this query generates date range
select date_add ('${hivevar:start_date}',s.i) as dt
from ( select posexplode(split(space(datediff('${hivevar:end_date}','${hivevar:start_date}')),' ')) as (i,x) ) s
)
INSERT INTO TABLE DateRangeTable
select d.dt, 0 qty
from date_range d
where d.dt between '2019-01-01' and '2016-06-30');

Selecting SUM of COUNT statements on 2 different tables

I want to SUM the COUNT values from 2 COUNT statements on different tables.
I tried:
SELECT(
SELECT COUNT(*) FROM articlegroups
UNION
SELECT COUNT(*) FROM emails
)
as t
I tried:
SELECT(
SELECT COUNT(*) FROM articlegroups
+
SELECT COUNT(*) FROM emails
)
as t
I tried:
SELECT SUM(
SELECT COUNT(*) FROM articlegroups
+
SELECT COUNT(*) FROM emails
)
as t
I don't know what else to try...
You didn't try:
SELECT SUM(cnt) as cnt FROM
(
SELECT COUNT(*) as cnt FROM articlegroups
UNION ALL
SELECT COUNT(*) as cnt FROM emails
) t
Or:
SELECT (SELECT COUNT(*) FROM articlegroups) +
(SELECT COUNT(*) FROM emails) as cnt
You can use:
SELECT (SELECT COUNT(*) FROM articlegroups) +
(SELECT COUNT(*) FROM emails) AS cnt
If either articlegroups or emails can be empty, then you should also use COALESCE:
SELECT COALESCE((SELECT COUNT(*) FROM articlegroups),0) +
COALESCE((SELECT COUNT(*) FROM emails),0) AS cnt

ordering union results with CTEs

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.