简体   繁体   中英

Combining queries into one final table with business date calculation

I am new to sql and I have two measures that were done in tableau. The measure is as followed

{ FIXED [Client ID]: MAX(IF [Appt Status] = 'Complete' AND [Appointment Type Roll-Up] = '1' THEN [Appt Date]END)}

{ FIXED [Client ID]: MIN(IF[Appointment Status Roll-UP]='Active' and [Appt Date] > [Last Direct Trmt Appt] then [Appt Date]END)}

These measures, I converted into SQL as followed:


select 
a.client_id,
Max(a.appointment_date) as max_date
From #Last_Direct_treatment A
where  a.[appointment_status] = 'Complete' 
and  a.[Appointment_type_roll_up] = '1'
Group by client_id


select 
a.client_id,
min(a.appointment_date) as min_date
From #Last_Direct_treatment A
where  a.[Appointment_Status_Roll_up] = 'active' 
and a.appointment_date > a.last_direct_only_date
Group by client_id

In SQL, I already created the first query looking for my main fields:



SELECT 
s.[appointment_id]
,c.[full_name]
,s.[client_id]
,s.[case_number]
,s.[appointment_date]
,s.[appointment_type]
,s.[appointment_status]
,ca.[last_direct_only_date]
,t.[authorization_status]
,t.[service_type]
,case 
when s.appointment_status = '*_Cancelled' then 'Cancelled'
when s.appointment_status = '*_Complete' then 'Complete'
when s.appointment_status = 'Complete' then 'Complete'
else 'Active'
end as Appointment_Status_Roll_up
,case when
s.[appointment_type] like '%IND%' or s.[appointment_type] like '%indirect%' then '0' else '1'
end as Appointment_type_roll_up

Into #Last_Direct_treatment 
     
FROM [appointment] s
INNER JOIN [client] c
ON s.[client_id] = c.[client_id]
INNER JOIN [client_case] ca
ON c.[client_id] = ca.[client_id]
INNER JOIN [authorization] t
ON ca.[case_number] = t.[case_number]

ORDER BY appointment_date DESC

I was successful in making the following queries find the min and max date of the fields I needed.

My desired result would be to incorporate these two new fields into one final table where I have these as the last two columns and a business date calculation of the two columns as a new column with the fields from the first query.


select 
a.client_id,
Max(a.appointment_date) as max_date
From #Last_Direct_treatment A
where  a.[appointment_status] = 'Complete' 
and  a.[Appointment_type_roll_up] = '1'
Group by client_id


select 
a.client_id,
min(a.appointment_date) as min_date
From #Last_Direct_treatment A
where  a.[Appointment_Status_Roll_up] = 'active' 
and a.appointment_date > a.last_direct_only_date
Group by client_id

The original measures appear to resemble case expressions rather than a set of where clause predicates , eg

CASE WHEN MAX(IF [Appt Status] = 'Complete' 
         AND [Appointment Type Roll-Up] = '1' THEN [Appt Date] END

CASE WHEN MIN(IF[Appointment Status Roll-UP]='Active' 
         AND [Appt Date] > [Last Direct Trmt Appt] THEN [Appt Date] END

and in a combined query these might be as follows:

SELECT
      a.client_id
    , CASE 
        WHEN a.[appointment_status] = 'Complete'
            AND a.[Appointment_type_roll_up] = '1'
            THEN Max(a.appointment_date)
        END AS max_date
    , CASE 
        WHEN a.[Appointment_Status_Roll_up] = 'active'
            AND a.appointment_date > a.last_direct_only_date
            THEN min(a.appointment_date)
        END AS min_date
FROM #Last_Direct_treatment A

In the unlikely event that you need those dates in a single column, note that case expressions allow for multiple sets of conditions to be considered, eg:

SELECT
      a.client_id
    , CASE 
        WHEN a.[appointment_status] = 'Complete'
            AND a.[Appointment_type_roll_up] = '1'
            THEN Max(a.appointment_date)
        WHEN a.[Appointment_Status_Roll_up] = 'active'
            AND a.appointment_date > a.last_direct_only_date
            THEN min(a.appointment_date)
        -- ELSE ...
        END AS min_or_max_date
FROM #Last_Direct_treatment A

These condition sets are evaluated top-to bottom, if one applies that determines the result and the condition sets that follow are not used. If no condition sets apply else may be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM