方便的pivot
查詢出所要資料 select get_date as gd,id as cg, SUM(number) as sdn from table group by get_date,id 想將id變為欄 select gd,ISNULL([NRDA1],0) as NRDA1 , ISNULL([NRDA6],0) as NRDA6 , ISNULL([NRDS3],0) as NRDS3 from ( select get_date as gd,id as cg, SUM(number) as sdn from table group by get_date,id ) as gtable pivot ( sum(sdn) for cg in ( NRDA1 , NRDA6 , NRDS3 ) ) as ptable http://technet.microsoft.com/zh-tw/library/ms177410(v=sql.105).aspx