我有一個表格,每個州有兩個郵政編碼之間有幾行排隊範圍。我希望能夠採取這兩個範圍,並顯示在他們各自的行上的兩個範圍之間的每個值。 EX下面。Dispaying兩個範圍之間的所有數據點
Zip Start Zip End State
00501 06390 NY
10001 10314 NY
10451 11003 NY
我希望數據顯示爲
00501 NY
00502 NY
00503 NY
00504 NY
任何幫助將不勝感激。
DROP TABLE IF EXISTS yourTable;
CREATE TABLE yourTable as
Select '00501' as zipstart,'06390' as zipend,'NY' as state union
select'10001','10314','NY'union
select'10451','11003','NY'
Distributed by (ZipStart,ZipEnd,State);
WITH cte AS (
SELECT *
FROM
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n)
)
, cteTally AS (
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as Num
FROM
cte c1
CROSS JOIN cte c2
CROSS JOIN cte c3
CROSS JOIN cte c4
CROSS JOIN cte c5
)
SELECT
SUBSTRING(ZipStart || CAST(tt.Num AS VARCHAR(100)),1,5) ZipSTart
,t.zipend,t.State
FROM
yourTable t
INNER JOIN cteTally tt
ON tt.Num <= CAST(t.ZipEnd AS INT)
AND tt.Num >= CAST(t.ZipStart AS INT)
你有什麼已經嘗試過,什麼問題(S)你的經驗嗎? – 3N1GM4
最初這是在使用硬編碼值和臨時表的SQL服務器中完成的。我們現在在PGAdmin中,應用程序不能再處理大量的值。 我希望能夠在子查詢中使用這個更大的報告。謝謝 – Garrett
好吧...這是使用SQL Server還是PostgresSql?選一個。 – SqlZim