-4

列の最大出現回数を取得する際に問題があります

 matnum   uom  amount
-------------------------------------
1234    EA   10
1234    CS   10
1234    EA    20
1235    CS   10
1235    CS    20

私の出力選択は

 matnum   uom  SUM(amount)
-------------------------------------
1234   EA (because EA occured Twice)    40
1235   CS                               30

希望する結果を得るために、selectクエリを提供してください

4

3 に答える 3

1

ここであなたが望むものについていくつかのことを想定していますが、

SELECT A.matnum, B.uom, C.s AS [SUM(amount)]
FROM yourtable A
INNER JOIN (
    SELECT matnum, uom, 
        ROW_NUMBER() OVER (PARTITION BY matnum ORDER BY COUNT(*) DESC) AS rnm
    FROM yourtable
    GROUP BY matnum, uom
) B ON A.matnum = B.matnum AND B.rnm = 1
INNER JOIN (
    SELECT matnum, SUM(amount) AS s 
    FROM yourtable 
    GROUP BY matnum
) C ON A.matnum = C.matnum
GROUP BY A.matnum, B.uom, C.s

SQL フィドル

于 2012-09-24T13:07:07.183 に答える
0
select matnum, 
       uom, 
       SUM(amount) as sum_amount
from your_table
group by matnum, uom
于 2012-09-24T12:59:41.287 に答える
0
select max(matnum) as matnum,max(uom) as uom,SUM(amount)
from yourtable
group by matnum,uom
于 2012-09-24T12:59:45.707 に答える