| Author |
Topic |
|
Neal44
Starting Member
17 Posts |
Posted - 2010-04-12 : 05:50:50
|
Hi,I have a temporary table that contains 2 fields (Name and Pos) that I need to group by. The 'Pos' values may change everytime I INSERT into this table:Table1Name PosJohn 11John 12John 18John 19John 25John 26John 32John 33Emma 11Emma 12Emma 18Emma 19Emma 25Emma 26Emma 32Emma 33Paul 11Paul 12Paul 18Paul 19Paul 25Paul 26Paul 32Paul 33 I need to perform 2 tasks on these fields. Firstly, I need add an indicator (Ind) on the 'Name' field for each occurence (where 1 = odd # AND 2 = even #). Then I need to group by each Name/Pos/Ind pairing. (The Pos values could change everytime I import a file)Name Pos Ind GroupJohn 11 1 1John 12 2 1John 18 1 2John 19 2 2John 25 1 3John 26 2 3John 32 1 4John 33 2 4Emma 11 1 1Emma 12 2 1Emma 18 1 2Emma 19 2 2Emma 25 1 3Emma 26 2 3Emma 32 1 4Emma 33 2 4Paul 11 1 1Paul 12 2 1Paul 18 1 2Paul 19 2 2Paul 25 1 3Paul 26 2 3Paul 32 1 4Paul 33 2 4 Any ideas please?Thanks,NNeal |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-12 : 06:08:52
|
[code]; with cte as( select *, row_no = row_number() over (partition by Name order by Pos) from Table1)select Name, Pos, Ind = case when row_no % 2 = 1 then 1 else 2 end, [Group] = (row_no - 1) / 2 + 1from cteorder by Name, Pos[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-12 : 06:14:40
|
| [code]SELECT Name,Pos,CASE WHEN Seq%2=0 THEN 2 ELSE 1 END AS Ind,((Seq-1)/2) + 1 AS [Group]FROM(SELECT Name,Pos,ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Pos) AS SeqFROM Table)tOrder By Name,Pos[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-12 : 06:15:32
|
Or select name,pos,row_number()over(partition by name,rowid order by name,rowid) as Ind,dense_rank()over (order by name,rowid)as [Group] from(select *,pos-row_number()over(order by name)as rowid from yourtable)t PBUH |
 |
|
|
Neal44
Starting Member
17 Posts |
Posted - 2010-04-12 : 06:22:13
|
| WOW!Thanks guys, that was amazingly prompt.Neal |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-12 : 06:27:36
|
please verify and let me know if anything else you want...SELECT Name, Pos, Ind, ROW_NUMBER() OVER(Partition by Name, Ind ORDER BY Name, Pos) [Group]FROM ( SELECT Name, Pos, CASE WHEN Rn%2 <> 0 THEN 1 ELSE 2 END Ind FROM ( SELECT Name, Pos, ROW_NUMBER() OVER(PARTITION BY Name ORDER BY (select 1) ) Rn FROM table1 ) a ) aORDER BY Name, Pos Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
Neal44
Starting Member
17 Posts |
Posted - 2010-04-12 : 08:01:51
|
I now need to insert my updated table into another SQL table.Table1 is now:Cust_Id Name Pos Ind Group Val1234 John 11 1 1 231234 John 12 2 1 231234 John 18 1 2 01234 John 19 2 2 2.41234 John 25 1 3 12.61234 John 26 2 3 23.451234 John 32 1 4 21.11234 John 33 2 4 14.51234 Emma 11 1 1 15.61234 Emma 12 2 1 15.321234 Emma 18 1 2 12.41234 Emma 19 2 2 11.61234 Emma 25 1 3 18.31234 Emma 26 2 3 14.41234 Emma 32 1 4 10.31234 Emma 33 2 4 17.41234 Paul 11 1 1 19.21234 Paul 12 2 1 17.31234 Paul 18 1 2 11.341234 Paul 19 2 2 10.31234 Paul 25 1 3 19.21234 Paul 26 2 3 19.71234 Paul 32 1 4 12.41234 Paul 33 2 4 14.5 I need to transfer the 'Val' data into specific columns based on the Name/Pos/Ind/Group grouping (the Cust_Id should always be the same).I must insert into Table2 as:Cust_Id John_1 John_2 Emma_1 Emma_2 Paul_1 Paul_21234 23 23 15.6 15.32 19.2 17.31234 0 2.4 12.4 11.6 11.34 10.31234 12.6 23.45 18.3 14.4 19.2 19.71234 21.1 14.5 10.3 17.4 12.4 14.5 For Example:A new row is inserted for each Group # count (i.e. 4 rows as this is MAX(Group))John_1 is populated with data from Table1 where Ind = 1John_2 is populated with data from Table1 where Ind = 2I hope I have given you enough information.Thanks,NNeal |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-12 : 08:10:58
|
| [code]SELECT Group,MAX(CASE WHEN Name='John' AND Ind=1 THEN Val ELSE NULL END) AS John_1,MAX(CASE WHEN Name='John' AND Ind=2 THEN Val ELSE NULL END) AS John_2,MAX(CASE WHEN Name=Emma' AND Ind=1 THEN Val ELSE NULL END) AS Emma_1,MAX(CASE WHEN Name='Emma' AND Ind=2 THEN Val ELSE NULL END) AS Emma_2,MAX(CASE WHEN Name='Paul' AND Ind=1 THEN Val ELSE NULL END) AS Paul_1,MAX(CASE WHEN Name='Paul' AND Ind=2 THEN Val ELSE NULL END) AS Paul_2FROM TableGROUP BY Group[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Neal44
Starting Member
17 Posts |
Posted - 2010-04-13 : 04:03:54
|
| Another success. Thank YOU VERY much.(and to everyone else who contributed).This is a excellent site.Neal |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-13 : 06:56:37
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|