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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Calculations
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

servmgr2004
Starting Member

USA
10 Posts

Posted - 12/20/2012 :  13:24:12  Show Profile  Reply with Quote
I sell CDs. They are shipped to different locations. The query below says if a CD is made for 1 location(East), change the count to zero and add its CDs to the other location (West).


	UPDATE #ShipLog
		SET Media_Copies = Media_Copies + (SELECT Media_Copies FROM #ShipLog WHERE Region = 'East' AND Job_Type = 'CD')
			WHERE Region = 'West' AND Job_Type = 'CD'

	UPDATE #ShipLog
		SET Media_Copies = 0
			WHERE Region = 'East' AND Job_Type = 'CD'



Now, the problem is if I have 6 CDs that are created for one location (East) that I need evenly split between two locations (West & South).

I want the query to say IF there are 6 CDs that have printed for East, THEN change the count to zero and SPLIT the CD count between West & South.

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/20/2012 :  13:29:01  Show Profile  Reply with Quote

UPDATE #ShipLog
SET Media_Copies = CASE WHEN SUM(CASE WHEN Region ='East' AND Job_Type = 'CD' THEN 1 ELSE 0 END) OVER () = 6 AND Region = 'East' THEN 0 ELSE Media_Copies + 3 END
WHERE Region in ('East','West','South') AND Job_Type = 'CD'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

servmgr2004
Starting Member

USA
10 Posts

Posted - 12/20/2012 :  13:36:34  Show Profile  Reply with Quote
Great, but I didn't mention that it need to split whatever the CD count is between two locations, meaning the count will not always be 6. It has to divide the CD count by two then move thier totals (whatever it is) to the other two locations.

Something like: A1 has X number of Cds, (X dividied by 2) = Y, then change A1 to zero and add Y to B1 and B2
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/20/2012 :  13:48:21  Show Profile  Reply with Quote

UPDATE s
SET Media_Copies = CASE WHEN EastTotal >0 AND Region = 'East' THEN 0 ELSE Media_Copies + EastTotal/2 END
FROM (SELECT *,SUM(CASE WHEN Region ='East' AND Job_Type = 'CD' THEN 1 ELSE 0 END) OVER () AS EastTotal FROM #ShipLog)s
WHERE Region in ('East','West','South') AND Job_Type = 'CD'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

servmgr2004
Starting Member

USA
10 Posts

Posted - 12/21/2012 :  07:16:36  Show Profile  Reply with Quote
It errors on the "s" at the end of the FROM statement.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/21/2012 :  07:54:15  Show Profile  Reply with Quote
it wont if you close braces properly (its working fine for me)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2224 Posts

Posted - 12/21/2012 :  07:59:20  Show Profile  Reply with Quote
quote:
Originally posted by servmgr2004

It errors on the "s" at the end of the FROM statement.


Can you post us the error message?

--
Chandu
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