for examaple
month1 month2 month3 total
district1 5 2 9 16
district2 1 0 11 12
.
.
total 260 150 140 550
here final total is not much important. but at least i need to show count per district per month.
SELECT Districts_mst.district_name,COUNT(Payments.PaymentId)users ,DATEPART(M,payments.saveon)Month
FROM Payments
JOIN Subsciber ON Payments.SubId =Subsciber.SubId
JOIN districts_mst ON districts_mst.district_id = Subsciber.District
where lang_id=1
group by district_name, DATEPART(M,payments.saveon)
which give me list like…..
district_name users Month
dist0 1 1
dist1 1 11
dist2 3 11
dist3 1 11
dist4 3 11
dist5 1 12
dist6 1 12
In SQL Server 2008 you can handle this task pretty easily with a PIVOT query. The following example relies on getting your data into the following format (which it looks like you have done already):
If you can do that, then your PIVOT query should look something like this:
In this example, I used the
SUM([Value]) OVER PARTITIONto get the sums for each District, and then I did a UNION to add a totals row to the bottom. The results look like this:One thing you’ll notice about this approach is that you have to know the column names you want at the top of the table ahead of time. That’s easy to do if you’re setting up the report to run for a full year, but is trickier if the number of columns is going to change. If you’re going to allow the users to specify a custom date range (i.e., 07/2011-10/2011 or 06/2011-11/2011), then one way handle that requirement is to build the PIVOT query using dynamic SQL and then execute it with sp_executesql.