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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inner join confusion

Author  Topic 

FData
Starting Member

24 Posts

Posted - 2012-08-31 : 05:30:51
Hey guys

I need some inner join help

I have built my querys in separate parts, and i am trying to join them all together now so i can get the final result

But i am struggling. I would appreciate any help available

The final layout, should be displayed as.

[Rolling 12 Net Sales]
[Rolling 12 MSC]
[Rolling 12 Net Interchange]
[Rolling 12 DI]
[Rolling 12 Scheme Fees]
[Rolling 12 DIA]
[Rolling 12 OtherRep]
[Rolling 12 Funding]
[Rolling12 Processing]
[rolling 12 Repcon]
[Rolling 12 PCI]
[Rolling 12 RTCharge]
[Rolling 12 NonRep]

My query is




-------------------------------- Sales ------------------------------

Declare @date varchar(10)
set @Date = (select dateadd(MM,-12,max(hst_date_processed))
from FDMS.dbo.Fact_Financial_History)
SELECT ParentID,
sum([Net_Sales]) as Net_Sales_Rolling12,
SUM ([Net_Intg]) as Net_Intg_Rolling12,
SUM ([Scheme_Fees]) as Scheme_Fees_Rolling12,
SUM ([Funding_Amt]) as Funding_Amt_Rolling12

Into #Sales
FROM [FDMS].[dbo].[Fact_Financial_History]f full outer join Dim_Outlet o
on f.hst_merchnum = o.FDMSAccountNo_First9
Where hst_date_processed > @date
group by o.ParentID

--select* from #Sales

--------------------------------ProcessingCost -----------------------

Declare @date varchar(10)
set @Date = (select dateadd(MM,-12,max(Period))
from [FDMS].[dbo].[Fact_ProcessingCost])
SELECT ParentID
,sum([Value]) as ProcessingCost_Rolling12
Into #Processing
FROM [FDMS].[dbo].[Fact_ProcessingCost]f full outer join Dim_Outlet o
on f.FDMSAccountNo = o.ParentID
Where Period > @date
group by o.ParentID

--select * from #Processing
--drop table #r1

------------------------------------MSC ----------------------------

SELECT
FDMSAccountNo,
[Month_end_date] as hst_date_processed,
Case when Fee_Code IN ('42B','42C','42D','42E') Then 'PCI'
When Fee_Code = '00Y' Then 'Refund Transaction Charge'
When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'Y' then 'Other repeatable'
When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'N' then 'Other Non repeatable'
Else salesMI_Group1 end as description,
d.repeat_msc,
Sum([Retail_amount]) As Amount
Into #MscCount
FROM FDMS.dbo.Fact_Fee_History f
INNER JOIN
FDMS.dbo.Dim_Fee_Codes D ON f.Fee_Sequence_Number = D.Fee_Code
where Month_end_date >= dateadd(mm,datediff(mm,0,getdate())-12,0)
and Month_end_date < dateadd(mm,datediff(mm,0,getdate()),0)
and FDMSAccountNo ='878000039882'
Group by FDMSAccountNo, Month_end_date, d.Fee_Code, d.SalesMI_Group1, d.Repeat_MSC

select * from #MscCount

--drop table #MscCount, #msc


SELECT [FDMSAccountNo],hst_date_processed,
SUM ( case when description ='MSC' then ([Amount])else 0 end ) as [MSC_2012],
SUM ( case when description ='PCI' then ([Amount])else 0 end ) as [PCI_2012],
SUM ( case when description ='Refund Transaction Charge' then ([Amount])else 0 end ) as [RefundTransactionCharge_2012],
SUM ( case when description ='Other repeatable' then ([Amount])else 0 end ) as [Other_repeatable2012],
SUM ( case when description ='Other Non repeatable' then ([Amount])else 0 end ) as [Other_Non_repeatable2012]
Into #Msc
FROM #MscCount
where FDMSAccountNo = '878000039882'
group by [FDMSAccountNo],hst_date_processed

--select * from #msc


into #results
from #Sales
full outer join #Processing on f.hst_merchnum = left(p.FDMSAccountNo,9)




------------------------------------Results Layout---------------------------------
--Select

-- [Net_Sales_Rolling12] as [Rolling 12 Net Sales]
-- [Net_Intg_Rolling12] as [Rolling 12 Net Interchange]
-- [Scheme_Fees_Rolling12] as[Rolling 12 Scheme Fees]
-- [Funding_Amt_Rolling12] as [Rolling 12 Funding]
-- [MSC_2012] as [Rolling 12 MSC]
-- [PCI_2012] as [Rolling 12 PCI]
-- [RefundTransactionCharge_2012] as [Rolling 12 RTCharge]
-- [Other_repeatable2012] as [Rolling 12 OtherRep]
-- [Other_Non_repeatable2012] as [Rolling 12 NonRep]


