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.
| Author |
Topic |
|
atlzbest
Starting Member
7 Posts |
Posted - 2010-06-03 : 15:02:15
|
| Hi All,I am having trouble putting this query together, any tips would be helpful.The table structure looks like thisCustomerNum PostDate OfferAccepted OfferName1000 5/1/10 Y Test11000 5/1/10 Y Test21000 5/1/10 N Test31001 5/1/10 N Test11001 5/1/10 N Test21001 5/1/10 Y Test3I want to flatten the table by each CustomerNum e.g.CustomerNum PostDate OffAcctd OfferName1 OfferName2 OfferName31000 5/1/10 Y Test1 Test2 Test31000 5/1/10 Y Test3 Reason for that is i have an older process that updates a dest. table that looks like the above flattened table. I am adding a new source and trying to do a UNION of the two tables so i need the newer one flattened. The only important thing above is I only keep the CustomerNumbers is they have atleast 1 OfferAccepted=Y and no more than 3.Sorry the table cell alignment is a bit off.Thanks in advance! |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-06-03 : 15:32:43
|
DECLARE @table table (CustomerNum int, PostDate datetime, OfferAccepted char(1), OfferName varchar(10))INSERT INTO @Tableselect 1000,'5/1/10','Y','Test1' UNION ALLselect 1000,'5/1/10','Y','Test2' UNION ALLselect 1000,'5/1/10','Y','Test3' UNION ALLselect 1001,'5/1/10','N','Test1' UNION ALLselect 1001,'5/1/10','N','Test2' UNION ALLselect 1001,'5/1/10','Y','Test3'SELECT customerNum,postDate,OfferAccepted ,MAX(CASE WHEN rank = 1 THEN OfferName END) as OfferName1 ,MAX(CASE WHEN rank = 2 THEN OfferName END) as OfferName2 ,MAX(CASE WHEN rank = 3 THEN OfferName END) as OfferName3 FROM(select customerNum,postDate,OfferAccepted,OfferName ,[Rank] = rank() over(partition by customerNum,postDate order by offername) from @tablewhere OfferAccepted = 'Y') tGROUP By customerNum,postDate,OfferAccepted JimP.S. you can use the tags to keep your columnsline upJimEveryday I learn something that somebody else already knew |
 |
|
|
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2010-06-03 : 16:33:23
|
| SELECT customerNum,postDate,OfferAccepted ,MAX(CASE WHEN OfferName = 'Test1' THEN 'Test1' END) as OfferName1 ,MAX(CASE WHEN OfferName = 'Test2' THEN 'Test2' END) as OfferName2 ,MAX(CASE WHEN OfferName = 'Test3' THEN 'Test3' END) as OfferName3 FROM@TableWHERE OfferAccepted = 'Y'GROUP By customerNum,postDate,OfferAcceptedORDER BY customerNum |
 |
|
|
atlzbest
Starting Member
7 Posts |
Posted - 2010-06-07 : 10:11:26
|
| Thanks guys, this is a fantastic start!What would i need to do to find a relationship like: CustomerNum record 1 ALWAYS has a OfferAccepted= Y and the remaining 2 records it doesn't matter whether OfferAccepted is Y or N i am thinking of a corelated query or a self-join but i am failing at writing one. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-06-07 : 12:00:51
|
quote: Originally posted by atlzbest Thanks guys, this is a fantastic start!What would i need to do to find a relationship like: CustomerNum record 1 ALWAYS has a OfferAccepted= Y and the remaining 2 records it doesn't matter whether OfferAccepted is Y or N i am thinking of a corelated query or a self-join but i am failing at writing one.
I'm sorry...but can you elaborate. Can you show us the expected output. I thought Jim's query already provided your expected output. |
 |
|
|
atlzbest
Starting Member
7 Posts |
Posted - 2010-06-07 : 12:22:26
|
| Sorry guys! Here goes:create table #table2 ( CustomerNum int, PostDate datetime, OfferAccepted char(1), OfferName varchar(10), )insert into #table2select 2001, '5/1/10', 'Y', 'Test1' unionselect 2001, '5/1/10', 'N', 'Test2' UNIONselect 2001, '5/1/10', 'N', 'Test3' UNIONselect 2001, '5/1/10', 'N', 'Test4' UNIONselect 2002, '5/2/10', 'Y', 'Test3' unionselect 2003, '5/2/10', 'N', 'Test1' UNIONselect 2003, '5/2/10', 'N', 'Test2' UNIONselect 2003, '5/2/10', 'N', 'Test4' UNION select 2003, '5/2/10', 'N', 'Test5' Desired output isCustomerNum PostDate Offer1 Offer2 Offer32001 5/1/10 Test1 Test2 Test32002 5/2/10 Test3Explanation: Pick top 3 offers by CustomerNum. CustomerNum must have atleast 1 offer with OfferAccepted = Y. Display Top 3 offers as columns (only 1 record per CustomerNum) |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-06-07 : 12:32:34
|
| That's what my query produces. Am I still not getting what you want in the new query?JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|
|
|