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
Use PIVOT.