------------------------------ Calculations ------------------------------
Select *,
[Rolling 12 MSC]-[Rolling 12 Net Interchange] as [Rolling 12 DI],
[Rolling 12 MSC]-[Rolling 12 Net Interchange] -[Rolling 12 Scheme Fees] as [Rolling 12 DIA],
SUM ([Rolling 12 DIA]+[Rolling 12 OtherRep]+[Rolling 12 Funding])- SUM([Rolling 12 Funding]) as [rolling 12 Repcon]


----------------------------------- F Results -----------------

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-31 : 06:09:09
one question, for the sales & cost calculation, you are based on the latest date in each of the table and then calculate 12 month back.
And in MSC, you are based on current date. Isn't it there is a possibility that you are not comparing based on the same period ?


set @Date = (select dateadd(MM,-12,max(hst_date_processed))
from FDMS.dbo.Fact_Financial_History)

set @Date = (select dateadd(MM,-12,max(Period))
from [FDMS].[dbo].[Fact_ProcessingCost])



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

FData
Starting Member

24 Posts

Posted - 2012-08-31 : 06:42:31
Hi khtan

MSC table should have the same date function as sales & cost calculation so set @Date = (select dateadd(MM,-12,max(hst_date_processed))


I just wasnt sure how to add it into my query

If you can resolve that issue, along with my inner join problem, you are a *
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-31 : 06:50:10
not sure how Sales & Processing is related to the Msc.

Anyway, it should be something like this. You have to make changes to the columns and join condition on the last query in blue


; with
Sales as
(
SELECT ParentID,
sum ([Net_Sales]) as Net_Sales_Rolling12,
SUM ([Net_Intg]) as Net_Intg_Rolling12,
SUM ([Scheme_Fees]) as Scheme_Fees_Rolling12,
SUM ([Funding_Amt]) as Funding_Amt_Rolling12
FROM [FDMS].[dbo].[Fact_Financial_History]f
full outer join Dim_Outlet o on f.hst_merchnum = o.FDMSAccountNo_First9
Where hst_date_processed > @date
group by o.ParentID
),
Processing as
(
SELECT ParentID
,sum([Value]) as ProcessingCost_Rolling12
FROM [FDMS].[dbo].[Fact_ProcessingCost]f
full outer join Dim_Outlet o on f.FDMSAccountNo = o.ParentID
Where Period > @date
group by o.ParentID
),
MscCount as
(
SELECT FDMSAccountNo,
[Month_end_date] as hst_date_processed,
Case when Fee_Code IN ('42B','42C','42D','42E') Then 'PCI'
When Fee_Code = '00Y' Then 'Refund Transaction Charge'
When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'Y' then 'Other repeatable'
When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'N' then 'Other Non repeatable'
Else salesMI_Group1
end as description,
d.repeat_msc,
Sum([Retail_amount]) As Amount
FROM FDMS.dbo.Fact_Fee_History f
INNER JOIN FDMS.dbo.Dim_Fee_Codes D ON f.Fee_Sequence_Number = D.Fee_Code
where Month_end_date >= dateadd(mm,datediff(mm,0,getdate())-12,0)
and Month_end_date < dateadd(mm,datediff(mm,0,getdate()),0)
and FDMSAccountNo ='878000039882'
Group by FDMSAccountNo, Month_end_date, d.Fee_Code, d.SalesMI_Group1, d.Repeat_MSC
),
Msc as
(
SELECT [FDMSAccountNo],
hst_date_processed,
SUM ( case when description ='MSC' then ([Amount])else 0 end ) as [MSC_2012],
SUM ( case when description ='PCI' then ([Amount])else 0 end ) as [PCI_2012],
SUM ( case when description ='Refund Transaction Charge' then ([Amount])else 0 end ) as [RefundTransactionCharge_2012],
SUM ( case when description ='Other repeatable' then ([Amount])else 0 end ) as [Other_repeatable2012],
SUM ( case when description ='Other Non repeatable' then ([Amount])else 0 end ) as [Other_Non_repeatable2012]
FROM MscCount
where FDMSAccountNo = '878000039882'
group by [FDMSAccountNo], hst_date_processed
)
select *
from Sales s
full outer join Processing p on f.ParentID = p.ParentID
full outer join Msc m on f.ParentID = m.FDMSAccountNo



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

FData
Starting Member

24 Posts

Posted - 2012-08-31 : 06:55:43
Hi khtan

For every fdmsaccountno there will be a msc,pci, processing cost, net sales etc etc
so what i am trying to build isa table which will give me the following

fdmsaccountno , Pci, Net sales, schemefees etc etc
Go to Top of Page

FData
Starting Member

24 Posts

Posted - 2012-08-31 : 07:05:26
Hi
What do you mean by' changes to the columns and join condition on the last query in blue
'
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-31 : 09:36:25
this part of the query.
select	*
from Sales s
full outer join Processing p on f.ParentID = p.ParentID
full outer join Msc m on f.ParentID = m.FDMSAccountNo



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -