Avatar of Aravind Ranganathan
Aravind Ranganathan
 asked on

Group By Giving me the Wrong Data

I have a sql query that groups by upcitem number and must return back the record with the highest value but in my example it is returning the lowest value back.

SELECT     [Unit UPC Base Item], [Item Description]
FROM         (SELECT     MIN([Item Description]) AS [Item Description], [Unit UPC Base Item]
                       FROM          dbo.ItemDescLookups
                       GROUP BY [Unit UPC Base Item]) AS Sub

Open in new window


Table result i want the value with  the 10 returned.
SQL

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Pawan Kumar

Pls try this -

use MAX. -  You need to use the MAX function to get the Maximum value from a COLUMN


SELECT     [Unit UPC Base Item], [Item Description]
FROM         (SELECT     MAX([Item Description]) AS [Item Description], [Unit UPC Base Item]
                       FROM          dbo.ItemDescLookups
                       GROUP BY [Unit UPC Base Item]) AS Sub

Open in new window


Another option is you can use a ROW_NUMBER function.
Daryl Bamforth

Group by doesn't change the order.

Add

order by [Unit UPC Base Item]  desc

Open in new window

Aravind Ranganathan

ASKER
@Pawan Kumar that is giving me the 6th record which has a countofdescription = 1
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Pawan Kumar

From where you are getting CountofDescription column.
Daryl Bamforth

So your wanting it to return the one with 10?
Aravind Ranganathan

ASKER
@ Pawan the count of description is part of the table, i am not running any Count to get that.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Aravind Ranganathan

ASKER
@ Daryl Banfort yes i want it to return the one with 10
Pawan Kumar

Pls try this

SELECT MIN([Item Description]) AS [Item Description], [Unit UPC Base Item]
	 , MAX(CountofDescription) CountofDescription 
FROM  dbo.ItemDescLookups
GROUP BY [Unit UPC Base Item] 

Open in new window

Aravind Ranganathan

ASKER
@Pawan Kumar that did not work either buddy :(
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Pawan Kumar

can you send some of your table data and expected output.
Daryl Bamforth

Try

SELECT     [Unit UPC Base Item], [Item Description]
FROM         (SELECT     Top 1([Item Description]) AS [Item Description], [Unit UPC Base Item]
                       FROM          dbo.ItemDescLookups
                       GROUP BY [Unit UPC Base Item]) 
                       ORDER BY CountOfDescription DESC LIMIT 1
                     AS Sub

Open in new window

Aravind Ranganathan

ASKER
@ Pawan
so this is the data from the itemdesclookups:

Unit UPC Base Item      Item Description      CountOfDescription
205                                            Something      10
205                                            Something1      6
205                                            Something2      4
205                                       Something3      2
205                                            Something4      2
205                                            Something5      1
205                                            Something6      1
205                                            Something7      1
205                                            Something8      1
205                                            Something9      1
205                                            Something10      1

then i have a view created
SELECT     [Unit UPC Base Item], [Item Description]
FROM         (SELECT     MIN([Item Description]) AS [Item Description], [Unit UPC Base Item]
                       FROM          dbo.ItemDescLookups
                       GROUP BY [Unit UPC Base Item]) AS Sub

Open in new window

When i try Min something 10 gets returned and when i try max something5 gets returned.

so you need anymore info bud?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

Output you need from the input you have given.
Aravind Ranganathan

ASKER
@ Pawan Kumar something the one with count of 10
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Aravind Ranganathan

ASKER
@Pavan Kumar worked beautifully exactly what i wanted. Thnx bud.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Aravind Ranganathan

ASKER
Thnx
Pawan Kumar

Welcome. Glad to help as always. :)
Aravind Ranganathan

ASKER
@Pavan Kumar how do i put this code in a view bud its throwing errors?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

CREATE VIEW VIEWName
AS
SELECT [Unit UPC Base Item],[Item Description],[CountOfDescription]
FROM 
(
	SELECT  * , ROW_NUMBER() OVER ( PARTITION BY [Unit UPC Base Item] ORDER BY CountOfDescription DESC) rnk
	FROM dbo.ItemDescLookups
)k WHERE rnk = 1

Open in new window