So, I've got a bit of a conundrum that I cannot for the life of me figure out.

Here's what I need to do :

In column PO_I, look for column ITEM with lowest # in column Seq_I.
If column Ship_Q < Qty_Ordered, look for Ship_Qty within ITEM with next largest Seq_I, and sum the Ship_Qtys. Continue this until all Ship_Qs of the ITEM in the same PO_I have been summed.

Is this possible to do in SQL?

8 Years
Discussion Span
Last Post by Ramy Mahrous

The table is layed out like this:

PO_I ITEM Ship_Q Seq_I Qty Ordered
1234 1112 2 1 6
1234 1112 2 2 6
1234 1112 2 3 6
1234 2224 3 1 6
1234 2224 3 2 6

And I'd like it to sum all instances Ship_Q for each specific item, so that it it equals the quantity ordered, like this :

PO_I ITEM Ship_Q Qty Ordered
1234 1112 6 6
1234 2224 6 6

Does that make more sense?


Edit : And each of the columns of numbers should line up with the column names, but I don't know how to do a non-breaking space in anything but html...sorry!


Here you're

SELECT     Ship_Q, SUM(Seq_I) AS Expr2, [PO_I ITEM], Ordered
FROM         tableTest -- table name
GROUP BY Ship_Q, [PO_I ITEM], Ordered

Thanks, I've almost got it.

Here's what I have now :

SELECT [Crossref table].PO_I, [Crossref table].MZRT_ITEM_I, Sum([Crossref table].[ITEM_SHIP_Q]) AS [Shipped Qty], [BA Access Main].[Qty Ordered],
WHERE [Crossref table].[Item_Ship_Q]<[BA Access Main].[Qty Ordered],
FROM [Crossref table] INNER JOIN [BA Access Main] ON [Crossref table].PO_I = [BA Access Main].[PO#]
GROUP BY [Crossref table].PO_I,  [Crossref table].MZRT_ITEM_I, [Crossref table].ITEM_SHIP_Q, [BA Access Main].[Qty Ordered];

But I can't seem to get the WHERE clause to work. To clarify, I only want the [ITEM_SHIP_Q] to be summed where [ITEM_SHIP_Q] is less than [Qty Ordered]. If it's not less than [Qty Ordered], I want it to stay as is.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.