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
 Duplicating Records based on a Condition...?

Author  Topic 

SQLShmeequal
Starting Member

2 Posts

Posted - 2012-12-12 : 10:20:19
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-12 : 10:29:36
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
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-12-12 : 10:36:26
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.

Go to Top of Page

SQLShmeequal
Starting Member

2 Posts

Posted - 2012-12-12 : 10:53:32
Thanks Both - Sunita, you're first solution worked beautifully!
Go to Top of Page
   

- Advertisement -