Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dewacorp.alliances
452 Posts |
Posted - 2005-01-28 : 20:00:50
|
| Hi thereI have a table Item like this:SupplierID INT,Periode INT,ProductName VARCHAR(255),ProductSize VARCHAR(255),PricePerKg (MONEY)Basically I have more than 10 supplier and want to pick the cheapest product per kg and know who the cheapest supplier is.Initially, I ran the following query:SELECT SupplierID, ProductName , MIN(PricePerKg)FROM ItemWHERE (PeriodID = 1) AND (PricePerKg > 0)GROUP BY ProductName And I got error: Column 'Item.SupplierID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.So I change this to this:SELECT SupplierID, ProductName, MIN(PricePerKg)FROM ItemWHERE (PeriodID = 1) AND (PricePerKg > 0)GROUP BY SupplierID, ProductName But it returns like these:SupplierID, ProductName, PricePerKg1 Alfafa 9.28572 Alfafa 8.93943 Alfafa 6.66671 Apricots 40-45mm 2.10002 Apricots 40-45mm 3.00003 Apricots 40-45mm 2.1500Is not really what I want!!!Can you guys help? Maybe you can point to the right direction?Thanks, VV |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-01-28 : 22:27:37
|
You need to do something like this:USE NorthwindGODROP TABLE ItemGOCREATE TABLE Item( PeriodID INT, SupplierID INT, ProductName VARCHAR(255), PricePerKg MONEY)GOINSERT Item(PeriodID, SupplierID, ProductName, PricePerKg) SELECT 1, 1, 'Alfafa', 9.2857 UNION ALL SELECT 1, 2, 'Alfafa', 8.9394 UNION ALL SELECT 1, 3, 'Alfafa', 6.6667 UNION ALL SELECT 1, 1, 'Apricots 40-45mm', 2.1000 UNION ALL SELECT 1, 2, 'Apricots 40-45mm', 3.0000 UNION ALL SELECT 1, 3, 'Apricots 40-45mm', 2.1500SELECT i1.SupplierID, i2.ProductName, i2.PricePerKgFROM Item i1 INNER JOIN ( SELECT ProductName, MIN(PricePerKg) AS PricePerKg FROM Item WHERE PeriodID = 1 AND PricePerKg > 0 GROUP BY ProductName) i2 ON i1.ProductName = i2.ProductName AND i1.PricePerKg = i2.PricePerKg MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
dewacorp.alliances
452 Posts |
Posted - 2005-01-29 : 02:10:27
|
| Thanks DerrickI've tried that one:SELECT i1.SupplierID, i2.ProductName, i2.PricePerKgFROM Item i1 INNER JOIN ( SELECT ProductName, MIN(PricePerKg) AS PricePerKg FROM Item WHERE PeriodID = 1 AND PricePerKg > 0 GROUP BY ProductName) i2 ON i1.SupplierID = i2.SupplierIDBut it returs: Invalid column name 'SupplierID'.Any other suggestions? |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-01-29 : 10:48:52
|
I edited the entry. The one I had up there definitely would have never worked. MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-01-31 : 06:33:02
|
| DerrickLegget, Is this simple?Select * from Item where PricePerKg in(Select Min(PricePerKg) from Item group by ProductName) Madhivanan |
 |
|
|
Lee-Z
Starting Member
7 Posts |
Posted - 2005-01-31 : 07:05:11
|
| I'm not quite sure what the result would be but this:> Select * from Item where PricePerKg in(> Select Min(PricePerKg) from Item group by ProductName) probably won't do it...the subquery would give you for instance:1.00 (product A)2.20 (product B)and the total query would show (for example-sake I assume that all products have the same prices)product A 1.00product A 2.20product B 2.20Your previous resultset was:SupplierID, ProductName, PricePerKg1 Alfafa 9.28572 Alfafa 8.93943 Alfafa 6.66671 Apricots 40-45mm 2.10002 Apricots 40-45mm 3.00003 Apricots 40-45mm 2.1500If what you want to see from this is3 Alfafa 6.66671 Apricots 40-45mm 2.1000Then you should do something like:select item.supplierid, item.productname, minPrice.pricekg from iteminner join (select productname, min(pricePerKg) as PriceKg from Item) MinPriceon item.productname = MinPrice.productnameThis:SELECT i1.SupplierID, i2.ProductName, i2.PricePerKgFROM Item i1INNER JOIN (SELECT ProductName, MIN(PricePerKg) AS PricePerKgFROM ItemWHERE PeriodID = 1 ANDPricePerKg > 0GROUP BY ProductName) i2 ON i1.SupplierID = i2.SupplierIDReturns: Invalid column name 'SupplierID', because SupplierID is not in i2...if this is what you want, you should add Supplierid to the subquery |
 |
|
|
dewacorp.alliances
452 Posts |
Posted - 2005-02-06 : 05:39:15
|
| Thank you for the input guys.VV |
 |
|
|
|
|
|
|
|