| Author |
Topic |
|
raaj
Posting Yak Master
129 Posts |
Posted - 2009-12-02 : 20:56:32
|
| Hi Guys,I am having a small problem with the query which I have written.According to that query, it returns the following sample result set:ID CODE NAME CONTACTPERSON AMOUNT123 8214 Raaj KEVIN 200123 8214 Raaj GEORGE 200124 8000 Ram SHANE 100128 9000 VIJ BOND 500128 9000 VIJ STEVE 500 But What I actually want the result set to look like is in this way:ID CODE NAME CONTACTPERSON AMOUNT123 8214 Raaj KEVIN 200 GEORGE 124 8000 Ram SHANE 100128 9000 VIJ BOND 500 STEVE AS you can see in the above result set when ID,CODE,NAME and AMOUNT have same values, I would like to show only the CONTACTPERSON for the next row.Any ideas of How to proceed?Thanks,Raaj. |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2009-12-02 : 23:50:20
|
u can use ROW_NUMBER() function to achieve this, some thing likeSELECT CASE WHEN Rno = 1 THEN ID ELSE NULL END AS ID, CASE WHEN Rno = 1 THEN CODE ELSE NULL END AS CODE,...FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ID, CODE, NAME ORDER BY ID, CODE, NAME) AS RnoFROM tbl) A"There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!" |
 |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2009-12-03 : 19:07:52
|
| Hi Peter,Thanks for your reply.I tried what you suggested. But its showing NULL values for the Duplicate rows.....Here is the sample query which I have written:Declare @Mytable Table (ID Int, Code Int, Name varchar(10), ContactPerson Varchar(10), Amount money)Insert into @MyTable values(123, 8214, 'Raaj', 'Kevin', 200)Insert into @MyTable values(123, 8214, 'Raaj', 'George', 200)Insert into @MyTable values(124, 8000, 'Ram', 'Shane', 100)Insert into @MyTable values(128, 9000, 'VIJ', 'BOND', 500)Insert into @MyTable values(128, 9000, 'VIJ', 'STEVE', 500)--Select * from @MyTableSELECT CASE WHEN Rno = 1 THEN ID ELSE NULL END AS ID,CASE WHEN Rno = 1 THEN CODE ELSE NULL END AS CODE,CASE WHEN Rno = 1 THEN NAME ELSE NULL END AS NAME,CASE WHEN Rno = 1 THEN CONTACTPERSON ELSE NULL END AS CONTACTPERSON,CASE WHEN Rno = 1 THEN AMOUNT ELSE NULL END AS AMOUNTFROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ID, CODE, NAME, AMOUNT ORDER BY ID, CODE, NAME, AMOUNT) AS RnoFROM @MyTable) AI want My final resultset to look like this :123 8214 Raaj KEVIN,GEORGE 200124 8000 Ram SHANE 100128 9000 VIJ BOND,STEVE 500AS you can see in the above result set when ID,CODE,NAME and AMOUNT have same values, I would like to show the CONTACTPERSON concatenated (seperated by comma) added to the previous which has the same values.Thanks,Raaj. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-12-03 : 21:08:53
|
[code]select ID, Code, Name, ContactPerson = STUFF((SELECT ',' + x.ContactPerson FROM @MyTable x WHERE x.ID = t.ID AND x.Code = t.Code AND x.Name = t.Name ORDER BY ',' + x.ContactPerson FOR XML PATH('')), 1, 1, ''), Amount = max(Amount)from @MyTable tgroup by ID, Code, Name[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2009-12-06 : 17:22:01
|
| Thanks Khtan,That worked perfect.Thanks,Raaj |
 |
|
|
|
|
|