2016-12-19 205 views
0
select SALESTABLE.CUSTACCOUNT, 
     SALESTABLE.salesname, 
     datename(month,SALESTABLE.SHIPPINGDATECONFIRMED)as Month, 
     sum(SALESLINE.LineAmount) SaleAmount 
from 
SALESTABLE 
inner join SALESLINE 
    on SALESTABLE.SALESID= SALESLINE.SALESID 

WHERE SUBSTRING(SALESTABLE.CUSTACCOUNT, 1,2)='O-' 
group by SALESTABLE.CUSTACCOUNT, 
     SALESTABLE.salesname, 
     datename(month,SALESTABLE.SHIPPINGDATECONFIRMED) 
order by SALESTABLE.salesname, 
     datename(month,SALESTABLE.SHIPPINGDATECONFIRMED) 

我可以轉換這個查詢爲支點,我想在像列標題中的表和總和LineAmount的頂部顯示的月份名稱婁這一個月列SQL數據透視表或交叉表?

+0

請告訴我們你是什麼版本的SQL使用和更新標籤。 –

+0

@Rana Ali,指定樣本數據和預期結果 – Mansoor

+0

請爲您正在使用的數據庫添加tog。 – Kacper

回答

0
select  * 

from  (select SALESTABLE.CUSTACCOUNT, 
        SALESTABLE.salesname, 
        datename(month,SALESTABLE.SHIPPINGDATECONFIRMED)as Month, 
        SALESLINE.LineAmount 
      from 
      SALESTABLE 
      inner join SALESLINE 
       on SALESTABLE.SALESID= SALESLINE.SALESID 

      WHERE SUBSTRING(SALESTABLE.CUSTACCOUNT, 1,2)='O-' 

      ) t pivot (sum (LineAmount) for Month in (January,February,March,April,May,June,July,August,September,October,November,December)) p