I have been searching for answers on this but found none. I think it might be due to the fact that I don't know what terms to search for. Also, this is my first post, so I want to apologise if I use the incorrect formats.
I need to following output:
Invoice | Inv Date | Created by | Destination | Goods $ | Freight | Insurance $ |
33813..| 12 Dec ..| Jack ........| Cape Town | 250.00 ..| 50.00 ..| 10.00 ...|
33814..| 12 Dec ..| Jenny .........| Durban ......| 5,000.00| 20.00 ..| ....|
The first 5 columns are build from various columns using the Invoice column as an index.
Then I want to add the freight and insurance. This is hosted in a different table with the below layout:
InvCostID | Invoice | Cost Code | Cost Description | Value |<br/>
556 ..........| 33813 .| 1 ...............| Freight ...............| 50.00 |
559 ..........| 33813 .| 2 ...............| Insurance ...........| 10.00 |
570 ..........| 33814 .| 1 ...............| Freight ...............| 20.00 |
The problem is that I cannot just select columns to include as the Freight and insurance are in different rows. To get around this, I have created two 'sub tables'. One were the Cost code is 1 (thus, the freight table) and one with the cost code 2 (the insurance table).
Then I just select the Value from the correct table.
The problem: If one of the cost components does not exist (like the Insurance for invoice 33814), my current query excludes that invoice from the results completely. (with the above tables, my below code would only show invoice 33813.
select
IT.Invoice as 'Invoice',
IT.InvDate as 'Inv Date',
UT.UserFullName as 'Created by',
IT.DestinationDescription as 'Destination',
IT.USDVal as 'Goods $',
FREIGHTLIST.Value as 'FREIGHT $',
INSURANCELIST.Value as 'INSURANCE $'
from InvoiceTable IT,
UserTable UT,
(select * from SundryCostTable where CostCode = 1) as FREIGHTLIST,
(select * from SundryCostTable where CostCode = 2) as INSURANCELIST
where IT.InvDate > '2014-12-01'
and IT.UserId = UT.UserId
and IT.Invoice = FREIGHTLIST.Invoice
and IT.Invoice = INSURANCELIST.Invoice
Please help.
Thank you Nico
(I am using SQL Server Management Studio to run the query)