Can you create a cross-tab report in my SQL Server! How can I get the report about sale quality for each store and each quarter and the total sale quality for each quarter at year 1993? You can use the table sales and stores in datatabase pubs. Table Sales record all sale detail item for each store. Column store_id is the id of each store,ord_date is the order date of each sale item,and column qty is the sale qulity. Table stores record all store information. I want to get the result look like as below: Output:
stor_name?????????????????????????????????????????????? Total?????? Qtr1??????? Qtr2??????? Qtr3??????? Qtr4??????? ------------------------------------------------ ----------- ----------- ----------- ----------- ----------- Barnum's???????????????????????????????????????????????? 50????????? 0?????????? 50????????? 0?????????? 0 Bookbeat????????????????????????????????????????????????55????????? 25????????? 30????????? 0?????????? 0 Doc-U-Mat: Quality Laundry and Books?? 85????????? 0?????????? 85????????? 0?????????? 0 Fricative Bookshop??????????????????????????????????60????????? 35????????? 0?????????? 0?????????? 25 Total??????????????????????????????????????????????????????? 250????????60????????? 165????????0??????????25
答案:
select b.stor_name, total=sum(a.qty), Q1=sum(case when datepart(mm,ord_date)<4 then a.qty else 0 end), Q2=sum(case when datepart(mm,ord_date)>3 and datepart(mm,ord_date)<7 then a.qty else 0 end), Q3=sum(case when datepart(mm,ord_date)>6 and datepart(mm,ord_date)<10 then a.qty else 0 end), Q4=sum(case when datepart(mm,ord_date)>9 then a.qty else 0 end) from sales as a inner join stores as b on b.stor_id = a.stor_id where a.ord_date BETWEEN convert(datetime,'1992-12-31 23:59:59') and convert(datetime,'1994-1-1') group by b.stor_name union select 'total', sum(a.qty), sum(case when datepart(mm,'1994-1-1')
?
没有pubs数据库的可以到这里找到创建pubs数据库的sql脚本.
?

(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|