0

次の列を持つテーブルがあります

toolid, Quantity, dtfrom, dtto, projectid etc.

ツールとプロジェクトのマスターテーブルがあります。dtfromツールには、 2012 年 1 月 1 日と2012 年 3 月 30 日の特定の期間のライセンスがdttoあります。

特定の月に購入したツールの数量の月次レポートを表示する必要があります。レポートは次のようになります

sno  toolname projectname quantity jan feb mar apr ......

そのためのSQLクエリを書くのを手伝ってください。

4

2 に答える 2

1

パフォーマンスの問題が発生した場合は、PIVOT で試すことができます

select * from (
select sum(quantity) quantity,left(DATENAME(MM, dtfrom),3) month,projectid from test
group by DATENAME(month, dtfrom),projectid
) as p
PIVOT(
  MAX(quantity) for month in ([Jan],[Feb],[Mar],[Apr],[May],[Jun],[Jul],[Aug],[Sep],[Oct],[Nov],[Dec])
)as pvt 

フィドル

于 2012-08-17T08:58:41.813 に答える
0

あなたはこのようなことをしなければなりません:

select 
  sno,
  toolname,
  projectname, 
  year(dtfrom) as year,
  jan=sum(case when month(dtfrom) =1 and month(dtto)=1 then quantity else 0 end),
  feb=sum(case when month(dtfrom) =2 and month(dtto)=2 then quantity else 0 end)
       .....
from <table>
group by toolname,
  projectname,
  dtfrom
于 2012-08-17T06:35:25.227 に答える