| Author |
Topic |
|
1seoadvies
Starting Member
2 Posts |
Posted - 2011-03-18 : 10:49:58
|
| I have tmp table with an id, (int) userid, (guid), photoid (int)row 1: 4, 000, 1row 2: 4, 000, 2row 3: 8, 111, nullrow 4: 9, 222, nullrow 5: 10, 333, 1row 6: 10, 333, 2row 7: 12, 555, nullhow do I get only the row(s) with the min(photoid) and also the row(s) with the null value?I only want to see row 1,3,4,5 and 7yes, I know about the uniqueidentifier ;-)Any ideas??http://www.1seoadvies.nl |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-03-18 : 11:06:36
|
| DECLARE @TABLE TABLE(RowId int identity(1,1),ID int,UserID Char(3),PhotoID int)INSERT INTO @TABLESELECT 4, '000', 1 UNION SELECT 4, '000', 2 UNION SELECT 8, '111', null UNION SELECT 9, '222', null UNION SELECT 10, '333', 1 UNION SELECT 10, '333', 2 UNION SELECT 12, '555', null SELECT t1.RowId,t1.ID,t1.UserID,t2.photoIDFROM @TABLE t1INNER JOIN(select t.ID,t.UserID ,MIN(ISNULL(PhotoID,0)) as PhotoIDfrom @TABLE tGROUP BY t.ID,t.UserID) t2on t1.ID = t2.IDand isnull(t1.PhotoID,0) = t2.photoidJimEveryday I learn something that somebody else already knew |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-03-18 : 11:13:47
|
| SELECT t1.RowId,t1.ID,t1.UserID,t1.IDFROM(SELECT RowId ,ID ,UserID ,PhotoID ,[rown] =ROW_NUMBER() over(partition by id,userid order by isnull(t1.photoid,0))FROM @TABLE t1)t1WHERE t1.rown =1jimEveryday I learn something that somebody else already knew |
 |
|
|
1seoadvies
Starting Member
2 Posts |
Posted - 2011-03-18 : 12:41:33
|
| Thanks JimThis helpshttp://www.1seoadvies.nl |
 |
|
|
|
|
|