| Author |
Topic |
|
lovray
Starting Member
9 Posts |
Posted - 2009-01-12 : 19:57:27
|
| I have a Table:myKey ProdNumber UPCnumber1 333 UPC_112 333 UPC_443 444 UPC_22One product can have multiple UPC's, I'm generating a product feed but I only need one UPC per productNumber, preferable the first UPC. For example I need a select statement which will give me this:1 333 UPC_113 444 UPC_22I can't figure this out for the life of me: I've tried:SELECT myKey, DISTINCT partNumber, UPCnumber FROM myTable;and It still won't work, no matter the order i place the column names. Please help. Thanks Much. -George. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-13 : 00:14:48
|
| [code]SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ProdNumber ORDER BY UPCnumber) AS Seq,*FROM Table)tWHERE Seq=1[/code] |
 |
|
|
lovray
Starting Member
9 Posts |
Posted - 2009-01-15 : 17:49:50
|
| Nice!! I wouldn't have been able to do it with out that code example! Thanks Much - George. |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-15 : 22:53:22
|
| declare @temp table (myKey int, ProdNumber int,UPCnumber varchar(32))insert into @tempselect 1, 333, 'UPC_11' union allselect 2, 333, 'UPC_44' union allselect 3, 444, 'UPC_22'select * from ( select *,rank() over ( partition by prodnumber order by mykey ) as rn from @temp ) as pwhere rn = 1 |
 |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-16 : 00:06:58
|
| Hi Dear,I hope the below query will help you to fix the issue.Query:SELECT MIN(MYKEY) ,PRODNUMBER ,MIN(UPCNUMBER)FROM DUMMY GROUP BY PRODNUMBER Change the Table Name and try. RegardsThiyagarajanwww.sqlhunt.blogspot.com |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2009-01-16 : 00:08:40
|
quote: Originally posted by thiyagu_rind Hi Dear,I hope the below query will help you to fix the issue.Query:SELECT MIN(MYKEY) ,PRODNUMBER ,MIN(UPCNUMBER)FROM DUMMY GROUP BY PRODNUMBER Change the Table Name and try. RegardsThiyagarajanwww.sqlhunt.blogspot.com
Do not use this solution. It is not guaranteed to be accurate. You could and probably will be pulling the MIN values from different rows.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 00:10:46
|
quote: Originally posted by Nageswar9 declare @temp table (myKey int, ProdNumber int,UPCnumber varchar(32))insert into @tempselect 1, 333, 'UPC_11' union allselect 2, 333, 'UPC_44' union allselect 3, 444, 'UPC_22'select * from ( select *,rank() over ( partition by prodnumber order by mykey ) as rn from @temp ) as pwhere rn = 1
will return all instances of first UPC in case value repeats. |
 |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-16 : 00:14:06
|
| Hi Man,Try the below queryQuery :SELECT MYKEY,PRODNUMBER,UPCNUMBERFROM DUMMY WHERE MYKEY IN (SELECT MIN(MYKEY) FROM DUMMYGROUP BY PRODNUMBER )I hope Now Tara Kizer will not raise a red flag.RegardsThiyagarajanwww.sqlhunt.blogspot.com |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-16 : 01:51:15
|
| Dear Tara keizer, The solution may be inefficient, but it is not a wrong solution. Can you pls explain, how it is wrong?RegardsThiyagarajan |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
dineshrajan_it
Posting Yak Master
217 Posts |
Posted - 2009-01-16 : 02:13:33
|
| The Above soln posted using Min() is not wrong as far iam concerned.But Row_number(),Rank() are preferable the best options and using min can be opted as least option. So, Min() is also an answer but less efficient. And he would have replied as just other alternative options for above post |
 |
|
|
ra.shinde
Posting Yak Master
103 Posts |
Posted - 2009-01-16 : 02:14:45
|
| Query:SELECT MIN(MYKEY),PRODNUMBER,MIN(UPCNUMBER)FROM DUMMY GROUP BY PRODNUMBERChange the Table Name and try. Above solution looks correct to me. I dont think it will match wrong rowsRahul Shinde |
 |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-16 : 02:16:47
|
| Dear Tara Keizer,I would like to know when it will return wrong rows. please use the statements and tell me when it returns wrong results.Queriescreate table dummy(myKey int, ProdNumber int,UPCnumber varchar(32))insert into dummyselect 1, 333, 'UPC_11' union allselect 2, 333, 'UPC_44' union allselect 3, 444, 'UPC_22' union allselect 4, 555, 'UPC_20' union allselect 5, 999, 'UPC_25' union allselect 6, 444, 'UPC_21' union allselect 7, 555, 'UPC_18' union allselect 8, 666, 'UPC_19'Code Snippet:select * from dummy order by prodnumber,mykeyGOSELECT MYKEY,PRODNUMBER,UPCNUMBERFROM DUMMY WHERE MYKEY IN (SELECT MIN(MYKEY) FROM DUMMYGROUP BY PRODNUMBER )Check the results and get back to me.RegardsThiyagarajan |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-01-16 : 03:28:29
|
| Dear Tara Kizer, Thank you.... Just to know where am wrong... Nothing RegardsThiyagarajanwww.sqlhunt.blogspot.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 09:20:16
|
quote: Originally posted by ra.shinde Query:SELECT MIN(MYKEY),PRODNUMBER,MIN(UPCNUMBER)FROM DUMMY GROUP BY PRODNUMBERChange the Table Name and try. Above solution looks correct to me. I dont think it will match wrong rowsRahul Shinde
its wrong because you cant guarantee MIN(MYKEY) & MIN(UPCNUMBER) occurs in same record. so it wont give OP's reqd output unless UPCnumber & myKey values go sequentially (either ascending or descending) for a value of ProdNumber |
 |
|
|
|