Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query invoice and price matrix

Status
Not open for further replies.

SQUASHJUNKIE

IS-IT--Management
Jul 11, 2003
8
NL
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top