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

  • SEARCH
  • Home
  • 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 6751925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:59:05+00:00 2026-05-26T12:59:05+00:00

I have a set of queries that outputs a pivot table. Is it possible

  • 0

I have a set of queries that outputs a pivot table. Is it possible to obtain row and/or column subtotals for a pivot table ?

My table I am selecting from looks like this

    Site     FormID   Present
    Site 1   Form A      Yes
    Site 1   Form B      Yes
    Site 1   Form D      Yes

etc…

My pivot table query is this

   SELECT *
   FROM (SELECT Site, COUNT(FormID) AS NumberOfForms,FormID
         FROM @CRFCount WHERE Present='Yes'
         GROUP BY Site, FormID) d
   PIVOT
   (SUM(NumberOfForms)
   FOR [Site] IN ([Site 1], [Site 2], [Site 3])
   )  AS p;

But I really want it to result in this (which of course it does not total for me)

    FormID  Site 1  Site 2  Site 3  Total
    Form A      8      8      15    31
    Form B     14      4    NULL    18
    Form C     14   NULL    NULL    14
    Form D     15      3      16    34
    Form E     12      4    NULL    16
    Form F     14      5       5    24
    Form G     14      8       6    28
    Form H     22     10      15    47
    Form I     15     10      16    41
    Form J     15      5      16    36
    Total     143     57      89   289

Thanks for your assistance !

-Don

  • 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-26T12:59:06+00:00Added an answer on May 26, 2026 at 12:59 pm
    ;WITH C as
    (
      SELECT FormID,
             [Site 1],
             [Site 2],
             [Site 3],
             (SELECT SUM(S)
              FROM (VALUES([Site 1]),
                          ([Site 2]),
                          ([Site 3])) AS T(S)) as Total
       FROM (SELECT Site, COUNT(FormID) AS NumberOfForms,FormID
             FROM @CRFCount WHERE Present='Yes'
             GROUP BY Site, FormID) d
       PIVOT
       (SUM(NumberOfForms)
       FOR [Site] IN ([Site 1], [Site 2], [Site 3])
       )  AS p
    )
    SELECT *
    FROM
      (
        SELECT FormID,
               [Site 1],
               [Site 2],
               [Site 3],
               Total
        FROM C
        UNION ALL
        SELECT 'Total',
               SUM([Site 1]),
               SUM([Site 2]),
               SUM([Site 3]),
               SUM(Total)
        FROM C
      ) AS T
    ORDER BY CASE WHEN FormID = 'Total' THEN 1 END
    

    Note: If you are using SQL Server 2005 you need to change this:

     (SELECT SUM(S)
      FROM (VALUES([Site 1]),
                  ([Site 2]),
                  ([Site 3])) AS T(S)) as Total
    

    to

     (SELECT SUM(S)
      FROM (SELECT [Site 1] UNION ALL
            SELECT [Site 2] UNION ALL
            SELECT [Site 3]) AS T(S)) as Total
    

    Try on SE Data

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

Sidebar

Related Questions

I have a set of sql - queries: List<String> queries = ... queries[0] =
I have set the eclipse java formatter to wrap lines that exceed 120 characters
I have set up an app that is registered for remote notifications. - (void)application:(UIApplication*)application
I have as a result from a dynamic query a table with only one
I have a query that will return results from 2 tables into 1 using
I have a single table that contains two types of items under the same
I have a set of data that contains full UNC paths to directories. I
I am trying to make Doctrine generate SQL from a set of models that
I have set a background on the data-role=page element like so <div data-role=page style=background:
I have set up Eclipse Indigo to do line debugging with CFEclipse over port

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.