开发者

SQL statement help!

I need help in sql statement. Now my database I have Category table(book, flower, stationary). Inside book table(bookIDm,bookTypes, size, price, c开发者_开发技巧atID)

For eg, 1st field -1(bookID), Diary(bookTypes), Big(size), $12.50(price), 1(catID) 2nd field -2(bookID), Diary(bookTypes), Big(size), $11.00(price), 1(catID) .

Now I just wanted it to display like ---- Diary | Small | $11.00---Means only display one data for each bookType with the lowest price. How do I write the sql statement for this requirement?


For Microsoft SQL Server you could use

SELECT     BookType, Size, MIN(Price) AS LowestPrice
FROM         dbo.Books
GROUP BY BookType, Size

The main things you are looking to learn are the use of agregate functions. I suggest you check out Aggregate Functions (Transact-SQL) .I used the Min function here to get the minimum price. To do this you must use the "Group By" for the fields that are not agregated.


select BookType, Size, min(Price)
from Book
group by BookType, Size
order by BookType
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