Author |
Topic |
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-07 : 14:24:58
|
Hi All,I have a requirement to select 3/n no of records and display it as a single record.for e.g - 3 fields table with 3 records-------aa 12 35bb 100 1cc 22 10this 3 records should be in this formataa 12 35 bb 100 1 cc 22 10Can anyone help me out to do it using T-Sql.Thanks in advance!!!RegardsAnand |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-08 : 02:24:03
|
something likeSELECT MAX(CASE WHEN seq=1 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=1 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=1 THEN field3 ELSE NULL END) AS field3,MAX(CASE WHEN seq=2 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=2 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=2 THEN field3 ELSE NULL END) AS field3,MAX(CASE WHEN seq=3 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=3 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=3 THEN field3 ELSE NULL END) AS field3FROM(SELECT ROW_NUMBER() OVER (PARTITION BY (rn-1)/3 ORDER BY rn) AS seq,(rn-1)/3 AS rowno,other columns...FROM(SELECT ROW_NUMBER() OVER ( ORDER BY field1) AS rn,*FROM Table)t)rGROUP BY (rn-1)/3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
anaxy
Starting Member
1 Post |
Posted - 2010-08-08 : 12:17:38
|
Use FULL JOIN for this requirement |
 |
|
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-08 : 15:43:13
|
Thanks for the post. Let me try!! |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-09 : 04:55:53
|
quote: Originally posted by anaxy Use FULL JOIN for this requirement
Why?MadhivananFailing to plan is Planning to fail |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-10 : 14:22:20
|
quote: Originally posted by anaxy Use FULL JOIN for this requirement
how?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-13 : 16:25:52
|
Hi visakh,I have tried your query, it throws an error message at GROUP BY clause. If I comment the Group by clause, it displays one the 1st row. Please adviceMsg 207, Level 16, State 1, Line 54Invalid column name 'rn'.RegardsAnand |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 01:26:30
|
quote: Originally posted by kka_anand Hi visakh,I have tried your query, it throws an error message at GROUP BY clause. If I comment the Group by clause, it displays one the 1st row. Please adviceMsg 207, Level 16, State 1, Line 54Invalid column name 'rn'.RegardsAnand
sorry it was a typoSELECT MAX(CASE WHEN seq=1 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=1 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=1 THEN field3 ELSE NULL END) AS field3,MAX(CASE WHEN seq=2 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=2 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=2 THEN field3 ELSE NULL END) AS field3,MAX(CASE WHEN seq=3 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=3 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=3 THEN field3 ELSE NULL END) AS field3FROM(SELECT ROW_NUMBER() OVER (PARTITION BY (rn-1)/3 ORDER BY rn) AS seq,(rn-1)/3 AS rowno,other columns...FROM(SELECT ROW_NUMBER() OVER ( ORDER BY field1) AS rn,*FROM Table)t)rGROUP BY (rn-1)/3 rowno ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-14 : 13:43:42
|
Thanks visakh,It's woking fine.As per the requirement, the records are partioned into 3 and displaying as a single record.I have another problem. If you see the below result, there are only 2 records exists for "123", because of that it is picking the "ABCDEF" as a 3 to partion causing the problem.To avoid this, I need to filter the condition to allow the record count that are equal to 3.1 0 ABC2 0 ABC3 0 ABC4 1 1235 1 1236 2 ABCDEF7 2 ABCDEF8 2 ABCDEF9 4 434If we add a filter then, it look like this (skips "123" record)1 0 ABC2 0 ABC3 0 ABC6 2 ABCDEF7 2 ABCDEF8 2 ABCDEF9 4 434...Now this is my requirement. How to add a filter condtion?Thanks in Advance....RegardsAnand |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 13:50:06
|
[code]SELECT MAX(CASE WHEN seq=1 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=1 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=1 THEN field3 ELSE NULL END) AS field3,MAX(CASE WHEN seq=2 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=2 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=2 THEN field3 ELSE NULL END) AS field3,MAX(CASE WHEN seq=3 THEN field1 ELSE NULL END) AS field1,MAX(CASE WHEN seq=3 THEN field2 ELSE NULL END) AS field2,MAX(CASE WHEN seq=3 THEN field3 ELSE NULL END) AS field3FROM(SELECT ROW_NUMBER() OVER (PARTITION BY (rn-1)/3 ORDER BY rn) AS seq,(rn-1)/3 AS rowno,other columns...FROM(SELECT ROW_NUMBER() OVER ( ORDER BY field1) AS rn,COUNT(1) OVER (PARTITION BY field3) AS Occur,*FROM Table)tWHERE Occur =3)rGROUP BY rowno[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-14 : 14:54:58
|
Thaks SSI have applied your comments, but still the same error. If you see the 'ASTRAL' value the rowno is collapsing (1 2 1 2 3).Occurs Seq rowno Date Name-------------------------------------------------------------------5 5 96 2010-07-26 00:00:00 ASTEC5 1 97 2010-07-30 00:00:00 ASTRAL5 2 97 2010-07-29 00:00:00 ASTRAL5 1 98 2010-07-28 00:00:00 ASTRAL5 2 98 2010-07-27 00:00:00 ASTRAL5 3 98 2010-07-26 00:00:00 ASTRAL5 4 98 2010-07-30 00:00:00 MICRO5 5 98 2010-07-29 00:00:00 MICROThanksAnand |
 |
|
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-14 : 15:57:29
|
Hi Visakh,I have used 5 as PARTITION and the result looks like this.If you see the 'ASTRAL' and 'MICRO' the rownos are collapsing (1 2 1 2 3).Occurs Seq rowno Date Name-------------------------------------------------------------------5 1 96 2010-07-30 00:00:00 ASTEC5 2 96 2010-07-29 00:00:00 ASTEC5 3 96 2010-07-28 00:00:00 ASTEC5 4 96 2010-07-27 00:00:00 ASTEC5 5 96 2010-07-26 00:00:00 ASTEC5 1 97 2010-07-30 00:00:00 ASTRAL5 2 97 2010-07-29 00:00:00 ASTRAL5 1 98 2010-07-28 00:00:00 ASTRAL5 2 98 2010-07-27 00:00:00 ASTRAL5 3 98 2010-07-26 00:00:00 ASTRAL5 4 98 2010-07-30 00:00:00 MICRO5 5 98 2010-07-29 00:00:00 MICRO5 1 99 2010-07-28 00:00:00 MICRO5 2 99 2010-07-27 00:00:00 MICRO5 3 99 2010-07-26 00:00:00 MICROFinal Result -- ASTEC is correct, others (ASTRAL & MICRO) are incorrect.============ 2010-07-30 ASTEC 2010-07-29 ASTEC 2010-07-28 ASTEC 2010-07-27 ASTEC 2010-07-26 ASTEC2010-07-30 ASTRAL 2010-07-29 ASTRAL NULL NULL NULL NULL NULL NULL2010-07-28 ASTRAL 2010-07-27 ASTRAL 2010-07-26 ASTRAL NULL NULL NULL NULLNULL NULL NULL NULL NULL NULL 2010-07-30 MICRO 2010-07-29 MICRO2010-07-29 MICROThanksAnand |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-15 : 03:14:52
|
may be this?SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY rowno) AS Seq, *FROM(SELECT (rn-1)/3 AS rowno,other columns...FROM(SELECT ROW_NUMBER() OVER ( ORDER BY field1) AS rn,COUNT(1) OVER (PARTITION BY field3) AS Occur,*FROM Table)t)r)p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kka_anand
Starting Member
24 Posts |
Posted - 2010-08-15 : 06:41:45
|
Hi Visakh,It Works fine. You are Great.Thanks a lot for your valuable time and support.RegardsAnand |
 |
|
|