October 30, 2012 at 12:38 am
I need to query a table that has three columns 1st RecordID (Primary Key, Unique), 2nd ItemID (non-unique) and 3rd Cost(currency). I need to return RecordID, ItemID( Group by) and Cost (Max) only one record per ItemID.
Hope some one can help.
October 30, 2012 at 1:23 am
Can you supply the DDL and DML as well as sample data and expected results as this will help us understand the data and what results you want.
Is there a reason why you need to include RecordId in the output?
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 30, 2012 at 1:28 am
What is DDL and DML?
October 30, 2012 at 1:32 am
Current Data:
RecordIDItemIDMaxOfCost
433923880 $50.00
390563881 $93.75
339413881 $97.50
339703881 $97.50
389503881 $98.75
404173881 $96.56
364913882 $30.00
372653883 $6.50
350543883 $4.50
336583884 $14.94
365853884 $18.77
331343884 $14.75
Results should be:
RecordIDItemIDMaxOfCost
433923880 $50.00
389503881 $98.75
364913882 $30.00
372653883 $6.50
365853884 $18.77
I require the RecordID to use in another query.
October 30, 2012 at 2:02 am
Hi Cory,
DDL is Data Definition language, eg Table defs etc
DML is Data Manipulation language, eg the Select/Insert statements
I see you posted some sample data so I'll have a look at it.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 30, 2012 at 2:10 am
In the future, you should provide DDL and consumable sample data in the format I've included below.
-- DDL
DECLARE @T TABLE
(RecordID INT, ItemID INT, MaxOfCost MONEY)
-- Consumable sample data
INSERT INTO @T
SELECT 43392,3880,$50.00
UNION ALL SELECT 39056,3881,$93.75
UNION ALL SELECT 33941,3881,$97.50
UNION ALL SELECT 33970,3881,$97.50
UNION ALL SELECT 38950,3881,$98.75
UNION ALL SELECT 40417,3881,$96.56
UNION ALL SELECT 36491,3882,$30.00
UNION ALL SELECT 37265,3883,$6.50
UNION ALL SELECT 35054,3883,$4.50
UNION ALL SELECT 33658,3884,$14.94
UNION ALL SELECT 36585,3884,$18.77
UNION ALL SELECT 33134,3884,$14.75
;WITH MyData AS (
SELECT RecordID, ItemID, MaxOfCost
,n=ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY MaxOfCost DESC)
FROM @T)
SELECT RecordID, ItemID, MaxOfCost
FROM MyData
WHERE n = 1
Hopefully this provides the results you seek but let us know.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 30, 2012 at 2:11 am
heres a quick and dirty way
CREATE Table #results
(
RecordId Int
,ItemId Int
,cost Decimal(38,2)
)
Insert into #results
values (43392, 3880 ,50.00)
,(39056, 3881, 93.75)
,(33941, 3881, 97.50)
,(33970, 3881, 97.50)
,(38950, 3881, 98.75)
,(99999, 3881, 98.75)
,(40417, 3881, 96.56)
,(36491, 3882, 30.00)
,(37265, 3883, 6.50)
,(35054, 3883 ,4.50)
,(33658, 3884, 14.94)
,(36585, 3884,18.77)
,(33134, 3884, 14.75)
Select r.RecordId,r.ItemId,r.cost
From #results r
JOIN (Select ItemId,MAX(cost) mcost
from #results
group by ItemId) m
on m.ItemId=r.ItemId
and mcost=r.cost
This assumes that the Max(Cost) per Item Id is unique.
Eg if you added a row 9999,3881,98.75 and ran this you would get two rows in the output
36585388418.77
3726538836.50
36491388230.00
38950388198.75
99999388198.75
43392388050.00
I dont know if thats what you want.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 30, 2012 at 2:16 am
dwain.c (10/30/2012)
In the future, you should provide DDL and consumable sample data in the format I've included below.
-- DDL
DECLARE @T TABLE
(RecordID INT, ItemID INT, MaxOfCost MONEY)
-- Consumable sample data
INSERT INTO @T
SELECT 43392,3880,$50.00
UNION ALL SELECT 39056,3881,$93.75
UNION ALL SELECT 33941,3881,$97.50
UNION ALL SELECT 33970,3881,$97.50
UNION ALL SELECT 38950,3881,$98.75
UNION ALL SELECT 40417,3881,$96.56
UNION ALL SELECT 36491,3882,$30.00
UNION ALL SELECT 37265,3883,$6.50
UNION ALL SELECT 35054,3883,$4.50
UNION ALL SELECT 33658,3884,$14.94
UNION ALL SELECT 36585,3884,$18.77
UNION ALL SELECT 33134,3884,$14.75
;WITH MyData AS (
SELECT RecordID, ItemID, MaxOfCost
,n=ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY MaxOfCost DESC)
FROM @T)
SELECT RecordID, ItemID, MaxOfCost
FROM MyData
WHERE n = 1
Hopefully this provides the results you seek but let us know.
Thats was quick dwain, and a nice solution though it will suppress any rows that have the same MAX(cost) as another row.
I suppose the question outstanding is what if there are two rows with the same ItemId and Cost, do you want all rows returned or just one, if just one which one takes takes precedence?
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 30, 2012 at 2:19 am
Jason-299789 (10/30/2012)
dwain.c (10/30/2012)
In the future, you should provide DDL and consumable sample data in the format I've included below.
-- DDL
DECLARE @T TABLE
(RecordID INT, ItemID INT, MaxOfCost MONEY)
-- Consumable sample data
INSERT INTO @T
SELECT 43392,3880,$50.00
UNION ALL SELECT 39056,3881,$93.75
UNION ALL SELECT 33941,3881,$97.50
UNION ALL SELECT 33970,3881,$97.50
UNION ALL SELECT 38950,3881,$98.75
UNION ALL SELECT 40417,3881,$96.56
UNION ALL SELECT 36491,3882,$30.00
UNION ALL SELECT 37265,3883,$6.50
UNION ALL SELECT 35054,3883,$4.50
UNION ALL SELECT 33658,3884,$14.94
UNION ALL SELECT 36585,3884,$18.77
UNION ALL SELECT 33134,3884,$14.75
;WITH MyData AS (
SELECT RecordID, ItemID, MaxOfCost
,n=ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY MaxOfCost DESC)
FROM @T)
SELECT RecordID, ItemID, MaxOfCost
FROM MyData
WHERE n = 1
Hopefully this provides the results you seek but let us know.
Thats was quick dwain, and a nice solution though it will suppress any rows that have the same MAX(cost) as another row.
I suppose the question outstanding is what if there are two rows with the same ItemId and Cost, do you want all rows returned or just one, if just one which one takes takes precedence?
True enough. Although you could use RANK() instead of ROW_NUMBER() to resolve that quandary.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 30, 2012 at 2:52 am
Jason-299789 (10/30/2012)
heres a quick and dirty way
CREATE Table #results
(
RecordId Int
,ItemId Int
,cost Decimal(38,2)
)
Insert into #results
values (43392, 3880 ,50.00)
,(39056, 3881, 93.75)
,(33941, 3881, 97.50)
,(33970, 3881, 97.50)
,(38950, 3881, 98.75)
,(99999, 3881, 98.75)
,(40417, 3881, 96.56)
,(36491, 3882, 30.00)
,(37265, 3883, 6.50)
,(35054, 3883 ,4.50)
,(33658, 3884, 14.94)
,(36585, 3884,18.77)
,(33134, 3884, 14.75)
Select r.RecordId,r.ItemId,r.cost
From #results r
JOIN (Select ItemId,MAX(cost) mcost
from #results
group by ItemId) m
on m.ItemId=r.ItemId
and mcost=r.cost
This assumes that the Max(Cost) per Item Id is unique.
Eg if you added a row 9999,3881,98.75 and ran this you would get two rows in the output
36585388418.77
3726538836.50
36491388230.00
38950388198.75
99999388198.75
43392388050.00
I dont know if thats what you want.
Sorry no i need to return only one result per ItemID. No preference on which one just need one result. I now see why you are after the DDL and DML.
October 30, 2012 at 3:31 am
No problem cory,
I'd recommend Dwains solution which uses the ROW_NUMBER() OVER
_________________________________________________________________________
SSC Guide to Posting and Best Practices
October 30, 2012 at 3:46 am
I have got it working now. Thanks to all for there help. :-):-)
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply