I have a SQL challenge which I need a little help with.
Below is a simplified example, in my real case I have about 500k rows in a slow VIEW. So if you have a solution that is effective as well, I would appreciate it. I’m thinking I have to use GROUP BY in one way or another, but I’m not sure.
Let’s say I have a table like this
╔═════════╦══════════╦══════════╦═══════╗
║ ORDERID ║ NAME ║ TYPE ║ PRICE ║
╠═════════╬══════════╬══════════╬═══════╣
║ 1 ║ Broccoli ║ Food ║ 1 ║
║ 1 ║ Beer ║ Beverage ║ 5 ║
║ 1 ║ Coke ║ Beverage ║ 2 ║
║ 2 ║ Beef ║ Food ║ 2.5 ║
║ 2 ║ Juice ║ Beverage ║ 1.5 ║
║ 3 ║ Beer ║ Beverage ║ 5 ║
║ 4 ║ Tomato ║ Food ║ 1 ║
║ 4 ║ Apple ║ Food ║ 1 ║
║ 4 ║ Broccoli ║ Food ║ 1 ║
╚═════════╩══════════╩══════════╩═══════╝
So what I want to do is:
In each order, where there are BOTH food and beverage order line, I want the highest beverage price
So in this example i would like to have a result set of this:
╔═════════╦═══════╦═══════╗
║ ORDERID ║ NAME ║ PRICE ║
╠═════════╬═══════╬═══════╣
║ 1 ║ Beer ║ 5 ║
║ 2 ║ Juice ║ 1.5 ║
╚═════════╩═══════╩═══════╝
How can I acheive this in an effective way?
Since you have tagged
SQL Server, make use ofCommon Table ExpressionandWindow Functions.