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 |
|
buzzi
Starting Member
48 Posts |
Posted - 2009-01-30 : 14:41:43
|
| Hello All,Can somebody please try to help me write a query to get the information that i am looking for.Below is the sample dataName Productjohn BerriesPhillips sqashjohn applesPhillips applesPhillips BerriesI need the out put to look like thisName ProductsJohn Berries,ApplesPhillips sqash,apples,berriesPlease let me know how can i get the desired result above.I know if i can create a function and pass the name as parameter to the function and get the all the products for the user in a comma separated way. Some thing that i can learn if there is a way to do it directly from a queryThanks for the help |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-30 : 14:46:56
|
| Here I have given answer in both SQL 2000 and 2005.http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=118937 |
 |
|
|
buzzi
Starting Member
48 Posts |
Posted - 2009-01-30 : 15:39:18
|
| Sodeep thank you so much. Sorry that i gave half information, But your response is still very usefull to me. But is there a way to extend thisName Product BuyDatejohn Berries 1/1/09Phillips sqash 1/1/09john apples 1/2/09Phillips apples 1/1/09Phillips Berries 1/2/09john oranges 1/2/09the result should be like thisname product buydatejohn berries 1/1/09john apples,oranges 1/2/09Phillips sqash,apples 1/1/09Phillips berries 1/2/09Thanks for your help |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-30 : 16:18:12
|
| [code]--Create Table VariableDeclare @t table([Name] varchar(15) ,Product varchar(15), BuyDate datetime)--Prepare Sample DataInsert @tSelect 'john','Berries', '1/1/09' union allSelect 'Phillips','sqash', '1/1/09' union allSelect 'john','apples', '1/2/09' union allSelect 'Phillips','apples','1/1/09' union allSelect 'Phillips','Berries', '1/2/09' union allSelect 'john','oranges','1/2/09'Select [Name],Max(Case when seq =1 then Product else '' end)+ ',' +Max(Case when seq =2 then Product else '' end)+ ',' +Max(Case when seq =3 then Product else '' end)+ ',' +Max(Case when seq =4 then Product else '' end) as Product,BuyDatefrom(SELECT [Name],Product,BuyDate,Row_number() over(partition by [Name],BuyDate order by [Name])as seqfrom @t) ZGroup by [Name],BuyDate--outputname product BuyDatejohn Berries, 2009-01-01 00:00:00.000john apples,oranges,, 2009-01-02 00:00:00.000Phillips sqash,apples,, 2009-01-01 00:00:00.000Phillips Berries,, 2009-01-02 00:00:00.000[/code] |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-30 : 16:30:58
|
| [code]--Create Table VariableDeclare @t table([Name] varchar(15) ,Product varchar(15), BuyDate datetime)--Prepare Sample DataInsert @tSelect 'john','Berries', '1/1/09' union allSelect 'Phillips','sqash', '1/1/09' union allSelect 'john','apples', '1/2/09' union allSelect 'Phillips','apples','1/1/09' union allSelect 'Phillips','Berries', '1/2/09' union allSelect 'john','oranges','1/2/09'SELECT z.name,LEFT(sl.list,LEN(sl.list)-1) as product,z.BuyDateFROM(SELECT DISTINCT [name],BuyDate FROM @t) zCROSS APPLY(SELECT product + ',' AS [text()]FROM @tWHERE [name] = z.name and BuyDate = z.BuyDateFOR XML PATH(''))sl(list)--outputname product BuyDatejohn Berries 2009-01-01 00:00:00.000john apples,oranges 2009-01-02 00:00:00.000Phillips sqash,apples 2009-01-01 00:00:00.000Phillips Berries 2009-01-02 00:00:00.000[/code] |
 |
|
|
buzzi
Starting Member
48 Posts |
Posted - 2009-01-30 : 17:20:49
|
| Thanks a lot, i modified the query that you gavethis works for me perfectly |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-30 : 17:24:43
|
You are Welcome . |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-30 : 18:01:14
|
Or Simply this:quote: Originally posted by sodeep
--Create Table VariableDeclare @t table([Name] varchar(15) ,Product varchar(15), BuyDate datetime)--Prepare Sample DataInsert @tSelect 'john','Berries', '1/1/09' union allSelect 'Phillips','sqash', '1/1/09' union allSelect 'john','apples', '1/2/09' union allSelect 'Phillips','apples','1/1/09' union allSelect 'Phillips','Berries', '1/2/09' union allSelect 'john','oranges','1/2/09'SELECT DISTINCT [name],STUFF((SELECT ','+ Product FROM @t WHERE [name] = m.[name] and BuyDate = m.BuyDate FOR XML PATH('')),1,1,'') AS List,BuyDateFROM @t m--outputname product BuyDatejohn Berries 2009-01-01 00:00:00.000john apples,oranges 2009-01-02 00:00:00.000Phillips sqash,apples 2009-01-01 00:00:00.000Phillips Berries 2009-01-02 00:00:00.000
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-31 : 08:22:38
|
| [code]SELECT DISTINCT t.name,STUFF((SELECT ','+ product FROM @tWHERE [name] = t.name and BuyDate = t.BuyDate FOR XML PATH('')),1,1,''),t.BuyDateFROM Table t[/code] |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-31 : 09:06:51
|
quote: Originally posted by visakh16
SELECT DISTINCT t.name,STUFF((SELECT ','+ product FROM @tWHERE [name] = t.name and BuyDate = t.BuyDate FOR XML PATH('')),1,1,''),t.BuyDateFROM Table t
I have already shown this. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-31 : 09:21:38
|
quote: Originally posted by sodeep
quote: Originally posted by visakh16
SELECT DISTINCT t.name,STUFF((SELECT ','+ product FROM @tWHERE [name] = t.name and BuyDate = t.BuyDate FOR XML PATH('')),1,1,''),t.BuyDateFROM Table t
I have already shown this.
Oops sorryseems like i didnt notice |
 |
|
|
|
|
|
|
|