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
 Update Help

Author  Topic 

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2013-10-09 : 11:42:57
Hey guys

I need some help

Aim- Join both results together, if there is a parentid, in both #MSIP and other query, then do an update, and update the sales column to reflect the correct amount

For eg
Parentid Parent_Name Sales
MSIP 878201390886 ABERDEEN UNIVERSITY 16563736.36
878201390886 ABERDEEN UNIVERSITY 4289717.05

Results
878201390886 ABERDEEN UNIVERSITY 20853453.41



-- Date Declaration Omnipay rolling12--
Declare @R12Omni datetime
set @R12Omni = (select dateadd(MM,-11,max(Period))
from [FDMS].[dbo].[Fact_Omnipay_Profitability])
select @R12Omni


SELECT
Dim_Outlet.ParentID,
Dim_Outlet.Parent_Name,
sum( Fact_Financial_History_2.hst_sales_amt_R12) as Sales
into #MSIP
FROM Fact_Financial_History_2 INNER JOIN
Dim_Outlet ON Fact_Financial_History_2.hst_merchnum = Dim_Outlet.FDMSAccountNo_First9
where Agent_Chain_No not in ('878970059886', '878970013883')
AND fdmsaccountno NOT IN (SELECT [ta_mid] FROM fdms_partnerreporting.tmp.trade_assocations)
and Account_Status ='16'
group by ParentID,Parent_Name
having SUM (hst_sales_amt_R12) > 1999999
--order by Parent_Name asc

union all

SELECT
ParentID,
Dim_Outlet.Parent_Name,
Sum([Tot_Purch_Amt]) as 'Sales'
FROM Fact_Omnipay_Profitability
INNER JOIN Dim_Outlet ON Fact_Omnipay_Profitability.FDMSAccountNo = Dim_Outlet.FDMSAccountNo
where Period >= @R12Omni
group by Dim_Outlet.ParentID,Parent_Name

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2013-10-09 : 11:49:12
i believe the update be something along the lines of

update #MSIP
set ParentID =
where (SELECT coalesce (SUM(Sales),0)
FROM #MSIP
where ParentID = ParentID
group by ParentID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-09 : 13:04:19
sonds like this

update m
set m.Sales = m.Sales + tmp.Sales
FROM #MSIP m
INNER JOIN (SELECT
ParentID,
Dim_Outlet.Parent_Name,
Sum([Tot_Purch_Amt]) as 'Sales'
FROM Fact_Omnipay_Profitability
INNER JOIN Dim_Outlet ON Fact_Omnipay_Profitability.FDMSAccountNo = Dim_Outlet.FDMSAccountNo
where Period >= @R12Omni
group by Dim_Outlet.ParentID,Parent_Name
)tmp
ON tmp.Parent_Name = m.Parent_Name
AND tmp.ParentID = m.ParentID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -