0
A
回答
0
您通常會在某種報告工具中執行「羣組標題」。但是,如果您願意,它可以在純SQL中完成。
您在「標準JOIN」先從數據:
-- The basic query
SELECT
table_1.table_1_id, phone, name, some_data,
row_number() over(partition by table_1.table_1_id order by some_data)
FROM
table_1
JOIN table_2 ON table_2.table_1_id = table_1.table_1_id ;
這將如下表產生:
table_1_id | phone | name | some_data | row_number ---------: | :---- | :--- | :-------- | ---------: 1 | 502 | aa | a | 1 1 | 502 | aa | b | 2 1 | 502 | aa | j | 3 1 | 502 | aa | n | 4 2 | 268 | bb | a | 1 2 | 268 | bb | s | 2 2 | 268 | bb | y | 3 5 | 984 | ee | a | 1 5 | 984 | ee | n | 2 5 | 984 | ee | w | 3
如果你想添加一些標題行,也不顯示的值phone, name, we need to *add* these data to the query. This is done by making a second specific
SELECT for the headers, and
UNION ALL`與第一個。
也就是說,我們將使用:
-- This query produces the 'headers'
SELECT DISTINCT
table_1.table_1_id,
phone,
name,
'' AS some_data,
true as is_header -- this marks this row as a 'header'
FROM
table_1
JOIN table_2 ON table_2.table_1_id = table_1.table_1_id
UNION ALL
-- This query produces the actual data
SELECT
table_1.table_1_id,
phone,
name,
some_data,
false as is_header
FROM
table_1
JOIN table_2 ON table_2.table_1_id = table_1.table_1_id ;
現在,我們讓這個子查詢,並提出一些額外的邏輯來決定哪些數據需要被顯示,並需要隱藏其中一個(實際上,顯示爲 ''):
-- A few tricks to do the formatting
SELECT
-- Here we decide which information to show, which not
case when is_header then cast(table_1_id as text) else '' end AS table_1_id,
case when is_header then phone else '' end AS phone,
case when is_header then name else '' end as name,
case when is_header then '' else some_data end as some_data
FROM
(-- This query produces the 'headers'
SELECT DISTINCT
table_1.table_1_id,
phone,
name,
'' AS some_data,
true as is_header -- this marks this row as a 'header
FROM
table_1
JOIN table_2 ON table_2.table_1_id = table_1.table_1_id
UNION ALL
-- This query produces the actual data
SELECT
table_1.table_1_id,
phone,
name,
some_data,
false as is_header
FROM
table_1
JOIN table_2 ON table_2.table_1_id = table_1.table_1_id
) AS q
ORDER BY
q.table_1_id, is_header DESC /* Header goes first */, some_data ;
這產生:
table_1_id | phone | name | some_data :--------- | :---- | :--- | :-------- 1 | 502 | aa | | | | a | | | b | | | j | | | n 2 | 268 | bb | | | | a | | | s | | | y 5 | 984 | ee | | | | a | | | n | | | w
你可以看到整個安裝和模擬數據在dbfiddle here
請注意,您指定的順序是不保留。你應該有一些邏輯應該如何訂購。 SQL沒有「插入順序」或「自然順序」的概念;你總是必須選擇你想要的一個(否則,數據庫會選擇它最方便的一個它,並且可能從一個執行改變到下一個)。
0
有像grouping sets
這樣不錯的功能。
使用它你可以在單個查詢/結果集中得到幾組不同的分組條件。感謝例如數據joanolo
讓我們開始在最簡單的查詢:
SELECT
table_1.table_1_id, phone, name, some_data
FROM
table_1 JOIN table_2 ON table_2.table_1_id = table_1.table_1_id;
┌────────────┬───────┬──────┬───────────┐ │ table_1_id │ phone │ name │ some_data │ ╞════════════╪═══════╪══════╪═══════════╡ │ 1 │ 502 │ aa │ a │ │ 1 │ 502 │ aa │ b │ │ 1 │ 502 │ aa │ n │ │ 1 │ 502 │ aa │ j │ │ 5 │ 984 │ ee │ w │ │ 5 │ 984 │ ee │ a │ │ 5 │ 984 │ ee │ n │ │ 2 │ 268 │ bb │ s │ │ 2 │ 268 │ bb │ a │ │ 2 │ 268 │ bb │ y │ └────────────┴───────┴──────┴───────────┘
這裏凌晨必要通過前三列添加組:
SELECT
table_1.table_1_id, phone, name--, some_data
FROM
table_1 JOIN table_2 ON table_2.table_1_id = table_1.table_1_id
GROUP BY GROUPING SETS ((table_1.table_1_id, phone, name));
┌────────────┬───────┬──────┐ │ table_1_id │ phone │ name │ ╞════════════╪═══════╪══════╡ │ 2 │ 268 │ bb │ │ 5 │ 984 │ ee │ │ 1 │ 502 │ aa │ └────────────┴───────┴──────┘
注意雙括號。實際上它等於簡單的GROUP BY table_1.table_1_id, phone, name
(這是因爲我註釋了some_data
列,它不在組中)。但是,我們要添加到我們查詢更多資料:
SELECT
table_1.table_1_id, phone, name, some_data
FROM
table_1 JOIN table_2 ON table_2.table_1_id = table_1.table_1_id
GROUP By GROUPING SETS ((table_1.table_1_id, phone, name),(table_1.table_1_id, phone, name, some_data));
┌────────────┬───────┬──────┬───────────┐ │ table_1_id │ phone │ name │ some_data │ ╞════════════╪═══════╪══════╪═══════════╡ │ 1 │ 502 │ aa │ a │ │ 1 │ 502 │ aa │ b │ │ 1 │ 502 │ aa │ j │ │ 1 │ 502 │ aa │ n │ │ 1 │ 502 │ aa │ ░░░░ │ │ 2 │ 268 │ bb │ a │ │ 2 │ 268 │ bb │ s │ │ 2 │ 268 │ bb │ y │ │ 2 │ 268 │ bb │ ░░░░ │ │ 5 │ 984 │ ee │ a │ │ 5 │ 984 │ ee │ n │ │ 5 │ 984 │ ee │ w │ │ 5 │ 984 │ ee │ ░░░░ │ └────────────┴───────┴──────┴───────────┘
這是我們想要什麼差不多。我們只需要訂購在適當的方式將數據和清理一些列:
SELECT
case when some_data is null then table_1.table_1_id end as table_1_id,
case when some_data is null then phone end as phone,
case when some_data is null then name end as name,
some_data
FROM
table_1
JOIN table_2 ON table_2.table_1_id = table_1.table_1_id
group by grouping sets((table_1.table_1_id, phone, name), (table_1.table_1_id, phone, name, some_data))
order by table_1.table_1_id, some_data nulls first;
┌────────────┬───────┬──────┬───────────┐ │ table_1_id │ phone │ name │ some_data │ ╞════════════╪═══════╪══════╪═══════════╡ │ 1 │ 502 │ aa │ ░░░░ │ │ ░░░░ │ ░░░░ │ ░░░░ │ a │ │ ░░░░ │ ░░░░ │ ░░░░ │ b │ │ ░░░░ │ ░░░░ │ ░░░░ │ j │ │ ░░░░ │ ░░░░ │ ░░░░ │ n │ │ 2 │ 268 │ bb │ ░░░░ │ │ ░░░░ │ ░░░░ │ ░░░░ │ a │ │ ░░░░ │ ░░░░ │ ░░░░ │ s │ │ ░░░░ │ ░░░░ │ ░░░░ │ y │ │ 5 │ 984 │ ee │ ░░░░ │ │ ░░░░ │ ░░░░ │ ░░░░ │ a │ │ ░░░░ │ ░░░░ │ ░░░░ │ n │ │ ░░░░ │ ░░░░ │ ░░░░ │ w │ └────────────┴───────┴──────┴───────────┘
賓果!希望這是你要求的。
相關問題
- 1. 來自多個表的MySQL查詢
- 2. 查詢來自多個表的數據
- 3. 來自多個表的查詢
- 4. 來自多個表的SQL IN查詢
- 5. 來自多個表的mysqli查詢
- 6. 來自多個表的Oracle查詢
- 7. POSTGRESQL:來自6個表的1個查詢
- 8. PostgreSQL - 組合來自多個查詢的結果行
- 9. postgresql查詢多個相同的表
- 10. MYSQL - 來自多個查詢
- 11. 來自多個API的一個查詢
- 12. 查詢來自同一多對多表
- 13. postgresql多個子查詢
- 14. MySQL將來自多個表的查詢結果結合起來
- 15. 來自SQL查詢的多個結果
- 16. 來自查詢的多個數組
- 17. 來自多個數據庫的查詢
- 18. 來自同一個表的多個MySQL查詢
- 19. SQL查詢顯示來自同一個表的多個記錄?
- 20. 一個查詢連接來自一個表中的多行
- 21. 來自兩個表的分層查詢
- 22. 來自2個表的SQL查詢
- 23. 加入來自兩個表的查詢
- 24. 來自兩個表的Mysql查詢
- 25. 來自兩個表的sql查詢
- 26. 來自4個表格的sql查詢
- 27. 創建來自多個查詢視圖
- 28. 創建來自多個查詢
- 29. GROUP_CONCAT來自多個子查詢?
- 30. 自定義postgresql的查詢
你想要的結果是什麼? –
圖像上的第三個輸出 –
如果只是通過存儲過程或在任何編程代碼中,給出一個例子,我們可以幫助將它變成簡單的SELECT語句 - 儘管通常問題是從SELECT語句開始的另一個方向到應用程序語言代碼。 – manassehkatz