※ 引述《hhomerun (HOMERUN家族弟弟)》之銘言:
: 其實很簡單, 你把他想得太複雜了~
: SELECT [ID]
: ,min([TYPE]) [TYPE]
: ,SUM([VAL]) [VAL]
: FROM [test].[dbo].[table]
: GROUP BY [ID]
: 技巧在min那邊, 要顯示A 就用min, 要顯示B 就用max
: 希望魯蛇的答案對你有幫助QQ
謝謝幫忙!!
後來請同事幫忙 艸
SELECT [ID], 'A', sum([VAL])
from [TABLE]
where
[ID] IN (
select [ID]
from [TABLE]
where [DATE] = '2014/05/13'
group by [ID]
HAVING COUNT(*)>1
)
group by [ID]
UNION
SELECT [ID], risk_kind, sum([VAL])
from [TABLE]
where [ID] IN (
select [ID]
from [TABLE]
where [DATE] = '2014/05/13'
group by [ID]
HAVING COUNT(*)=1
)
group by [ID], risk_kind
只是目前要查的資料會讓ID重複的只有同ID同時有A與B兩筆資料的狀況
以外的剛好不會(希望未來也不要會…………會的話這樣的判斷就不對了)
所以這樣做就解決了T_T
總之謝謝大家!!