January 11, 2010 at 11:56 am
hi friends
seems a book table with columns : Id,Title,Subject,Price
i want to get the most expensive books in each Subject.how can i do this ?
i tried this :
[Code]
SelectTitle,Subject,Max(Price)
FROMBook
GROUP BYSubject
[/Code]
but it raise error that "Column 'Title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
what can i do ?
Thanks
January 11, 2010 at 11:58 am
This seriously looks like homework. Is it?
The way I'd do this is build a CTE that uses the Rank() function to rank the titles by price within each subject, then the outer query would filter by the rank column of the CTE.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 11, 2010 at 12:02 pm
GSquared (1/11/2010)
This seriously looks like homework. Is it?The way I'd do this is build a CTE that uses the Rank() function to rank the titles by price within each subject, then the outer query would filter by the rank column of the CTE.
its not home work actually, it emergency now cause my friend ask me.
i think about it but i raise error .. hum , could u please tell me more what do u mean ?
January 11, 2010 at 12:27 pm
hooRRRa ,i do like this :
SELECT * FROM Book Inner JOIN
(
SelectSubject,Max(Price) as Price
FROMBook
GROUP BYSubject
)T
On T.Subject=Book.Subject AND T.Price=Book.Price
but please guide me if there's any better solution :O)
January 11, 2010 at 4:06 pm
Normal aggregate functions (SUM(), AVG, () COUNT(), MIN(), MAX(), etc.) are available to be used as windowed functions (using OVER and PARTITION BY).
SELECT Title,Subject, Max(Price) OVER(PARTITION BY Subject) As MaxPriceBySubject
FROM Book
-Eddie
Eddie Wuerch
MCM: SQL
January 11, 2010 at 9:45 pm
Eddie Wuerch (1/11/2010)
Normal aggregate functions (SUM(), AVG, () COUNT(), MIN(), MAX(), etc.) are available to be used as windowed functions (using OVER and PARTITION BY).
SELECT Title,Subject, Max(Price) OVER(PARTITION BY Subject) As MaxPriceBySubject
FROM Book
-Eddie
i do this exactlly, but this return all rows :O(
how should i change your code for geting most expencive book in per subject ?
January 12, 2010 at 1:09 am
The query that Gsquared has suggested, (untested)
;WITH CTE (Title, Subject, row_no) as
( SELECT Title,Subject, Row_number() OVER(PARTITION BY Subject
Order by price desc) as row_num
FROM Book)
Select * from CTE
where row_num = 1
---------------------------------------------------------------------------------
December 9, 2015 at 10:30 am
Sorry to be a troll, but....
It is emergency because my friend asked me...
NO...come on.
December 9, 2015 at 10:34 am
taseedorf (12/9/2015)
Sorry to be a troll, but....It is emergency because my friend asked me...
NO...come on.
Please note....this thread is 5 years old.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
December 9, 2015 at 6:46 pm
Sean Lange (12/9/2015)
taseedorf (12/9/2015)
Sorry to be a troll, but....It is emergency because my friend asked me...
NO...come on.
Please note....this thread is 5 years old.
Heh... I've never seen a 5 year old troll before. They're a lot shorter than what I expected. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply