Reply to Re: Top N rows, discard duplicate

Your name:

Reply:


Posted by Erland Sommarskog on 10/01/37 11:19

(medhanush@yahoo.com) writes:
> Can sb pl help to what t-sql query to use to achieve as follows,
> for some reason, there are duplicate rows, but i need to get
> top most rows of category A, B as follows.
>
> Num Category ProductName
> ---- -------- -----------
> 100 A Product1
> 100 B Product1
>
> 101 A Product2
> 101 B Product2
>
> 102 A Product3
> 102 B Product3

There is no such thing as a "top-most" row in a table. A table is an
orderd set.

It seems that with the sample data you have given that this would do:

SELECT DISTINCT Num, Category, ProductName
FROM tbl
WHERE Category IN ('A', 'B')

If product names are different, you can do:

SELECT Num, Category, MAX(ProductName)
FROM tbl
WHERE Category IN ('A', 'B')
GROUP BY Num, Category




--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация