Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-05 : 07:33:12
|
Hey guys i need some help i have created the following query Declare @date varchar(10)set @Date = (select dateadd(MM,-2,max(hst_date_processed))from FDMS.dbo.Fact_Financial_History) SELECT Fact_Financial_History.hst_date_processed, --Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.Region,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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,--Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.regionorder by Fact_Financial_History.hst_date_processed desc at present the query produces the results as follows hst_date_processed Region Sum Of NetSales Sum Of Net Transactions01/06/2012 EU 2031196.44 1679401/06/2012 INT 1479859.09 1327801/07/2012 EU 1974633.66 1467301/06/2012 UK 99433288.66 90380501/08/2012 EU 2242086.39 1558901/08/2012 INT 1859279.81 1332301/07/2012 INT 1486238.01 1268201/07/2012 UK 112457878.6 101568401/08/2012 UK 104054609.7 1030256i however need the results to be displayed as Month EU NT UK Grand Total August net Sales 2242086 1859280 104054609.7 108155976August net trans 15589 13323 1030256 1059168July net Sales 1974634 1486238 112457878.58 115918750July net trans 14673 12682 1015684 1043039june net Sales 2031196 1479859 99433288.66 102944344june net trans 16794 13278 903805 933877 Total net sales 6247916 4825377 315945776.9 327019070 Total net Trans 47056 39283 2949745 3036084 Does anyone have any ideas how i can achieve this ? ps( i dont have reporting services) |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-10-05 : 08:15:58
|
Check this oneEdit:--> Replace 'grand total' as ISNULL([EU], 0)+ISNULL([INT], 0)+ISNULL([UK], 0) in both cases--> Replace UNION ALL with UNIONDeclare @date varchar(10)set @Date = (select dateadd(MM,-2,max(hst_date_processed))from FDMS.dbo.Fact_Financial_History) SELECT DISTINCT DATENAME(MM, hst_date_processed)+' Net Sales' as Months, [EU], [INT], [UK], [EU]+[INT]+[UK] as 'Grand Total'FROM(SELECT Fact_Financial_History.hst_date_processed, --Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.Region,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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,--Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.region) as pPIVOT( MAX( [Sum Of Net Sales]) FOR Region IN ([EU], [INT], [UK]) ) as pvtUNION ALLSELECT DISTINCT DATENAME(MM, hst_date_processed)+' Net Trans' as Months, [EU], [INT], [UK], [EU]+[INT]+[UK] as 'Grand Total'FROM(SELECT Fact_Financial_History.hst_date_processed, --Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.Region,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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,--Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.region) as pPIVOT( MAX( [Sum Of Net Transactions]) FOR Region IN ([EU], [INT], [UK]) ) as pvtorder by Months This code will get only this partMonth EU NT UK Grand Total August net Sales 2242086 1859280 104054609.7 108155976August net trans 15589 13323 1030256 1059168July net Sales 1974634 1486238 112457878.58 115918750July net trans 14673 12682 1015684 1043039june net Sales 2031196 1479859 99433288.66 102944344june net trans 16794 13278 903805 933877--Chandu |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-05 : 08:33:10
|
HI bandi Sorry to seem thick but i take it you mean like so .. Declare @date varchar(10)set @Date = (select dateadd(MM,-2,max(hst_date_processed))from FDMS.dbo.Fact_Financial_History) SELECT DISTINCT DATENAME(MM, hst_date_processed)+' Net Sales' as Months, [EU], [INT], [UK], ISNULL([EU], 0)+ISNULL([INT], 0)+ISNULL([UK], 0) as 'Grand Total'FROM(SELECT Fact_Financial_History.hst_date_processed, --Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.Region,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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,--Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.region) as pPIVOT( MAX( [Sum Of Net Sales]) FOR Region IN ([EU], [INT], [UK]) ) as pvtUNION SELECT DISTINCT DATENAME(MM, hst_date_processed)+' Net Trans' as Months, [EU], [INT], [UK], ISNULL([EU], 0)+ISNULL([INT], 0)+ISNULL([UK], 0) as 'Grand Total'FROM(SELECT Fact_Financial_History.hst_date_processed, --Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.Region,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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,--Dim_Interchange_Tier_2.Qualification_2,Dim_Interchange_Tier_2.region) as pPIVOT( MAX( [Sum Of Net Transactions]) FOR Region IN ([EU], [INT], [UK]) ) as pvtorder by Months if thats the case then my results will be displayed as . Months EU INT UK Grand TotalAugust Net Sales NULL NULL 104054609.69 104054609.69August Net Sales NULL 1859279.81 NULL 1859279.81August Net Sales 2242086.39 NULL NULL 2242086.39August Net Trans NULL NULL 1030256.00 1030256.00August Net Trans NULL 13323.00 NULL 13323.00August Net Trans 15589.00 NULL NULL 15589.00July Net Sales NULL NULL 112457878.58 112457878.58July Net Sales NULL 1486238.01 NULL 1486238.01July Net Sales 1974633.66 NULL NULL 1974633.66July Net Trans NULL NULL 1015684.00 1015684.00July Net Trans NULL 12682.00 NULL 12682.00July Net Trans 14673.00 NULL NULL 14673.00June Net Sales NULL NULL 99433288.66 99433288.66June Net Sales NULL 1479859.09 NULL 1479859.09June Net Sales 2031196.44 NULL NULL 2031196.44June Net Trans NULL NULL 903805.00 903805.00June Net Trans NULL 13278.00 NULL 13278.00June Net Trans 16794.00 NULL NULL 16794.00ideally i need a one liner per month for net sales and net transso for eg eu int Uk Grand total August Net sales 100 200 300 600Is that possible ? |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-05 : 09:02:58
|
Bandi thank you for providing that link, it was a simple sum which threw me out ? the query should of been Declare @date varchar(10)set @Date = (select dateadd(MM,-2,max(hst_date_processed))from FDMS.dbo.Fact_Financial_History) SELECT DISTINCT DATENAME(MM, hst_date_processed)+'Net Sales' as Months,[EU], [INT], [UK], ISNULL([EU],0)+ISNULL([INT],0)+ISNULL([UK],0) as 'Grand Total'FROM(SELECT Fact_Financial_History.hst_date_processed, Dim_Interchange_Tier_2.Region,SUM(Fact_Financial_History.Net_Sales) AS [Sum Of Net Sales]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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,Dim_Interchange_Tier_2.region) as pPIVOT( MAX( [Sum Of Net Sales]) FOR Region IN ([EU], [INT], [UK]) ) as pvtUNION SELECT DISTINCT DATENAME(MM, hst_date_processed)+'Net Trans' as Months, [EU], [INT], [UK], ISNULL([EU], 0)+ISNULL([INT], 0)+ISNULL([UK], 0) as 'Grand Total'FROM(SELECT Fact_Financial_History.hst_date_processed, Dim_Interchange_Tier_2.Region,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_CodeWHERE (Fact_Financial_History.hst_date_processed >= @date)and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')GROUP BY Fact_Financial_History.hst_date_processed,Dim_Interchange_Tier_2.region) as pPIVOT( MAX( [Sum Of Net Transactions]) FOR Region IN ([EU], [INT], [UK]) ) as pvtorder by Months |
|
|
|
|
|
|
|