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
精彩评论