When asking for help on a forum - it is generally considered good practice to provide sample data in the form of table create (or declare) and insert statements. This allows others on the forums to test solutions based on actual data.
Pictures are okay to help describe the issue - but we cannot take a picture and run a query.
With that said - here is a solution, with sample data:
--==== Sample data
Declare @CPE_Invoice_Detail Table (InvoiceDate date, InvoiceState char(2), SalesPrice numeric(8,2));
Declare @MailOut_Data Table (MailOutState char(2), Quantity int, MailOutDate date);
Insert Into @CPE_Invoice_Detail (InvoiceDate, InvoiceState, SalesPrice)
Values ('2018-06-20', 'NE', 149.00)
, ('2018-06-26', 'NE', 69.00)
, ('2018-01-18', 'NE', 50.00)
, ('2018-10-31', 'NE', 139.00)
, ('2018-09-20', 'NE', 108.00)
, ('2018-03-28', 'NE', 129.00)
, ('2018-08-15', 'NE', 129.00)
, ('2018-09-06', 'WY', -35.60)
, ('2018-10-02', 'WY', -32.70)
, ('2018-01-18', 'WY', 50.00)
, ('2018-05-24', 'WY', 35.00)
, ('2018-09-26', 'WY', 92.00)
, ('2018-01-15', 'WY', 49.00)
, ('2018-05-11', 'WY', 49.00);
Insert Into @MailOut_Data (MailOutState, Quantity, MailOutDate)
Values ('NE', 2260, '2018-08-15')
, ('WY', 562, '2018-08-29');
--==== Solution
With allStatesDates
As (
Select inv.InvoiceState
, inv.InvoiceDate
From @CPE_Invoice_Detail inv
Union
Select md.MailOutState
, md.MailOutDate
From @MailOut_Data md
)
Select asd.InvoiceState
, Quantity = coalesce(md.Quantity, 0)
, asd.InvoiceDate
, SalesPrice = coalesce(inv.SalesPrice, 0.00)
From allStatesDates asd
Left Join @CPE_Invoice_Detail inv On inv.InvoiceState = asd.InvoiceState
And inv.InvoiceDate = asd.InvoiceDate
Left Join @MailOut_Data md On md.MailOutState = asd.InvoiceState
And md.MailOutDate = asd.InvoiceDate;
--==== Second Solution
Select inv.InvoiceState
, Quantity = coalesce(md.Quantity, 0)
, inv.InvoiceDate
, inv.SalesPrice
From @CPE_Invoice_Detail inv
Left Join @MailOut_Data md On md.MailOutState = inv.InvoiceState
And md.MailOutDate = inv.InvoiceDate
Union
Select md.MailOutState
, md.Quantity
, md.MailOutDate
, SalesPrice = coalesce(inv.SalesPrice, 0.00)
From @MailOut_Data md
Left Join @CPE_Invoice_Detail inv On inv.InvoiceState = md.MailOutState
And inv.InvoiceDate = md.MailOutDate
In the sample data - I changed the column names because [State] and [Date] are reserved words and spaces in column names need to be quoted.
The first solution requires creating a list of all possible state/date combinations - then using that as the source of the query. You can then outer join in both of your tables to get the desired results - using coalesce to set the quantity and sales to 0 when not found.
The second solution UNIONS the results of 2 separate queries that return the data from both sets of data.
The UNION removes any duplicate rows that would be returned.