我有一组记录,一旦组合在一起,我想根据该组中记录的组合分配一个类别..
想知道解决这个问题的最佳方法..我能想到的就是我下面的内容,但认为可能有更聪明的方法来做到这一点(也许使用 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