| Author |
Topic |
|
Arend
Starting Member
17 Posts |
Posted - 2007-02-06 : 08:36:38
|
| Hi,I need assistance with the following please.I have to display the total for an invoice. The price for the products change over time. I want the invoice date to correspond with the current period's price.Thus if invoice date is y, it should calculate using the price between x and z.Thanks,Arend |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-02-06 : 08:40:12
|
| Please post some tangible sample data, expected output and table structures to get accurate replies.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
Arend
Starting Member
17 Posts |
Posted - 2007-02-07 : 03:35:50
|
| Hi,I have to display the details and the total cost of a specific invoice. This is the code i have sofar.CREATE PROCEDURE sp_DisplayInvoice@invoice_id VARCHAR(10)ASSelect * FROM INVOICEWHERE invoice_id = @invoice_idSELECT product_code, materialdetail_quantityFROM MATERIALDETAILWHERE invoice_id = @invoice_idSELECT SUM(materialdetail_quantity * price_price) AS TotalCostFROM MATERIALDETAIL, PRICEWHERE invoice_id = @invoice_idGO*************I am having trouble with my SUM code. the problem i have is that i have a price table with dates for different prices for a specific product. i have to calculate the cost for the products where the issue date of the invoice falls between the start and end date of the prices for the specific products.for instance, product A101 has the price 3 for the period 24/07 to 24/08 and then the price change to 5 for the period 24/9 to 24/10. my invoice is issued on 1/08 so that mean i have to calculate the product with the price of 3. how do i do it? also if i can change my code to work better please assist me.Thanx a million. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-07 : 03:41:37
|
| Post some DDL for the PRICE table and MaterialDetail table, please. Without this information, it is impossible for us to know your business logic.As of now, you are producing a Cartesian Product (CROSS JOIN) for all invoice's quantities with all prices...For the second query, change to this just in case you have several lines for same product codeSELECT product_code, sum(materialdetail_quantity) as materialdetail_quantityFROM MATERIALDETAILWHERE invoice_id = @invoice_idgroup by product_codePeter LarssonHelsingborg, Sweden |
 |
|
|
Arend
Starting Member
17 Posts |
Posted - 2007-02-07 : 03:55:03
|
| Price Table with columns: productCode, startDate, endDate, priceMaterialDetail Table with columns: invoiceId, productCode, quantityInvoice Table with columns: id, date, clientIdHope this helps...Thanks for the assisatance. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-07 : 04:02:04
|
Something like thisSELECT SUM(md.Quantity * p.Price) AS TotalCostFROM Invoice AS iINNER JOIN MaterialDetail AS md ON md.InvoiceID = i.IDINNER JOIN Price AS p ON p.ProductCode = md.ProductCode AND p.StartDate <= i.Date AND p.EndDate >= i.DateWHERE i.ID = @Invoice_ID Peter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-02-07 : 04:03:48
|
Something like this?Select p.ProductCode, sum(md.Quantity * p.price)from MaterialDetail as MD JOIN Price p on md.productcode = p.productcodeJOIN Invoice i on md.invoiceid = i.md.invoiceidwhere md.invoiceid = @invoice_idGroup by p.ProductCodeHaving i.[date] between p.StartDate and p.EndDate Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
Arend
Starting Member
17 Posts |
Posted - 2007-02-07 : 04:12:20
|
| Thank you guys. I am going to try it right away.God bless. |
 |
|
|
|