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 |
|
geoffsmiths
Starting Member
7 Posts |
Posted - 2010-05-26 : 10:45:33
|
| Hello SQL experts!I've got the following issue:ID | DATA_ID | URL | STORE_ID-----------------------------1 | 24 | bla | 102 | 11 | lol | 113 | 24 | Jak | 124 | 11 | iop | 5And This is what I want: I want for each data_id a the lowest store_id. I do need the ID, Data_id, URL and Store_ID.ID | DATA_ID | URL | STORE_ID-----------------------------1 | 24 | bla | 104 | 11 | iop | 5Once more I need your help! |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-26 : 11:00:29
|
| Try this:Declare @Data table(Id int,Data_id int,URL varchar(50),Store_id int)Insert into @DataSelect 1 , 24 , 'bla' , 10 unionSelect 2 , 11 , 'lol' , 11 unionSelect 3 , 24 , 'Jak' , 12 unionSelect 4 , 11 , 'iop' , 5Select Id, Data_id, url, Store_id from@Data D1 where store_id = (Select min(store_id) from @Data D2where D1.DATA_ID = D2.DATA_ID)order by id Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 11:01:34
|
select ID,DATA_ID,URL,STORE_IDfrom(select rownumber() over (partition by DATA_ID order by STORE_ID ASC) as rownum,* from Your_table)dtwhere rownum=1 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
geoffsmiths
Starting Member
7 Posts |
Posted - 2010-05-26 : 11:14:48
|
| &WEBFRED -> That worked great! Just simple and great SQL.Thank you again! |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 11:17:20
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|