Solved

Add Join to Return All Dates

Posted on 2008-10-24
2
244 Views
Last Modified: 2012-05-05
I have the query in the below snippet that works fine.  However, instead of returning only dates that contain values - as it does now, I, instead, need it to return ALL dates, and if there is no data for a given date, then it can just return every other column as null.

So, in an effort to accomplish this, I built a table named 'TSIDateLookup' with a column named 'DateFull' that contains ALL dates.

I was thinking that I just needed to LEFT JOIN this new table to the below query, but apparently I do not know how to accomplish that.  The join that needs to be made is between my new table/column dbo.TSIDateLookup.DateFull , and that would be equal to the following field in the existing query: dbo.p21_view_oe_hdr.order_date .  So, dbo.TSIDateLookup.DateFull  = dbo.p21_view_oe_hdr.order_date is what needs to somehow be added.

In case it matters, the new table is just that - a table.  Everything else being selected in the existing query is being pulled from a view.

Thanks for your help.
SELECT DISTINCT 

                      TOP 100 PERCENT dbo.p21_view_oe_hdr.company_id, dbo.p21_view_location.location_id AS [Sales Location ID], dbo.p21_view_oe_hdr.customer_id, 

                      dbo.p21_view_customer.customer_name, dbo.p21_view_customer.class_1id, dbo.p21_view_oe_hdr.order_no, dbo.p21_view_oe_hdr.order_date, 

                      dbo.p21_view_ship_to_salesrep.ship_to_id, dbo.p21_view_oe_hdr.ship2_name, dbo.p21_view_ship_to_salesrep.salesrep_id AS [Salesrep ID Ship2], 

                      p21_view_contacts_2.last_name + ', ' + p21_view_contacts_2.first_name AS [Salesrep Name Ship2], 

                      p21_view_contacts_2.first_name AS [Salesrep First Ship2], p21_view_contacts_2.last_name AS [Salesrep Last Ship2], 

                      dbo.p21_view_customer.salesrep_id AS [Salesrep ID Cust], 

                      p21_view_contacts_1.last_name + ', ' + p21_view_contacts_1.first_name AS [Salesrep Name Cust], 

                      p21_view_contacts_1.first_name AS [Salesrep First Cust], p21_view_contacts_1.last_name AS [Salesrep Last Cust], 

                      dbo.p21_view_oe_hdr_salesrep.salesrep_id AS [Salesrep ID Order], 

                      dbo.p21_view_contacts.last_name + ', ' + dbo.p21_view_contacts.first_name AS [Salesrep Name Order], 

                      dbo.p21_view_contacts.first_name AS [Salesrep First Order], dbo.p21_view_contacts.last_name AS [Salesrep Last Order], 

                      dbo.p21_view_oe_line.line_no, dbo.p21_view_oe_line.delete_flag AS OE_Lin_Del_Flag, dbo.p21_view_oe_line.cancel_flag AS OE_Lin_Cancel_Flag, 

                      dbo.p21_view_inventory_supplier.supplier_id AS [Primary Supplier ID], dbo.p21_view_supplier.supplier_name AS [Primary Supplier Name], 

                      dbo.p21_view_oe_line.item_id, dbo.p21_view_oe_line.qty_ordered, dbo.p21_view_oe_line.qty_per_assembly, dbo.p21_view_oe_line.unit_price, 

                      dbo.p21_view_oe_line.extended_price, dbo.p21_view_oe_line.assembly, dbo.p21_view_oe_line.unit_of_measure, 

                      dbo.p21_view_oe_line.commission_cost, dbo.p21_view_oe_line.commission_cost * dbo.p21_view_oe_line.qty_ordered AS Ext_Comm_Cost, 

                      dbo.p21_view_oe_line.extended_price - dbo.p21_view_oe_line.commission_cost * dbo.p21_view_oe_line.qty_ordered AS Profit, 

                      dbo.p21_view_oe_line.parent_oe_line_uid, dbo.p21_view_oe_line.disposition, dbo.p21_view_oe_hdr_salesrep.delete_flag AS Salesrep_Del_Flag, 

                      dbo.p21_view_oe_hdr.rma_flag

