SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Grand total help


Grand total help

Author
Message
djmason_2001
djmason_2001
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 11
Hey guys

This is my first post on this website, so please be gentle

i need some help producing a grand total


i have query 1 (which is below) which produces a grand total,
i then have query 2 ( which is below query1) which also produces a grand total on there.

i need the grand total from query 2 to be subtracted from the query1 grand total to produce a Final total

For eg layout
months sum of net sales sum of net transactions
August 100 300
July 200 200
june 300 100
(query 1)grandtotal 600 600
(query2)Grandtotal 100 200
Final total 500 400



query1

Declare @date varchar(10)
set @Date = (select dateadd(MM,-2,max(hst_date_processed))
from FDMS.dbo.Fact_Financial_History)

SELECT
coalesce (cast(DATENAME(MM,hst_date_processed) as varchar(20)), 'Grand Total') as 'Months',
SUM(Fact_Financial_History.Net_Sales) AS [Sum Of Net Sales],
SUM(Fact_Financial_History.Net_Trans) AS [Sum Of Net Transactions]
FROM Fact_Financial_History INNER JOIN Dim_Interchange_Tier_2 ON
Fact_Financial_History.hst_prod_code + '-' + Fact_Financial_History.hst_plan_code = Dim_Interchange_Tier_2.Plan_Code
WHERE (Fact_Financial_History.hst_date_processed >= @date)
--and Dim_Interchange_Tier_2.Qualification_2 in ('moto')
and (dbo.Fact_Financial_History.hst_prod_code='79'
Or dbo.Fact_Financial_History.hst_prod_code='81')
and (dbo.Fact_Financial_History.hst_plan_code= '002'
Or dbo.Fact_Financial_History.hst_plan_code='010'
Or dbo.Fact_Financial_History.hst_plan_code='011'
Or dbo.Fact_Financial_History.hst_plan_code='015'
Or dbo.Fact_Financial_History.hst_plan_code='037'
Or dbo.Fact_Financial_History.hst_plan_code='033'
Or dbo.Fact_Financial_History.hst_plan_code='021'
Or dbo.Fact_Financial_History.hst_plan_code='019'
Or dbo.Fact_Financial_History.hst_plan_code='020'
Or dbo.Fact_Financial_History.hst_plan_code='030'
Or dbo.Fact_Financial_History.hst_plan_code='029')
and Dim_Interchange_Tier_2.Plan_Code <> '79-021'
GROUP BY hst_date_processed with rollup
order by hst_date_processed desc


Query2


Declare @date varchar(10)
set @Date = (select dateadd(MM,-2,max(hst_date_processed))
from FDMS.dbo.Fact_Financial_History)

SELECT *
FROM ( SELECT
CASE
WHEN GROUPING([dbo].[Dim_Outlet].FDMSAccountNo_First9) = 1 THEN 'Grand Total'
ELSE CAST([dbo].[Dim_Outlet].FDMSAccountNo_First9 AS VARCHAR(20))
END AS FDMSAccountNo_First9,
dbo.Dim_Outlet.DBA_Name,
SUM(dbo.Fact_Financial_History.Net_Sales) AS TotalofNetAmount,
SUM(dbo.Fact_Financial_History.Net_Trans)AS TotalofNetTransactions

FROM
Fact_Financial_History
INNER JOIN
Dim_Outlet
ON Fact_Financial_History.hst_merchnum = Dim_Outlet.FDMSAccountNo_First9
INNER JOIN
dbo.Dim_MCC
ON dbo.Dim_Outlet.MCC_Code = dbo.Dim_MCC.MCC
WHERE
(dbo.Fact_Financial_History.hst_prod_code BETWEEN '79' AND '84')
and MCC_Code = '5968'
AND (dbo.Fact_Financial_History.hst_date_processed > @date)
group by
[dbo].[Dim_Outlet].FDMSAccountNo_First9,
[dbo].[Dim_Outlet].DBA_Name
with rollup
) as temp
WHERE temp.DBA_NAME IS NOT NULL OR temp.FDMSAccountNo_First9 = 'Grand Total'
djmason_2001
djmason_2001
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 11
Does anyone have any ideas?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)

Group: General Forum Members
Points: 101483 Visits: 18186
Is this a report? If so, a calculated field would be easier.

