gpt4 book ai didi

sql - db2 特定于不同行数的查询

转载 作者:行者123 更新时间:2023-12-02 01:02:23 25 4
gpt4 key购买 nike

我正在使用 DB2。我有这个查询:

SELECT DISTINCT 
ORDER_NUMBER,
trdm as TRADEMARK,
cmdnsm as CUSTOMER_NAME
FROM DB1.ORDFA
JOIN DB2.CCADF ON SFORD# = WDRSO
JOIN DB2.DADA06 ON WD3ADF = WD3FLA
JOIN DB1.CMP1 ON C1LK2 = SF2LK
WHERE YEAR = '14' AND PART = '5484'
ORDER BY CUSTOMER_NAME, TRADEMARK

我从上面的查询中得到了这个结果集

order_number    trademark      customer_name
2220824. AIR EPR PRODUCTS INC
2254614. AVALON EPR PRODUCTS INC
2214146. AVALON EPR PRODUCTS INC
2338002. AVALON EPR PRODUCTS INC
2102733. BOSS EPR PRODUCTS INC
2286148. CDC EPR PRODUCTS INC
2116148. CDC EPR PRODUCTS INC
2032105. HC SUMMLC
2014046. HC SUMMLC
2232312. SDEXO SUMMLC
2267369. SDEXO SUMMLC
2085280. SDEXO SUMMLC
2170433. SDEXO SUMMLC
2179283. SDEXO SUMMLC
2378745. SDEXO SUMMLC

我需要的是一个可以给我这个结果集的 DB2 查询:

 NUM_ORDERS      TRADEMARK      CUSTOMER_NAME
6 SDEXO SUMMLC
3 AVALON EPR PRODUCTS INC
2 CDC EPR PRODUCTS INC
2 HC SUMMLC
1 BOSS EPR PRODUCTS INC
1 AIR EPR PRODUCTS INC

最佳答案

如果 DB2 支持 ansi 标准 sql,我相信您只需要添加一个countgroup by:

SELECT 
COUNT(DISTINCT ORDER_NUMBER) AS NUM_ORDERS,
trdm as TRADEMARK,
cmdnsm as CUSTOMER_NAME
FROM DB1.ORDFA
JOIN DB2.CCADF ON SFORD# = WDRSO
JOIN DB2.DADA06 ON WD3ADF = WD3FLA
JOIN DB1.CMP1 ON C1LK2 = SF2LK
WHERE YEAR = '14' AND PART = '5484'
GROUP BY trdm, cmdnsm
ORDER BY NUM_ORDERS DESC, CUSTOMER_NAME, TRADEMARK

关于sql - db2 特定于不同行数的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com