Sunday, 15 February 2015

sql - Merge common columns in two tables into a single set of columns -


I have a database that stores customer invoices information in the following fashion

< Invoice - Customer Customer ID Customer ID Store Invoice Item Details

  ID Month Customer ID Total Amount   

InvoiceItemITS - For customers, / P>

  id InvoiceID DescriptionI D Price Volume Amount Details A ???? - Non-General details entered in the manual pipeline   

InvocieItemTP - Store invoice item details for TP customers

  ID InvoiceID DescriptionID Value Volume Amount Transaction Start Date End Date   

Details - Store General Invoice Item Details

  ID name   

I have to show all invoice items for both customers (both types) for the given month. The result should include the details of all the columns of the customer and the invoice table and the common columns and invoitesitets in Invoice ITMP should be manually entered with the details columns if it is a general description for the other details details table. InvoiceITs and invoices ITMT are mutually exclusive and nearly half the columns are in common. I need help in invoicing and invoicing ITMTP to incorporate common columns into a set of columns and decide the details of each line. how can I do this?

FYI: I can not add or change columns in tables or database structures

You can use LEFT JOIN both tables and then CASE on type to select from the appropriate table. Here's a sample for some columns, you can do it for all the columns.

  SELECT c.ID, c.Name, c.Type, i.Month, i [Total amount], when the case is c.Type = 'its' then its.Price otherwise tp.Price end -construct the value for the common column, as the case c.Type when' its' then its.Volume when ' T.P. ' Then tp.Volume or Zero End Quantity - or something like this, is the case when c.Type = 'its' then details as COALESCE (its.Description, dits.Name) and dtp.Name end - Customer For the left description of this, join the invoice in the on.CustomerID = c.ID join the left InvoiceItemITS on its own.InvoiceID = in.ID on the left Join InvocieItemTP tp on tp.InvoiceID = in.ID Join Left [Details ] Include DTP .id = tp.DescriptionID left on DTP [details] DITIIT ID on Id = its Descript ID    

No comments:

Post a Comment