I’ve been trying to find a way to split my rows into multiple rows. I came across a CROSS APPLY example and tried to recreate it. I was able to do this, however, I cannot get anything to filter from the WHERE clause. The code looks messy, so I am wondering if I need to just trying a nesting function or something completely different? No matter if I change the first where clause, the second where clause, or both WHERE clauses, the filtering does not occur (such as removing Terry Johnson because TYPE column says ‘LOT’). I am using Microsoft SQL Server Management Studio, and I have been looking everywhere for an answer or alternative to CROSS APPLY. I may not be searching the correct words for what I need. If you can help point me in the direction, that would be great. Thanks!
My original data looks like this:
Div------- Job #------- Type---- Sales Agent--- Cancel Date--- Contract Approved Date
CAE----- Aaab-001--- SNGL--- Joe Smith-------- 02/01/2018------ 01/15/2018
CLT----- Eeef-203----- DUPL--- Jane Smiths---- 05/28/2018------- 05/02/2018
CAE---- Zzzz-101----- LOT----- Terry Johnson----- (blank)---------- 06/01/2018
Output looks like this:
Div------- Job #------- Type---- Sales Agent--- Cancel Date--- Contract Approved Date
CAE----- Aaab-001--- SNGL--- Joe Smith-------- Null---------------- 01/15/2018
CAE----- Aaab-001--- SNGL--- Joe Smith------ 02/01/2018------- Null
CLT----- Eeef-203----- DUPL--- Jane Smiths---- Null---------------- 05/02/2018
CLT----- Eeef-203----- DUPL--- Jane Smiths---- 05/28/2018------- Null
CAE---- Zzzz-101----- LOT----- Terry Johnson----- (blank)---------- 06/01/2018
Want data to look like this (by filtering out LOT):
Div------- Job #------- Type---- Sales Agent--- Cancel Date--- Contract Approved Date
CAE----- Aaab-001--- SNGL--- Joe Smith-------- Null---------------- 01/15/2018
CAE----- Aaab-001--- SNGL--- Joe Smith------ 02/01/2018------- Null
CLT----- Eeef-203----- DUPL--- Jane Smiths---- Null---------------- 05/02/2018
CLT----- Eeef-203----- DUPL--- Jane Smiths---- 05/28/2018------- Null
SELECT
[Ounit Code] AS "Div",
[Project Code] + '-' + [Lot] AS [Job #],
[Sales Agent],
cr.[Cancel Date],
cr.[Contract Approved Date]
FROM [dbo].[Sales Summ] AS ss
CROSS APPLY (VALUES
([Cancel Date],NULL),
(NULL,[Contract Approved Date]))
CR ([Cancel Date], [Contract Approved Date])
WHERE ss.[Cancel Date] IS NOT NULL AND ss.[Contract Approved Date] IS NOT NULL
AND TYPE <> ‘LOT’
UNION
SELECT
[Ounit Code] AS "Div",
[Project Code] + '-' + [Lot] AS [Job #],
[Sales Agent],
ss.[Cancel Date],
ss.[Contract Approved Date]
FROM [dbo].[Sales Summ] AS ss
WHERE ss.[Cancel Date] IS NULL OR ss.[Contract Approved Date] IS NULL
AND TYPE <> ‘LOT’