Here's one way:declare @table table (Cust# int, DeliverToState char(2), LoadUnloadState char(2))insert @tableselect 101, 'MI', 'OH' union allselect 101, 'MI', 'MI' union allselect 102, 'IN', 'MI'select p.cust# ,[mi] as Qty2MI ,[OH] as Qty2OH ,[IN] as Qty2INfrom ( select up.cust#, up.val as st from @table unpivot (val for cols in (DeliverToState, LoadUnloadState)) up ) ppivot (count(st) for st in ([MI],[OH],[IN])) pOUTPUT:cust# Qty2MI Qty2OH Qty2IN----------- ----------- ----------- -----------101 3 1 0102 1 0 1
Be One with the OptimizerTG