tohru
tohru

Reputation: 45

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, catID)

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?

Upvotes: 0

Views: 111

Answers (2)

Bentley Davis
Bentley Davis

Reputation: 716

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.

Upvotes: 3

Alex
Alex

Reputation: 2420

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

Upvotes: 1

Related Questions