dianemarie
Instructor
Hello,
This is for a stored procedure I am using for a report. I have a table that has several transactions per order number, and each transaction has an associated quantity and cost (dollars). As an order is revised, so are the dollars and quantities associated with that order. I need all the dollar transactions, but only the most recent quantity. I have created a table variable to pull one record per order with the most recent document date and it's associated quantity. I get the dollar transactions from my main table. I link my main table to the var table, which returns all the dollar transactions per order number with a field from my var table for qty, which just repeats itself. I will deal with that in the report. Because I am grouping by order number, I have to have the quantity be some sort of aggregate. But neither Min nor Max will work, as it pulls the qty information independently of the document date. I don't know if I have to completely rethink this. The way I am declaring the table is below and an example of how records are returned by the stored procedure also. Again, the problem is that the Qty field is pulling the Min or Max independently of the document date and I'm not sure how else to aggregate that field. Any guidance would be greatly appreciated.
DECLARE @AP_Billed_Qty table
(DocumentDate datetime,
OrderNumber varchar (100),
APBilledQty int)
Insert @AP_Billed_Qty
select
max(ap.documentdate),
max(ap.order_id),
max(ap.qty_invoicebill_base)
from nm_ap ap
group by ap.ordernumber
DocDate OrderID DocNumber Dollars Qty
2-1-09 ABC ABC1 $200 10
2-2-09 ABC ABC2 -$200 10
2-2-09 ABC ABC3 $180 10
This is for a stored procedure I am using for a report. I have a table that has several transactions per order number, and each transaction has an associated quantity and cost (dollars). As an order is revised, so are the dollars and quantities associated with that order. I need all the dollar transactions, but only the most recent quantity. I have created a table variable to pull one record per order with the most recent document date and it's associated quantity. I get the dollar transactions from my main table. I link my main table to the var table, which returns all the dollar transactions per order number with a field from my var table for qty, which just repeats itself. I will deal with that in the report. Because I am grouping by order number, I have to have the quantity be some sort of aggregate. But neither Min nor Max will work, as it pulls the qty information independently of the document date. I don't know if I have to completely rethink this. The way I am declaring the table is below and an example of how records are returned by the stored procedure also. Again, the problem is that the Qty field is pulling the Min or Max independently of the document date and I'm not sure how else to aggregate that field. Any guidance would be greatly appreciated.
DECLARE @AP_Billed_Qty table
(DocumentDate datetime,
OrderNumber varchar (100),
APBilledQty int)
Insert @AP_Billed_Qty
select
max(ap.documentdate),
max(ap.order_id),
max(ap.qty_invoicebill_base)
from nm_ap ap
group by ap.ordernumber
DocDate OrderID DocNumber Dollars Qty
2-1-09 ABC ABC1 $200 10
2-2-09 ABC ABC2 -$200 10
2-2-09 ABC ABC3 $180 10