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 |
Looper
Yak Posting Veteran
68 Posts |
Posted - 2010-10-12 : 08:50:40
|
I have a table with the following fieldsId, EmployeeId, RegIdi have two rows in the database for an employee.1, 1, 152, 1, 16I want to write a bit of sql so that I can extract like follows:EmployeeId, RegId1, RegId21, 15, 16RegId1 will always be the earliest IdCan anyone help |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-12 : 09:08:08
|
How many regid can a paticular employeeId have?PBUH |
 |
|
Looper
Yak Posting Veteran
68 Posts |
Posted - 2010-10-12 : 09:34:49
|
upto 2 RegIds per employeeId |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-12 : 12:06:37
|
[code]declare @tbl table (Id int, EmployeeId int, RegId int)insert @tblselect 1, 1, 15 unionselect 2, 1, 16 unionselect 2, 2, 16select * from @tblselect employeeid, isnull(max(case when rid=1 then regid end),0)regid1, isnull(max(case when rid=2 then regid end),0)regid2 from( select *,row_number()over(partition by employeeid order by id)rid from @tbl)Tgroup by EmployeeId[/code]PBUH |
 |
|
Looper
Yak Posting Veteran
68 Posts |
Posted - 2010-10-12 : 12:32:31
|
ThanksThis part where you have specified the data:select 1, 1, 15 unionselect 2, 1, 16 unionselect 2, 2, 16does not help if the table has over 1000 rows. Sorry I should have said that the two rows i provided was an example of how the table looks in general. But how can i extract all out of the table in the format I specified? |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-12 : 12:36:08
|
It's just some sample data.You need to run the query I posted against your table replacing it with your table name & column name in the query.PBUH |
 |
|
Looper
Yak Posting Veteran
68 Posts |
Posted - 2010-10-12 : 12:44:42
|
of course it is, silly me. it has been a long dayThanks |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-12 : 12:47:36
|
quote: Originally posted by Looper of course it is, silly me. it has been a long dayThanks
No probs PBUH |
 |
|
|
|
|
|
|