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
 Duplicating Records based on a Condition...?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLShmeequal
Starting Member

United Kingdom
2 Posts

Posted - 12/12/2012 :  10:20:19  Show Profile  Reply with Quote
Hi All

SQL Server 2012 Newbie(ish..) here and wondering if any of you awesome folk can help?

I have a table which logs customers food purchases last month and subsquently which promotions to include them in for next month.

Everyone will be included in campaign A, but some people will be selected for Campaign B, C and D based on their usage criteria.

CustID Age Soup Meat Fish Sweet Fruit PromotionToOffer
--------------------------------------------------------------------
1 18 3 12 5 13 8 Campaign-A
2 32 9 9 2 10 7 Campaign-A
3 44 12 6 1 6 12 Campaign-A
4 26 8 15 0 7 19 Campaign-A


I need to duplicate some of these records (based on criteria, e.g. 'everyone who is over 20 and eats more than 15 fruit a month') and in these new record, change the PromotionToOffer to be 'Campaign-B'

Could someone guide me on how to do this?

Much appreciated!



sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/12/2012 :  10:29:36  Show Profile  Reply with Quote
If you want to add another row (i.e., offer a second promotion) to the qualifying people, do something like this
INSERT INTO tbl
(CustID, Age, Soup, Meat, Fish, Sweet, Fruit, PromotionToOffer)
SELECT
	CustId,
	Age,
	Soup,
	Meat,
	Fish,
	Sweet,
	Fruit,
	'Campaign-B' AS PromotionToOffer
WHERE
	Age > 20 AND Fruit > 15;

If you want to replace such people's offer with Campaign-B, then use update as shown below
UPDATE tbl SET
	PromotionToOffer = 'Campaign-B'
WHERE
	Age > 20 AND Fruit > 15;
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8768 Posts

Posted - 12/12/2012 :  10:36:26  Show Profile  Visit webfred's Homepage  Reply with Quote
This is simple:
You have a select that gives you what you want and the only "wrong" thing is the PromotionToOffer column?

Then in your SELECT list change
select
CustID,
Age,
...,
PromotionToOffer
from ... where ...

to

select
CustID,
Age,
...,
'Campaign-B' as PromotionToOffer
from ... where ...




Too old to Rock'n'Roll too young to die.


Edited by - webfred on 12/12/2012 10:37:55
Go to Top of Page

SQLShmeequal
Starting Member

United Kingdom
2 Posts

Posted - 12/12/2012 :  10:53:32  Show Profile  Reply with Quote
Thanks Both - Sunita, you're first solution worked beautifully!
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.08 seconds. Powered By: Snitz Forums 2000