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 3670700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:23:50+00:00 2026-05-19T02:23:50+00:00

I am trying to build this type of query in SQLServer for my report

  • 0

I am trying to build this type of query in SQLServer for my report but for branches.

Branch_Master

Branch_ID
Branch_Name

CheckList_Master

CheckList_ID
CheckList_Name

CheckList_Detail

CheckList_ID
Branch_ID
Chk_Value
ResponseDate

Now I want to pass only the month and year and it should do something like the following. Note: I have taken this query from oracle forum for the requirement of MONTHS but i want BRANCHES. Also note for example if i have 12 branches in detail table but 28 branches in master still i need to show 28 branches in query and show 0 value if no data of relavent branch in detail table.

select
c_name,
sum(decode(mon, 'Jan', pono, 0)) jan,
sum(decode(mon, 'Feb', pono, 0)) feb,
.....
.....
sum(decode(mon, 'Nov', pono, 0)) nov,
sum(decode(mon, 'Dec', pono, 0)) dec,
sum(decode(mon, 'Mon_Total', pono, 0)) mtotal
from (
select decode(grouping(c_name), 1, 'Total', c_name) c_name,
decode(grouping(month), 0, month, 'Mon_Total') mon, sum(po_no) pono
from t
group by cube(c_name, month)
)
group by c_name

And the result may look like:

C_NAME BR1 BR2 BR3 BR4 BR5 BR6 BR7 BR8 BR9 BR10 BR11 BR12 TOTAL_OF_ROW
------------------------------ ---------- ---------- ---------- ----------    
----------    ---------- ---------- ---------- ---------- ---------- ---------- 
CHKLST1 1 2 3 4 5 6 7 8 9 10 11 12 78
CHKLST2 20 30 40 50 60 70 80 90 100 110 120 780
  • 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-19T02:23:51+00:00Added an answer on May 19, 2026 at 2:23 am

    Use PIVOT.

    SELECT *
    FROM
    (
        SELECT c.CheckList_Name,
            b.BranchName,
            COUNT(*) ItemCount
        FROM dbo.CheckList_Detail cd
        JOIN dbo.CheckList_Master c
            ON c.CheckList_ID = cd.CheckList_ID
        JOIN dbo.Branch_Master b
            ON b.Branch_ID = cd.Branch_ID
    ) a
    PIVOT
    (
        MIN(ItemCount) FOR BranchName IN
        (
            [BR1], [BR2], [BR3], [BR4], [BR5], [BR6],
            [BR7], [BR8], [BR9], [BR10], [BR11], [BR12]
        )
    ) b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to build on TeamCity, which uses Microsoft.Office.Interop dll. This will
Trying to build a somewhat complicated WordPress query. In one query, I'm trying to:
I'm trying to build a generic class to work with entities from EF. This
I am trying to use the Join(...) extension method to build a query based
Maybe this is a shot in the dark here but I'm trying to find
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
I trying to build and compile my xcodeproj in command line and it is
I'm trying to build a grammar with the following: NUMERIC: INTEGER | FLOAT |
I'm currently trying to build a personal website to create a presence on the
I am trying to build a function in C/C++ to sort an array and

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.