SQUASHJUNKIE
IS-IT--Management
I am trying to query two tables in MySQL 4.1, one which has invoice information, the other which has price history info.
In a sample scenario the invoice table has CUSTOMER, INV_NO, ITEM, INV_DATE, and the price table has CUSTOMER, ITEM, START_DATE, END_DATE, PRICE. The price table does not necessarily have a value loaded in the "CUSTOMER" field
e.g. INVOICES
INV01 CUSTA ITEM1 2011-01-08
INV01 CUSTA ITEM2 2011-01-08
INV02 CUSTB ITEM2 2011-01-20
PRICING table
CUSTA ITEM1 2011-01-01 2011-01-31 14.00 EUR
CUSTA ITEM1 2011-02-01 0000-00-00 15.00 EUR
CUSTA ITEM1 2011-02-01 0000-00-00 10.00 GBP
CUSTA ITEM2 2011-01-01 0000-00-00 10.00 EUR
NULL ITEM2 2011-01-01 0000-00-00 12.50 EUR
NULL ITEM2 2011-01-01 0000-00-00 10.00 GBP
I need to query the invoice line items and return the correct line item price, but I am at a bit of a loss...
I have tried the following, but get the error that the subquery returns more than one row..
select inv.cust,inv.invoicenum,inv.item,inv.date,
(
select
(case
when price.cust=inv.cust AND price.from>=inv.date and price.to<=inv.date then price.value
when price.cust=inv.cust AND price.to='0000-00-00' then price.value
when price.to='0000-00-00' then price.value
else 0
end) as price
from schema.pricing price
)
from schema.invoices inv
left outer join schema.pricing price
on inv.item=price.item
In a sample scenario the invoice table has CUSTOMER, INV_NO, ITEM, INV_DATE, and the price table has CUSTOMER, ITEM, START_DATE, END_DATE, PRICE. The price table does not necessarily have a value loaded in the "CUSTOMER" field
e.g. INVOICES
INV01 CUSTA ITEM1 2011-01-08
INV01 CUSTA ITEM2 2011-01-08
INV02 CUSTB ITEM2 2011-01-20
PRICING table
CUSTA ITEM1 2011-01-01 2011-01-31 14.00 EUR
CUSTA ITEM1 2011-02-01 0000-00-00 15.00 EUR
CUSTA ITEM1 2011-02-01 0000-00-00 10.00 GBP
CUSTA ITEM2 2011-01-01 0000-00-00 10.00 EUR
NULL ITEM2 2011-01-01 0000-00-00 12.50 EUR
NULL ITEM2 2011-01-01 0000-00-00 10.00 GBP
I need to query the invoice line items and return the correct line item price, but I am at a bit of a loss...
I have tried the following, but get the error that the subquery returns more than one row..
select inv.cust,inv.invoicenum,inv.item,inv.date,
(
select
(case
when price.cust=inv.cust AND price.from>=inv.date and price.to<=inv.date then price.value
when price.cust=inv.cust AND price.to='0000-00-00' then price.value
when price.to='0000-00-00' then price.value
else 0
end) as price
from schema.pricing price
)
from schema.invoices inv
left outer join schema.pricing price
on inv.item=price.item