I have a table with following columns
toolid, Quantity, dtfrom, dtto, projectid etc.
We have master tables for tools and project. The tools have license for a particular period say dtfrom
is 1/1/2012 and dtto
is 30/3/2012.
I have to show monthly report for quantity of tools purchased in a particular month. The report will look like
sno toolname projectname quantity jan feb mar apr ......
Please help in writing SQL query for it.
in case of any performance issue you can try with 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
you have to do something like this:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.