开发者社区 问答 正文

如何在不同的列中选择第一,第二和第三值-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 我想编写一个查询来选择它:

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

问题来源于stack overflow

展开
收起
保持可爱mmm 2019-11-18 10:09:15 431 分享 版权
1 条回答
写回答
取消 提交回答
  • 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; 几乎所有其他数据库都支持窗口功能,这将使其效率更高。

    2019-11-18 10:09:30
    赞同 展开评论
问答地址: