Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

MAX Function Problem Expand / Collapse
Author
Message
Posted Tuesday, October 30, 2012 12:38 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
Points: 14, Visits: 44
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.
Post #1378585
Posted Tuesday, October 30, 2012 1:23 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:09 AM
Points: 878, Visits: 2,395
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
Post #1378589
Posted Tuesday, October 30, 2012 1:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
Points: 14, Visits: 44
What is DDL and DML?
Post #1378593
Posted Tuesday, October 30, 2012 1:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
Points: 14, Visits: 44
Current Data:

RecordID ItemID MaxOfCost
43392 3880 $50.00
39056 3881 $93.75
33941 3881 $97.50
33970 3881 $97.50
38950 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


Results should be:
RecordID ItemID MaxOfCost
43392 3880 $50.00
38950 3881 $98.75
36491 3882 $30.00
37265 3883 $6.50
36585 3884 $18.77

I require the RecordID to use in another query.



Post #1378596
Posted Tuesday, October 30, 2012 2:02 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:09 AM
Points: 878, Visits: 2,395
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
Post #1378601
Posted Tuesday, October 30, 2012 2:10 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:25 AM
Points: 3,648, Visits: 5,328
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1378602
Posted Tuesday, October 30, 2012 2:11 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:09 AM
Points: 878, Visits: 2,395
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

36585 3884 18.77
37265 3883 6.50
36491 3882 30.00
38950 3881 98.75
99999 3881 98.75
43392 3880 50.00

I dont know if thats what you want.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1378603
Posted Tuesday, October 30, 2012 2:16 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 7:09 AM
Points: 878, Visits: 2,395
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
Post #1378604
Posted Tuesday, October 30, 2012 2:19 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:25 AM
Points: 3,648, Visits: 5,328
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1378608
Posted Tuesday, October 30, 2012 2:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 27, 2013 6:37 AM
Points: 14, Visits: 44
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

36585 3884 18.77
37265 3883 6.50
36491 3882 30.00
38950 3881 98.75
99999 3881 98.75
43392 3880 50.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.
Post #1378615
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse