Solved

SQL Question

Posted on 2013-01-13
4
258 Views
Last Modified: 2013-01-14
I need to Display the part id, description, and price for the highest and lowest priced parts in the  inventory.   When I run the query below it gives me all the rcords in the inventory table.  

How should modify it ?  Thanks

SELECT partid,description,
        MAX(price) AS highPrice,
        MIN(price) AS lowPrice
FROM INVENTORY
GROUP BY partid,description
0
Comment
Question by:ocdc
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38773227
It is possible that you could have multiple stock items with the same low price and same high price.

Something like this might get you started.

select a.partid, a.description, x.minprice, b.partid, b.description, x.maxprice
from inventory a
join 
(select min(price) as minprice, max(price) as maxprice 
 from inventory) as x on a.price = x.minprice
join inventory b on b.price = x.maxprice

Open in new window


In the event that you have one unique minprice and one unique maxprice you'll get one simple row.

In the event that you have more than one of each, you're going to get effectively a cartesian join, showing you every permutation of the values of min and max.
0
 

Author Comment

by:ocdc
ID: 38773240
just did this: when I look at the INVENTORY.Price column in INVENTORY table,   there are two items one with low 2.50  the other one is high 80.00.  The query below gives me the mentioned columns.   Any feedback ?

SELECT i.Partid
      ,i.Description
      ,i.Price    
FROM INVENTORY i  
WHERE  i.Price IN
(SELECT MAX(Price)
 FROM INVENTORY)
GROUP BY i.PartID
        ,Description
        ,i.Price
UNION ALL
SELECT i.PartID
      ,Description
      ,i.Price
FROM INVENTORY i  
WHERE  i.Price IN
(SELECT MIN(Price)
FROM INVENTORY)
GROUP BY i.PartID,i.Description,i.Price
0
 

Author Comment

by:ocdc
ID: 38775035
I've requested that this question be deleted for the following reason:

no need anymore
0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38773366
That query will do the job and give you as many rows in your result set as there are Mins and Maxes (and will look prettier than mine).

Mine gave it all on one row (with duplicates), yours gives multiple rows in the result set - so whatever works.

Would probably change the IN to = though.

WHERE  i.Price =
(SELECT MIN(Price)
FROM INVENTORY)

Do the same for Max.

In is usually for when you don't know how many rows will be in your result set.  Since you're using Min and Max you'll have exactly one Min and exactly one Max - so use equals.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question