SELECT
dbo.ProductionTicket.JCNumber AS JCNumber,
CONVERT(date, dbo.ProductionTicket.DateReceived) AS Received,
dbo.ProductionTicket.Description AS Description,
'Production' AS Job,
CONVERT(date, dbo.ProductionTicket.DateCompleted) AS DateCompleted,
CASE dbo.ProductionTicket.Closed
WHEN 'False' THEN 'Open'
WHEN 'True' THEN 'Completed' END AS Completed
FROM dbo.ProductionTicket
WHERE (dbo.ProductionTicket.Closed = 'False')
OR
((dbo.ProductionTicket.Closed = 'True')
AND (dbo.ProductionTicket.DateCompleted > GETDATE() - 30))
UNION ALL
SELECT
CAST(dbo.TranscriptProcess.ID AS nvarchar(15)) AS JCNumber,
CONVERT(date, dbo.TranscriptProcess.DTReceived) AS Received,
LTRIM(RTRIM((ISNULL(dbo.TranscriptProcess.NameFirst, '') + ' ' + ISNULL(dbo.TranscriptProcess.NameLast, '')))) AS Description,
'Transcription' AS Job,
CONVERT(date, dbo.TranscriptProcess.DTFromReader) AS DateCompleted,
CASE dbo.TranscriptProcess.JobStatus
WHEN '19 - Returned to Client' THEN 'Completed'
WHEN '20 - Cancelled by Client' THEN 'Cancelled'
ELSE 'Open'
END AS Completed
FROM dbo.TranscriptProcess
WHERE ((dbo.TranscriptProcess.JobStatus = '19 - Returned to Client')
AND (dbo.TranscriptProcess.DTDUE > GETDATE() - 30))
OR
(dbo.TranscriptProcess.JobStatus <> '19 - Returned to Client'
AND dbo.TranscriptProcess.JobStatus <> '20 - Cancelled by Client')
This is designed to return various in-process jobs from two different tables and give a single view result.
So, there are two queries (the two SELECT statements) combined (the UNION ALL statement) to one record set.
Both SELECT statements return six fields (JCNumber, Received, Description, Job, DateCompleted and Completed).
Since the two tables are not identical, there is some data massaging going on prior to the UNION ALL step.
The first SELECT statement fields in order:
- Simply applies a specific name to the field JCNumber.
- This field needs to be CONVERTed to a Date format and named Received.
- This is just named Description
- This is just named Job.
- Another Date CONVERT and renamed to DateCompleted.
- This needs boolean values CASEd to Open and Completed and named Completed.
The second SELECT statement fields in order:
- First the field has to be CAST as a 15 character nvarchar, then named JCNumber.
- Needs to match the field from the first SELECT statement.
- This field needs to be CONVERTed to a Date format and named Received.
- This field is generated by the concatenation of two NULLable fields (FirstName and LastName) with a space between with any leading (LTRIM) and trailing (RTRIM) spaces removed; and named Description.
- This is just named Job.
- Another Date CONVERT and named to DateCompleted.
- Another CASE process, except this time we are pealing off two specific values and the remaining ones are set to Open; and named Completed.
Just wanted to show how convoluted SQL queries can be de-composed into easy to understand bits.
Nothing like explaining something to blow the cobwebs from the mind.
I like it.
chuck
No comments:
Post a Comment