0
我有一組記錄,曾經分組在一起,我想根據該組中的記錄組合分配一個類別..如何根據Transact-sql中的group-by子句中的項目設置類別?
想知道最好的方式去這個..所有我能想到的就是我下面有,但認爲有可能是一個更聰明的方式來做到這一點(使用UDF的可能?)
SELECT *
, case when productgroup in ('chihuaha','labrador') then 1 else 0 end AS Dog
, case when productgroup in ('siamese', 'tabby') then 1 else 0 end as Cat
INTO #tmp_items
FROM dbo.Items
SELECT docket
, sum(value)
, case when sum(Dog) > 0 and sum(Cat) > 0 then 'Dog and Cat'
when sum(Dog) > 0 and sum(Cat) = 0 then 'Dog Only'
when sum(Dog) = 0 and sum(Cat) > 0 then 'Cat Only'
end
as Purchase_Type
FROM #tmp_items
GROUP BY docket
這個答案幫助我寫一個聚合選擇與左外連接查找表,謝謝! – 2012-04-23 17:08:33