[英]How can I Select 1st, 2nd and 3rd values in different columns - Ms Access
我在表格中有這些數據:
ID ItemID ItemSupplier
1 1 E
2 2 E
3 2 B
4 3 B
5 4 B
6 4 E
7 4 C
8 5 'NULL'
9 6 C
我想這樣寫一個查詢 select :
ItemID Supplier1 Supplier2 Supplier3
1 E
2 E B
3 B
4 B E C
5
6 C
但我只能得到第一列:
SELECT ItemID, FIRST(ItemSupplier) AS Supplier1
FROM myTable GROUP BY ItemID
謝謝
MS Access 並不是最好的工具。
一種方法使用相關子查詢來枚舉值,然后進行條件聚合:
select itemid,
max(iif(seqnum = 1, itemsupplier, null)) as supplier_1,
max(iif(seqnum = 2, itemsupplier, null)) as supplier_2,
max(iif(seqnum = 3, itemsupplier, null)) as supplier_3
from (select t.*,
(select count(*)
from t as t2
where t2.itemid = t.itemid and t2.id <= t.id
) as seqnum
from t
) as t
group by itemid;
幾乎所有其他數據庫都支持 window 函數,這將提高效率。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.