If not, there are a few ways to deal with this. They all involve the basic premise of:


select (select sum(col) as Total from Query1) - (select sum(col) as OtherTotal from Query2) as NewTotal



This could be done in a collection of subqueries like above or you could do it in a cte. Either way, that is basically what you need to do.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
djmason_2001
djmason_2001
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 11
Hi

it is in a report, i will need to show each value

so for eg
i need to see grand total of query1
grand total of query2
and the difference once query2 has been subtracted from query1. which produce a new total
djmason_2001
djmason_2001
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 11
hey

Has anyone else got any ideas on how to solve this issue ?
ChrisM@home
ChrisM@home
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8573 Visits: 10670
djmason_2001 (10/9/2012)
hey

Has anyone else got any ideas on how to solve this issue ?


CTE's are probably easiest for this, the two queries are I think may be too dissimilar to be merged together:

;WITH Query1 AS (
SELECT
coalesce (cast(DATENAME(MM,hst_date_processed) as varchar(20)), 'Grand Total') as [Months],
SUM(ffh.Net_Sales) AS [Sum Of Net Sales],
SUM(ffh.Net_Trans) AS [Sum Of Net Transactions]
FROM Fact_Financial_History ffh
INNER JOIN Dim_Interchange_Tier_2 i
ON (ffh.hst_prod_code + '-' + ffh.hst_plan_code) = i.Plan_Code
WHERE ffh.hst_date_processed >= @date
AND ffh.hst_prod_code IN ('79','81')
AND ffh.hst_plan_code IN ('002','010','011','015','037','033','021','019','020','030','029')
AND I.Plan_Code <> '79-021'
GROUP BY ffh.hst_date_processed
WITH ROLLUP
--ORDER BY ffh.hst_date_processed desc
),

Query2 AS (
SELECT *
FROM (
SELECT
CASE
WHEN GROUPING(o.FDMSAccountNo_First9) = 1 THEN 'Grand Total'
ELSE CAST(o.FDMSAccountNo_First9 AS VARCHAR(20))
END AS FDMSAccountNo_First9,
o.DBA_Name,
SUM(ffh.Net_Sales) AS TotalofNetAmount,
SUM(ffh.Net_Trans)AS TotalofNetTransactions
FROM Fact_Financial_History ffh
INNER JOIN Dim_Outlet o
ON ffh.hst_merchnum = o.FDMSAccountNo_First9
INNER JOIN dbo.Dim_MCC
ON o.MCC_Code = dbo.Dim_MCC.MCC
WHERE ffh.hst_date_processed > @date
AND ffh.hst_prod_code BETWEEN '79' AND '84'
AND o.MCC_Code = '5968'
GROUP BY
o.FDMSAccountNo_First9,
o.DBA_Name
WITH ROLLUP
) AS temp
WHERE temp.DBA_NAME IS NOT NULL
OR temp.FDMSAccountNo_First9 = 'Grand Total'
)
SELECT WhateverYouWant
FROM Query1
INNER/LEFT OUTER/FULL OUTER JOIN Query2
ON Query2.somecolumns = Query1.somecolumns
ORDER BY SomethingMeaningful




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
djmason_2001
djmason_2001
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 11
Hi

thank you for your response

In your last part of the query i dont get what this is meant to do

I understand you want me to select certain columns/titles.

but the on query2.somcolumns is confusing me


SELECT WhateverYouWant
FROM Query1
INNER/LEFT OUTER/FULL OUTER JOIN Query2
ON Query2.somecolumns = Query1.somecolumns
ORDER BY SomethingMeaningful



--------------------------------------------------------------------------------
ChrisM@home
ChrisM@home
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8573 Visits: 10670
djmason_2001 (10/9/2012)
Hi

thank you for your response

In your last part of the query i dont get what this is meant to do

I understand you want me to select certain columns/titles.

but the on query2.somcolumns is confusing me


SELECT WhateverYouWant
FROM Query1
INNER/LEFT OUTER/FULL OUTER JOIN Query2
ON Query2.somecolumns = Query1.somecolumns
ORDER BY SomethingMeaningful



--------------------------------------------------------------------------------


I've no idea how you might join these two CTE's together because I can't see your data - only you know that. "somecolumns" is a placeholder for what the real columns might be.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search