FROM         dbo.p21_view_ship_to_salesrep INNER JOIN

                      dbo.p21_view_oe_line INNER JOIN

                      dbo.p21_view_oe_hdr ON dbo.p21_view_oe_line.order_no = dbo.p21_view_oe_hdr.order_no INNER JOIN

                      dbo.p21_view_location ON dbo.p21_view_oe_hdr.location_id = dbo.p21_view_location.location_id AND 

                      dbo.p21_view_oe_hdr.company_id = dbo.p21_view_location.company_id INNER JOIN

                      dbo.p21_view_address ON dbo.p21_view_location.location_id = dbo.p21_view_address.id INNER JOIN

                      dbo.p21_view_oe_hdr_salesrep ON dbo.p21_view_oe_hdr.order_no = dbo.p21_view_oe_hdr_salesrep.order_number INNER JOIN

                      dbo.p21_view_contacts ON dbo.p21_view_oe_hdr_salesrep.salesrep_id = dbo.p21_view_contacts.id INNER JOIN

                      dbo.p21_view_customer ON dbo.p21_view_oe_hdr.customer_id = dbo.p21_view_customer.customer_id AND 

                      dbo.p21_view_oe_hdr.company_id = dbo.p21_view_customer.company_id INNER JOIN

                      dbo.p21_view_contacts p21_view_contacts_1 ON dbo.p21_view_customer.salesrep_id = p21_view_contacts_1.id INNER JOIN

                      dbo.p21_view_inv_mast ON dbo.p21_view_oe_line.inv_mast_uid = dbo.p21_view_inv_mast.inv_mast_uid INNER JOIN

                      dbo.p21_view_inventory_supplier ON dbo.p21_view_inv_mast.inv_mast_uid = dbo.p21_view_inventory_supplier.inv_mast_uid INNER JOIN

                      dbo.p21_view_inventory_supplier_x_loc ON 

                      dbo.p21_view_inventory_supplier.inventory_supplier_uid = dbo.p21_view_inventory_supplier_x_loc.inventory_supplier_uid AND 

                      dbo.p21_view_oe_line.source_loc_id = dbo.p21_view_inventory_supplier_x_loc.location_id INNER JOIN

                      dbo.p21_view_supplier ON dbo.p21_view_inventory_supplier.supplier_id = dbo.p21_view_supplier.supplier_id ON 

                      dbo.p21_view_ship_to_salesrep.company_id = dbo.p21_view_oe_hdr.company_id AND 

                      dbo.p21_view_ship_to_salesrep.ship_to_id = dbo.p21_view_oe_hdr.address_id INNER JOIN

                      dbo.p21_view_contacts p21_view_contacts_2 ON dbo.p21_view_ship_to_salesrep.salesrep_id = p21_view_contacts_2.id

WHERE     (dbo.p21_view_oe_line.delete_flag = 'N') AND (dbo.p21_view_oe_line.item_id <> 'MEMO') AND (dbo.p21_view_oe_line.cancel_flag = 'N') AND 

                      (dbo.p21_view_oe_line.parent_oe_line_uid = 0) AND (dbo.p21_view_oe_line.disposition <> 'C' OR

                      dbo.p21_view_oe_line.disposition IS NULL) AND (dbo.p21_view_oe_hdr_salesrep.delete_flag = 'N') AND (dbo.p21_view_oe_hdr.company_id = '01') 

                      AND (dbo.p21_view_oe_hdr.delete_flag = 'N') AND (dbo.p21_view_oe_hdr.projected_order = 'N') AND (dbo.p21_view_oe_hdr.cancel_flag = 'N') AND 

                      (dbo.p21_view_inventory_supplier_x_loc.primary_supplier = 'Y')

