bobk207
asked on
distinct records
how can I get the attached query to only return unique pallet#'s with largest OnHandQuantity value?
thanks
query.jpg
thanks
query.jpg
ASKER
I did with same results as posted
can you post the text of your query
ASKER
I attached to query with group by and the results
query2.jpg
query2.jpg
If you attach the text of your query instead of image I can rewrite it.
now only UDF04 makes your records duplicate. it depends on your logic, but you can remove UDF04 from group by clause and use MAX(UDF04) in select clause.
if it is not the result you are looking for, another option is to use partition by.
now only UDF04 makes your records duplicate. it depends on your logic, but you can remove UDF04 from group by clause and use MAX(UDF04) in select clause.
if it is not the result you are looking for, another option is to use partition by.
ASKER
SELECT DISTINCT
Inventory.Pallet, Inventory.Site, Inventory.ItemNumber, Inventory.OnHandQuantity, Inventory.UDF04, Item.Description, Item.AlternateNumber,
Inventory.AllocatedOrder, Inventory.OrderNumber, Item.AlternateNumber2, ItemUnitOfMeasure.UnitOfMe asure, ItemUnitOfMeasure.EachCoun t
FROM Inventory INNER JOIN
Item ON Inventory.ItemNumber = Item.ItemNumber INNER JOIN
PickingOrder ON Inventory.Site = PickingOrder.Site AND Inventory.AllocatedOrder = PickingOrder.OrderNumber INNER JOIN
ItemUnitOfMeasure ON Item.ItemNumber = ItemUnitOfMeasure.ItemNumb er
WHERE (Inventory.AllocatedOrder = @ordernumber) AND (Inventory.ItemNumber <> '011299CP') AND (ItemUnitOfMeasure.UnitOfM easure = 'pallet') AND
(Inventory.ItemNumber = '546000j#')
GROUP BY Inventory.Pallet, Inventory.Site, Inventory.ItemNumber, Inventory.OnHandQuantity, Inventory.UDF04, Item.Description, Item.AlternateNumber,
Inventory.AllocatedOrder, Inventory.OrderNumber, Item.AlternateNumber2, ItemUnitOfMeasure.UnitOfMe asure, ItemUnitOfMeasure.EachCoun t
Inventory.Pallet, Inventory.Site, Inventory.ItemNumber, Inventory.OnHandQuantity, Inventory.UDF04, Item.Description, Item.AlternateNumber,
Inventory.AllocatedOrder, Inventory.OrderNumber, Item.AlternateNumber2, ItemUnitOfMeasure.UnitOfMe
FROM Inventory INNER JOIN
Item ON Inventory.ItemNumber = Item.ItemNumber INNER JOIN
PickingOrder ON Inventory.Site = PickingOrder.Site AND Inventory.AllocatedOrder = PickingOrder.OrderNumber INNER JOIN
ItemUnitOfMeasure ON Item.ItemNumber = ItemUnitOfMeasure.ItemNumb
WHERE (Inventory.AllocatedOrder = @ordernumber) AND (Inventory.ItemNumber <> '011299CP') AND (ItemUnitOfMeasure.UnitOfM
(Inventory.ItemNumber = '546000j#')
GROUP BY Inventory.Pallet, Inventory.Site, Inventory.ItemNumber, Inventory.OnHandQuantity, Inventory.UDF04, Item.Description, Item.AlternateNumber,
Inventory.AllocatedOrder, Inventory.OrderNumber, Item.AlternateNumber2, ItemUnitOfMeasure.UnitOfMe
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Start with this:
SELECT i.Pallet,
i.Site,
i.ItemNumber,
MAX(i.OnHandQuantity) MaxOnHandQuantity
FROM Inventory i
INNER JOIN Item t ON i.ItemNumber = t.ItemNumber
INNER JOIN PickingOrder p ON i.[Site] = p.[Site] AND i.AllocatedOrder = p.OrderNumber
INNER JOIN ItemUnitOfMeasure m ON t.ItemNumber = m.ItemNumber
WHERE i.AllocatedOrder = @ordernumber
AND i.ItemNumber <> '011299CP'
AND m.UnitOfMeasure = 'pallet'
AND i.ItemNumber = '546000j#'
GROUP BY i.Pallet,
i.Site,
i.ItemNumber
And add columns as needed. Just understand that they need to be either added as an aggregate function or added to the GROUP BY. This last could cause more rows than you need.
Open in new window