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 |
stpn
Starting Member
8 Posts |
Posted - 2014-03-26 : 06:50:57
|
Hi,I have 2 tables "PRODUCTS" and "CATEGORIES".Table: ProductsProID ProName1 A2 B3 C4 D5 ETable : CategoriesProID CatID CatName1 1 AA1 2 BB1 3 CC1 4 DD2 3 EE2 4 FF3 1 GG4 1 HH4 3 II4 4 JJ5 1 KK5 2 LLI want to write a query, which returns only the starting 2 categories, when productID is Passed as a parameter.If ProID=1Output ExampleProID CatID CatName1 1 AA1 2 BBIf ProID=4Output ExampleProID CatID CatName4 1 HH4 3 III tired with the below query, but i know it gives only the details of CatID 1 and 2, Where as all the products does not have CatID 1 and 2 so it will not work out for me.Create Proc SP_CatDetails@ProID intasBeginSelect P.ProID,C.CatID,C.CatName from Products PINNER JOIN Categories CON P.ProID=C.ProIDWhere P.ProID=@ProIDand C.CatID Between 1 and 2order by C.CatIDEndThanks in advance for your assistance |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2014-03-26 : 07:57:58
|
try this:Select P.ProID,dt.CatID,dt.CatName from Products PINNER JOIN (select row_number() over (partion by ProID oder by CatID) as rn, * from Categories) as dt ON P.ProID=dt.ProID and dt.rn < 3Where P.ProID=@ProID Too old to Rock'n'Roll too young to die. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|