Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:10:38+00:00 2026-05-27T05:10:38+00:00

for examaple month1 month2 month3 total district1 5 2 9 16 district2 1 0

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:10:39+00:00Added an answer on May 27, 2026 at 5:10 am

    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):

    Name        Month    Value
    ----------  -------  -----
    District 1  Month 1     10
    District 1  Month 2      5
    District 1  Month 3      6
    District 2  Month 1      1
    District 2  Month 2      2
    District 2  Month 3      3
    District 3  Month 1      8
    District 3  Month 2      6
    District 3  Month 3     11
    

    If you can do that, then your PIVOT query should look something like this:

    DECLARE @myTable AS TABLE([Name] VARCHAR(20), [Month] VARCHAR(20), [Value] INT)
    INSERT INTO @myTable VALUES ('District 1', 'Month 1', 10)
    INSERT INTO @myTable VALUES ('District 1', 'Month 2', 5)
    INSERT INTO @myTable VALUES ('District 1', 'Month 3', 6)
    INSERT INTO @myTable VALUES ('District 2', 'Month 1', 1)
    INSERT INTO @myTable VALUES ('District 2', 'Month 2', 2)
    INSERT INTO @myTable VALUES ('District 2', 'Month 3', 3)
    INSERT INTO @myTable VALUES ('District 3', 'Month 1', 8)
    INSERT INTO @myTable VALUES ('District 3', 'Month 2', 6)
    INSERT INTO @myTable VALUES ('District 3', 'Month 3', 11)
    
    SELECT [Name], [Month 1], [Month 2], [Month 3], [NameTotalValue] AS [Total]
    FROM
    (
          SELECT [Name], [Month], [Value], 
                 SUM([Value]) OVER (PARTITION BY [Name]) as [NameTotalValue] 
          FROM @myTable
        UNION 
          SELECT 'Total', [Month], SUM([Value]), (SELECT SUM([Value]) FROM @myTable)
          FROM @myTable
          GROUP BY [Month]
    ) t
    PIVOT
    (
        SUM([Value]) FOR [Month] IN ([Month 1], [Month 2], [Month 3]) 
    ) AS pvt
    ORDER BY pvt.[Name]
    

    In this example, I used the SUM([Value]) OVER PARTITION to 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:

    Name        Month 1 Month 2 Month 3 Total
    ----------- ------- ------- ------- -----
    District 1       10       5       6    21
    District 2        1       2       3     6
    District 3        8       6      11    25
    Total            19      13      20    52
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Total newbie to R here (so far very pleased!). I have a large array(vector?)
I am creating small program what shows us total sell sum by months via
I'm tracking articles views by Total, Today, Yesterday, Week, Month and Year, and I
I want to generate a report to accumulate the total stock quantity that has
I'm using rails to try and calculate the total of a specific column in
See below image. In the % column I need total % for each of
here's the scenario. I am generating a report of all the members who have
the output i would like to display was accumulator of total EMPL_NUM. For example,
I'm trying to complete a Project that will show total annual sales from an
Not using operators makes my code obscure. (aNumber / aNother) * count is better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.