Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-04-15 : 09:32:13
|
| Hi all - had a bit of a nightmare at work with order accounts application. Basically - all the orders have been set to uninvoiced and I need to set the ones which have been invoiced back to invoiced. Problem is that orders can be part invoiced.Tables as follows:ordersorderid PK intinvoiced intorderlineorderid intqty intproductid intinvoicesinvoiceid PK intorderid intinvoicelineinvoiceid intqty intproductid intThere can be multiple invoices for each orderQuestion is - how can I write a query which updates the order table - set invoiced to 1 where there are the same amount of items in the order as in the related invoices?Real emergency this one folks!Hopefully someone can help me out!Stephen. |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-15 : 09:37:22
|
| does orderline relate to orders and invoiceline relate to invoices? think you should be a little clearer in your example firstEm |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-04-15 : 09:38:52
|
| Sorry - edited for clarity. |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-15 : 09:46:43
|
obviously not tested but something like...select *from (select orderid, sum(qty) as orderQtyfrom orders o join orderline ol on o.orderid = ol.orderidgroup by orderid) orders join(select orderid,sum(qty) as invoiceQtyfrom invoice i join invoiceline il on i.invoiceid = il.invoiceidgroup by orderid) invoices on orders.orderid = invoices.orderidwhere orderqty = invoiceqty Em |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-04-15 : 09:54:06
|
| Thanks for the prompt reply! That does not link on the productid though does it? |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-15 : 09:55:35
|
| no, if you just want to check the quantities on the order and the invoice you wouldn't need it eh?Em |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-04-15 : 09:56:57
|
| But there's more than 1 product to an order? |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-15 : 09:59:37
|
| so? if it's just grouped on orderid and totalling the quantity, what difference does it make about the constituent products?perhaps you should post some sample data for us to test withEm |
 |
|
|
steve_c
Yak Posting Veteran
54 Posts |
Posted - 2008-04-15 : 10:21:50
|
| Of course you're right. Emergency averted - a million thanks :) |
 |
|
|
|
|
|