October 8, 2012 at 12:18 pm
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'
October 8, 2012 at 2:33 pm
Does anyone have any ideas?
October 8, 2012 at 3:04 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 8, 2012 at 3:09 pm
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
October 9, 2012 at 12:34 am
hey
Has anyone else got any ideas on how to solve this issue ?
October 9, 2012 at 1:46 am
djmason_2001 (10/9/2012)
heyHas 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
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
October 9, 2012 at 2:42 am
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
--------------------------------------------------------------------------------
October 9, 2012 at 3:20 am
djmason_2001 (10/9/2012)
Hithank 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.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply