473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Select statement

Hello guys,

I have table "Shopping" that has 3 columns (Item, Category, Price)
Is there a straight forward way in T-SQL to select the Item that has
the highest price for each category as below:

Item Category Price
-------- ----------- ----------
1 2 13.99
2 2 20.69
3 2 7.00
4 3 16.00
5 3 17.00

The select statement I'm after should return Item: 2 and 5

Thanks for your help,

Maya.

Feb 21 '06 #1
6 1520
SELECT MAX(Price), Category
FROM table
GROUP BY Category
Just off the top of my head, haven't tested and I'm nothing special
with sql.

Feb 21 '06 #2
Maya wrote:
I have table "Shopping" that has 3 columns (Item, Category, Price)
Is there a straight forward way in T-SQL to select the Item that has
the highest price for each category as below:


<snip>

While I'm sure you *will* get an answer here at some point (and I'll
think about it myself and try to answer soon) your question would
really be more suitable in a SQL group.

Jon

Feb 21 '06 #3
Hi Maya,
I have table "Shopping" that has 3 columns (Item, Category, Price)
Is there a straight forward way in T-SQL to select the Item that has
the highest price for each category as below:
The select statement I'm after should return Item: 2 and 5


For example sth. like this:
"select top 2 * From dbo.Item order by price desc"
but this belongs to a SQL NG.
ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP/MVP C#]
http://Dzaebel.NET
Feb 21 '06 #4
Frank Dzaebel wrote:
I have table "Shopping" that has 3 columns (Item, Category, Price)
Is there a straight forward way in T-SQL to select the Item that has
the highest price for each category as below:
The select statement I'm after should return Item: 2 and 5
For example sth. like this:
"select top 2 * From dbo.Item order by price desc"


No, because that doesn't distinguish by category. If the top two prices
are both for the same category, it'll just give those two.
but this belongs to a SQL NG.


Agreed.

Jon

Feb 21 '06 #5
SELECT category, Max(price) AS HighestPrice, MAX(item) AS Item_ID
FROM tbl
GROUP BY category
If you want Item ID included...

Feb 21 '06 #6
Sorry about the wrong newsgroup, have posted here by mistake as I'm
working on c'# as the same time as well.

Anyway, got the answer from the SQL news group.

Apologies again...

Maya

Feb 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
4
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
6
by: Bob Stearns | last post by:
I am getting duplicate rows back from a select distinct statement of the form: SELECT DISTINCT 'jhough', '000111', t0.bhid FROM (SELECT lots of good stuff) t0 LEFT OUTER JOIN another_table ...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
0
FishVal
by: FishVal | last post by:
Hereby I'm proposing a way of convinient work with properties containing SQL Select statements, particulary RowSource property of ComboBox and ListBox. The usual way is the following. Private...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.