?
Solved

SQL Select Max for Unique Rows

Posted on 2011-04-22
6
Medium Priority
?
316 Views
Last Modified: 2012-05-11
I am looking at pricing for inventory items.  Some items are returned more than once because they have multiple pricing in the system.  I need to get the MAX pricing for each item.  The query is below.

a.Item_Number is the item that may be returned multiple times if there is multiple prices
b.unit_cost is what I want to get the MAX for each individual item
SELECT  Item_Class_Code,
        a.Item_Number,
        [OnHand] = Qty_Received - Qty_Sold,
        b.Unit_Cost,
        a.Standard_Cost,
        [CostDiff] = a.Standard_Cost - b.Unit_Cost
FROM    dbo.tspvItemMaster a
        LEFT JOIN dbo.tspvInventoryReceipts b ON a.Item_Number = b.Item_Number
WHERE   Qty_Received - Qty_Sold > 0
        AND ( a.Standard_Cost - b.Unit_Cost < -0.02 )
        AND a.Item_Number NOT LIKE '*%'
        AND Item_Class_Code <> 'used equip'
        AND Standard_Cost <> '0.00'
ORDER BY (a.Standard_Cost-b.Unit_Cost)

Open in new window

0
Comment
Question by:r270ba
  • 4
6 Comments
 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 2000 total points
ID: 35447607
Select *, (Select MAX(b.Unit_Cost) from dbo.tspvInventoryReceipts b Where  b.Item_Number =  a.Item_Number  group by b.Item_Number) from dbo.tspvItemMaster a
0
 

Author Comment

by:r270ba
ID: 35447658
Patel,

Your query did not take into account any of my WHERE clause:

WHERE   Qty_Received - Qty_Sold > 0
        AND ( a.Standard_Cost - b.Unit_Cost < -0.02 )
        AND a.Item_Number NOT LIKE '*%'
        AND Item_Class_Code <> 'used equip'
        AND Standard_Cost <> '0.00'
0
 

Assisted Solution

by:r270ba
r270ba earned 0 total points
ID: 35447816
Here is the working query based off of your original post and finished by me:
Select  Item_Class_Code,
        Item_Number,
        Item_Description,
        Standard_Cost,
        ( Select    MAX(b.Unit_Cost) AS 'unit_cost'
          from      dbo.tspvInventoryReceipts b
          Where     b.Item_Number = a.Item_Number
                    AND ( Qty_Received - Qty_Sold > 0 )
          group by  b.Item_Number
        ) AS 'Unit_Cost'
from    dbo.tspvItemMaster a
WHERE   a.Item_Number NOT LIKE '*%'
        AND Item_Class_Code <> 'used equip'
        AND ( Select    MAX(b.Unit_Cost) AS 'unit_cost'
              from      dbo.tspvInventoryReceipts b
              Where     b.Item_Number = a.Item_Number
                        AND ( Qty_Received - Qty_Sold > 0 )
              group by  b.Item_Number
            ) IS NOT NULL
        AND a.Standard_Cost - ( Select  MAX(b.Unit_Cost) AS 'unit_cost'
                                from    dbo.tspvInventoryReceipts b
                                Where   b.Item_Number = a.Item_Number
                                        AND ( Qty_Received - Qty_Sold > 0 )
                                group by b.Item_Number
                              ) < -0.02
ORDER BY Item_Class_Code, a.Item_Number

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35447827
I think you want to read this article: http://www.experts-exchange.com/A_3203.html
0
 

Accepted Solution

by:
r270ba earned 0 total points
ID: 35447838
Thanks angellll.  I will take a look.  I have posted a solution based on Patel's original post.
0
 

Author Closing Comment

by:r270ba
ID: 35473512
I have posted the finished query above based on Patel's original post.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

850 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