SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 07/22/2013 :  10:55:00  Show Profile  Reply with Quote
Hey guys
I really need your help and i have been looking at examples on line and haven’t progressed any further in the last 2 hours

Aim – to get the values in the temp table (Mca2) column MCA to update the values within table #build2 column MCA, if certain criteria is met.

For eg table #build2 column MCA if the values = 'FD MCA' or 'MCA - External party' then don’t update, however if value = 'Potential Opportunity' then use the values in table #mca2

--- build 2table --
select
#final.FDMSAccountNo,
#final.Account_Status,
#final.Amex,
#final.Omnipay,

CASE
WHEN Clientline IS NULL THEN 'No' WHEN Clientline is not null THEN 'Yes' ELSE 'Check'END as Clientline,

case
when DCC IS NULL THEN 'No R12 Financials' when dcc = 'No DCC Opportunity' then 'No DCC Opportunity' else 'DCC Opportunity' end as DCC,

case
when mca IS NULL THEN 'Potential Opportunity' when mca = 'FD' then 'FD MCA' else 'MCA - External party' end as MCA,

CASE
WHEN VT IS NULL THEN 'No' WHEN VT is not null THEN 'Yes' ELSE 'Check'END as VT

into #build2
from #clientline right join #final on #final.FDMSAccountNo = #clientline.FDMSAccountNo
order by #final.MCA Desc

--- -- MCA2update --
SELECT
FDMSAccountNo,
SUM(Fact_Financial_History_2.hst_sales_amt_R12) / 12 AS SalesAvg,
CASE WHEN SUM([hst_sales_amt_R12]) / 12 IS NULL THEN 'No R12 Financials'
WHEN SUM([hst_sales_amt_R12]) / 12 BETWEEN 0 AND 999.99 THEN 'No MCA Opportunity'
WHEN SUM([hst_sales_amt_R12]) / 12 > 1000 THEN 'Potential Opportunity' ELSE 'Check' END AS MCA
into #mca2
FROM Fact_Financial_History_2
full outer join Dim_Outlet ON Fact_Financial_History_2.hst_merchnum = Dim_Outlet.FDMSAccountNo_First9
GROUP BY Dim_Outlet.FDMSAccountNo

SwePeso
Patron Saint of Lost Yaks

Sweden
30115 Posts

Posted - 07/22/2013 :  11:36:34  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT		f.FDMSAccountNo,
		f.Account_Status,
		f.Amex,
		f.Omnipay,
		CASE 
			WHEN cl.Clientline IS NULL THEN 'No'
			WHEN cl.Clientline IS NOT NULL THEN 'Yes'
			ELSE 'Check'
		END AS Clientline,
		CASE 
			WHEN cl.DCC IS NULL THEN 'No R12 Financials'
			WHEN cl.dcc = 'No DCC Opportunity' THEN 'No DCC Opportunity'
			ELSE 'DCC Opportunity'
		END AS DCC,
		CASE
			WHEN cl.mca IS NULL THEN 'Potential Opportunity'
			WHEN cl.mca = 'FD' THEN 'FD MCA'
			ELSE 'MCA - External party'
		END AS MCA,
		CASE 
			WHEN cl.VT IS NULL THEN 'No'
			WHEN cl.VT IS NOT NULL THEN 'Yes'
			ELSE 'Check'
		END AS VT
INTO		#Build2
FROM		#Final AS f
LEFT JOIN	#ClientLine AS cl ON cl..FDMSAccountNo = f.FDMSAccountNo;

-- MCA2update
SELECT		o.FDMSAccountNo, 
		SUM(fh.hst_sales_amt_R12) / 12E AS SalesAvg, 
		CASE
			WHEN SUM(fh.hst_sales_amt_R12) IS NULL THEN 'No R12 Financials' 
			WHEN SUM(fh.hst_sales_amt_R12) / 12E BETWEEN 0 AND 999.99 THEN 'No MCA Opportunity' 
			WHEN SUM(fh.hst_sales_amt_R12) / 12E > 1000 THEN 'Potential Opportunity'
			ELSE 'Check'
		END AS MCA
INTO		#Mca2
FROM		dbo.Fact_Financial_History_2 AS fh
INNER JOIN	dbo.Dim_Outlet AS o ON o.FDMSAccountNo_First9 = fh.hst_merchnum
GROUP BY	o.FDMSAccountNo;

-- SwePeso
UPDATE		b
SET		b.MCA = w.MCA
FROM		#Build2 AS b
INNER JOIN	#Mca2 AS w ON ON w.FDMSAccountNo = b.FDMSAccountNo
WHERE		b.MCA = 'Potential Opportunity';



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000