I need to Join two tables where each row in the first table get every row in the second table.
Example
Table 1 :
InvoiceNo, Product code ,quantity
Table 2 :
Product code,Product name
In table 1 I, for each invoice record I have only some products, but in the output I have to get the all the products for each invoice keeping the quantity to 0 if that product is not included in the invoice.
How can I do this?
I have had to guess at your table names but I think this would work as required:
EDIT
To get the data in the format you are after you need to use conditional
SUMs as I don’t think SQL-Server CE supportsPIVOT. Something like this would work for you:This only works though if you know all the products you have at the time of writing the code. if this is likely to change you can use dynamic SQL to create the above query but adding the products on the fly so when new products are added the query doesn’t need to be changed. I haven’t tested this on CE, but I don’t see any reason it wouldn’t work.
I’ve put some examples using SQL-Server 2008R2 on SQL Fiddle