SQL - добавить пустой столбец и заполнить на основе значения в вычисляемом столбце с датой - PullRequest
0 голосов
/ 17 октября 2019

Я очень новичок в SQL и тоже самоучка .... так что будьте ясны, пожалуйста. Я пытаюсь добавить пустой столбец в мои данные, который будет показывать, если число в столбце дней:

Между 15-21 AS 15-21 дней, 22-28 дней AS 22-28 дней,> 28 дней после нарушения

Я могу получить пустой столбец с помощью ' ' AS Status. Мне просто нужно знать, как его заполнить

declare @HG Varchar (100)

set @HG = 'Clinical Support - Health Group'



SELECT distinct OURREF as [DATIX ref], DATE_OPENED as [Date opened (dd/mm/yyyy)], DATE_OF_INCIDENT as [Incident date], ORGANISATION AS [Health Group], DIRECTORATE as [Division], SPECIALTY as [Specialty], ACTUAL_LOCATION As [Exact location], SEVERITY as [Severity], DATEDIFF(Day, DATE_OPENED, CURRENT_TIMESTAMP) as Days, INCIDENT_TYPE as [Type], CATEGORY as [Category], SUBCATEGORY as [sub Category], APPROVED_CODE as [Current approval status], inc_mgr AS [Handler]
FROM [CDI_PATEX_INCIDENTS] AS a
JOIN dbo.DATIX_incidents_main b on a.OURREF = b.inc_ourref

where OURREF not like 'SUI%' and APPROVED_CODE in ('INREV') and ORGANISATION in (@HG) 
and DATEDIFF(dd,DATE_OPENED,GETDATE()) between 15 and 21

UNION

SELECT distinct OURREF as [DATIX ref], DATE_OPENED as [Date opened (dd/mm/yyyy)], DATE_OF_INCIDENT as [Incident date], ORGANISATION AS [Health Group], DIRECTORATE as [Division], SPECIALTY as [Specialty], ACTUAL_LOCATION As [Exact location], SEVERITY as [Severity], DATEDIFF(Day, DATE_OPENED, CURRENT_TIMESTAMP) as Days, INCIDENT_TYPE as [Type], CATEGORY as [Category], SUBCATEGORY as [sub Category], APPROVED_CODE as [Current approval status], inc_mgr AS [Handler]
FROM [CDI_PATEX_INCIDENTS] AS a
JOIN dbo.DATIX_incidents_main b on a.OURREF = b.inc_ourref

where OURREF not like 'SUI%' and APPROVED_CODE in ('INREV') and ORGANISATION in (@HG) 
and DATEDIFF(dd,date_reported,GETDATE()) between 21 and 28

UNION


SELECT distinct OURREF as [DATIX ref], DATE_OPENED as [Date opened (dd/mm/yyyy)], DATE_OF_INCIDENT as [Incident date], ORGANISATION AS [Health Group], DIRECTORATE as [Division], SPECIALTY as [Specialty], ACTUAL_LOCATION As [Exact location], SEVERITY as [Severity], DATEDIFF(Day, DATE_OPENED, CURRENT_TIMESTAMP) as Days, INCIDENT_TYPE as [Type], CATEGORY as [Category], SUBCATEGORY as [sub Category], APPROVED_CODE as [Current approval status], inc_mgr AS [Handler]
FROM [CDI_PATEX_INCIDENTS] AS a
JOIN dbo.DATIX_incidents_main b on a.OURREF = b.inc_ourref

where OURREF not like 'SUI%' and APPROVED_CODE in ('INREV') and ORGANISATION in (@HG) 
and DATEDIFF(dd,DATE_OPENED,GETDATE()) > 28


Group by OURREF, SPECIALTY, ORGANISATION, SEVERITY, DIRECTORATE, DATE_OPENED, DATE_OF_INCIDENT, ACTUAL_LOCATION, INCIDENT_TYPE, CATEGORY, SUBCATEGORY, DATE_REPORTED, APPROVED_CODE, inc_mgr
Order by Days desc

1 Ответ

1 голос
/ 17 октября 2019

Почему бы вам не сделать их одним выбором, без союзов, и не использовать эту технику?

select 
....,

case when DATEDIFF(dd, DATE_OPENED, GETDATE()) BETWEEN 21 AND 28 then 'x'
when DATEDIFF(dd, DATE_OPENED, GETDATE()) > 28 then 'y'
else 'z' end as Status

from tablex
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...