ORDER BY dbo.p21_view_oe_hdr.order_date, dbo.p21_view_oe_line.line_no

Open in new window

0
Comment
Question by:timoteoga
2 Comments
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22797166
I am not sure if i understand correctly but you are using inner joins between your views so getting all records cant be done becouse with some of those joins you are probably removing records that u need to get.

First what u can try is adding appropriate left joins instead of inner joins for table that actually remove your records from resultset.

It is hard for me to know wich view contains what data and where inner join or left join should be used.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22797285
I'll try to help clarify that, and throw another possible solution in.

If you have

a inner join b outer join c

then a and b must exist but c is optional

if you have

a outer join b inner join c

then a, b and c must exist because c is inner joined.

Now you can make all of your inners outer, but that may not work.  but you can wrap the existing query into a derived table and left outer join to it.

like this:


select t.fulldate, a.*

from tsiDatelookup t

left outer join 

(SELECT DISTINCT 

                      TOP 100 PERCENT dbo.p21_view_oe_hdr.company_id, dbo.p21_view_location.location_id AS [Sales Location ID], dbo.p21_view_oe_hdr.customer_id, 

                      dbo.p21_view_customer.customer_name, dbo.p21_view_customer.class_1id, dbo.p21_view_oe_hdr.order_no, dbo.p21_view_oe_hdr.order_date, 

                      dbo.p21_view_ship_to_salesrep.ship_to_id, dbo.p21_view_oe_hdr.ship2_name, dbo.p21_view_ship_to_salesrep.salesrep_id AS [Salesrep ID Ship2], 

                      p21_view_contacts_2.last_name + ', ' + p21_view_contacts_2.first_name AS [Salesrep Name Ship2], 

                      p21_view_contacts_2.first_name AS [Salesrep First Ship2], p21_view_contacts_2.last_name AS [Salesrep Last Ship2], 

                      dbo.p21_view_customer.salesrep_id AS [Salesrep ID Cust], 

                      p21_view_contacts_1.last_name + ', ' + p21_view_contacts_1.first_name AS [Salesrep Name Cust], 

                      p21_view_contacts_1.first_name AS [Salesrep First Cust], p21_view_contacts_1.last_name AS [Salesrep Last Cust], 

                      dbo.p21_view_oe_hdr_salesrep.salesrep_id AS [Salesrep ID Order], 

                      dbo.p21_view_contacts.last_name + ', ' + dbo.p21_view_contacts.first_name AS [Salesrep Name Order], 

                      dbo.p21_view_contacts.first_name AS [Salesrep First Order], dbo.p21_view_contacts.last_name AS [Salesrep Last Order], 

                      dbo.p21_view_oe_line.line_no, dbo.p21_view_oe_line.delete_flag AS OE_Lin_Del_Flag, dbo.p21_view_oe_line.cancel_flag AS OE_Lin_Cancel_Flag, 

                      dbo.p21_view_inventory_supplier.supplier_id AS [Primary Supplier ID], dbo.p21_view_supplier.supplier_name AS [Primary Supplier Name], 

                      dbo.p21_view_oe_line.item_id, dbo.p21_view_oe_line.qty_ordered, dbo.p21_view_oe_line.qty_per_assembly, dbo.p21_view_oe_line.unit_price, 

                      dbo.p21_view_oe_line.extended_price, dbo.p21_view_oe_line.assembly, dbo.p21_view_oe_line.unit_of_measure, 

                      dbo.p21_view_oe_line.commission_cost, dbo.p21_view_oe_line.commission_cost * dbo.p21_view_oe_line.qty_ordered AS Ext_Comm_Cost, 

                      dbo.p21_view_oe_line.extended_price - dbo.p21_view_oe_line.commission_cost * dbo.p21_view_oe_line.qty_ordered AS Profit, 

                      dbo.p21_view_oe_line.parent_oe_line_uid, dbo.p21_view_oe_line.disposition, dbo.p21_view_oe_hdr_salesrep.delete_flag AS Salesrep_Del_Flag, 

                      dbo.p21_view_oe_hdr.rma_flag

FROM         dbo.p21_view_ship_to_salesrep INNER JOIN

                      dbo.p21_view_oe_line INNER JOIN

                      dbo.p21_view_oe_hdr ON dbo.p21_view_oe_line.order_no = dbo.p21_view_oe_hdr.order_no INNER JOIN

                      dbo.p21_view_location ON dbo.p21_view_oe_hdr.location_id = dbo.p21_view_location.location_id AND 

                      dbo.p21_view_oe_hdr.company_id = dbo.p21_view_location.company_id INNER JOIN

                      dbo.p21_view_address ON dbo.p21_view_location.location_id = dbo.p21_view_address.id INNER JOIN

                      dbo.p21_view_oe_hdr_salesrep ON dbo.p21_view_oe_hdr.order_no = dbo.p21_view_oe_hdr_salesrep.order_number INNER JOIN

                      dbo.p21_view_contacts ON dbo.p21_view_oe_hdr_salesrep.salesrep_id = dbo.p21_view_contacts.id INNER JOIN

                      dbo.p21_view_customer ON dbo.p21_view_oe_hdr.customer_id = dbo.p21_view_customer.customer_id AND 

                      dbo.p21_view_oe_hdr.company_id = dbo.p21_view_customer.company_id INNER JOIN

                      dbo.p21_view_contacts p21_view_contacts_1 ON dbo.p21_view_customer.salesrep_id = p21_view_contacts_1.id INNER JOIN

                      dbo.p21_view_inv_mast ON dbo.p21_view_oe_line.inv_mast_uid = dbo.p21_view_inv_mast.inv_mast_uid INNER JOIN

                      dbo.p21_view_inventory_supplier ON dbo.p21_view_inv_mast.inv_mast_uid = dbo.p21_view_inventory_supplier.inv_mast_uid INNER JOIN

                      dbo.p21_view_inventory_supplier_x_loc ON 

                      dbo.p21_view_inventory_supplier.inventory_supplier_uid = dbo.p21_view_inventory_supplier_x_loc.inventory_supplier_uid AND 

                      dbo.p21_view_oe_line.source_loc_id = dbo.p21_view_inventory_supplier_x_loc.location_id INNER JOIN

                      dbo.p21_view_supplier ON dbo.p21_view_inventory_supplier.supplier_id = dbo.p21_view_supplier.supplier_id ON 

                      dbo.p21_view_ship_to_salesrep.company_id = dbo.p21_view_oe_hdr.company_id AND 

                      dbo.p21_view_ship_to_salesrep.ship_to_id = dbo.p21_view_oe_hdr.address_id INNER JOIN

                      dbo.p21_view_contacts p21_view_contacts_2 ON dbo.p21_view_ship_to_salesrep.salesrep_id = p21_view_contacts_2.id

WHERE     (dbo.p21_view_oe_line.delete_flag = 'N') AND (dbo.p21_view_oe_line.item_id <> 'MEMO') AND (dbo.p21_view_oe_line.cancel_flag = 'N') AND 

                      (dbo.p21_view_oe_line.parent_oe_line_uid = 0) AND (dbo.p21_view_oe_line.disposition <> 'C' OR

                      dbo.p21_view_oe_line.disposition IS NULL) AND (dbo.p21_view_oe_hdr_salesrep.delete_flag = 'N') AND (dbo.p21_view_oe_hdr.company_id = '01') 

                      AND (dbo.p21_view_oe_hdr.delete_flag = 'N') AND (dbo.p21_view_oe_hdr.projected_order = 'N') AND (dbo.p21_view_oe_hdr.cancel_flag = 'N') AND 

                      (dbo.p21_view_inventory_supplier_x_loc.primary_supplier = 'Y')

)a

on t.fulldate = a.WHATDATE

ORDER BY a.order_date, a.line_no

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now