如何将两个 select 查询与不同的列组合起来
How to combine two select query with different columns
我知道 union
可以合并两个具有相同数量和类型的列的查询。但是我有一个条件,我必须将两个 select 语句与不同的 table 和不同的列组合在一起,尽管 1 table 在两者中都很常见,即 PatientAppointment。以下是两个声明:
select p.CDRId, p.Gender,p.MRN,p.DoB as DOB,pa.AppointmentDateTime,cn.Description,ehv.ProgramName,
cn.CreatedBy as CareTeamStaffMember,cn.Profile as Role,cn.Title as Credentials,
date_format(pa.AppointmentDateTime, '%Y-%m') BillingMonth,
((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) +
(case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes
from Patient p inner join EnrollmentHistoryView ehv on ehv.CDRId = p.CDRId
inner join ClinicalNote cn on cn.CDRId = p.CDRId
inner join PatientAppointment pa on pa.CDRId = p.CDRId
where p.CDRId='9493b505-03b9-46a0-b009-99b34f7a5d41'
and ehv.ProgramName!='N/A'
group by p.CDRId, p.Gender, p.MRN, p.Dob, pa.AppointmentDateTime,cn.Description,cn.CreatedBy,cn.Profile,cn.Title,pa.Duration,ehv.ProgramName
联盟
SELECT AppointmentDateTime,
duration,
minutes,
CASE WHEN @prev_month != BillingMonth
THEN total >= 20
WHEN @prev_total < 20
THEN 1
ELSE 0
END 99457Elig,
CASE WHEN @prev_month != BillingMonth
THEN total >= 40
WHEN @prev_total < 40
THEN 1
ELSE 0
END 99458Elig,
@prev_month := BillingMonth BillingMonth,
@prev_total := total total
FROM (select AppointmentDateTime,
duration,
@cur_dur := ((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) +
(case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes,
CASE WHEN @year_month = date_format(AppointmentDateTime, '%Y-%m')
THEN @cum_sum := @cum_sum + @cur_dur
ELSE @cum_sum := @cur_dur
END total,
@year_month := date_format(AppointmentDateTime, '%Y-%m') BillingMonth
from PatientAppointment, (SELECT @year_month:='', @cum_sum:=0, @cur_dur:=0) variables
ORDER BY AppointmentDateTime) subquery,
(SELECT @prev_month:=0, @prev_total:=0) variable
ORDER BY AppointmentDateTime
查询太复杂,我也无法创建数据集。请帮助我的方法。至少给我一些建议。我自己试试。
将您的第一个查询视为 Query1
,将第二个查询视为 Query2
,您可以在这两个查询之间使用简单连接。
如你所说 PatientAppointment
table 在两者中都很常见,使用它的主键(CDRId
)作为这两者之间的连接。所以你的查询看起来像。
SELECT *
FROM ( Query1 ) AS table1
INNER JOIN ( Query2) AS table2 ON table1.CDRId = table2.CDRId;
我知道 union
可以合并两个具有相同数量和类型的列的查询。但是我有一个条件,我必须将两个 select 语句与不同的 table 和不同的列组合在一起,尽管 1 table 在两者中都很常见,即 PatientAppointment。以下是两个声明:
select p.CDRId, p.Gender,p.MRN,p.DoB as DOB,pa.AppointmentDateTime,cn.Description,ehv.ProgramName,
cn.CreatedBy as CareTeamStaffMember,cn.Profile as Role,cn.Title as Credentials,
date_format(pa.AppointmentDateTime, '%Y-%m') BillingMonth,
((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) +
(case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes
from Patient p inner join EnrollmentHistoryView ehv on ehv.CDRId = p.CDRId
inner join ClinicalNote cn on cn.CDRId = p.CDRId
inner join PatientAppointment pa on pa.CDRId = p.CDRId
where p.CDRId='9493b505-03b9-46a0-b009-99b34f7a5d41'
and ehv.ProgramName!='N/A'
group by p.CDRId, p.Gender, p.MRN, p.Dob, pa.AppointmentDateTime,cn.Description,cn.CreatedBy,cn.Profile,cn.Title,pa.Duration,ehv.ProgramName
联盟
SELECT AppointmentDateTime,
duration,
minutes,
CASE WHEN @prev_month != BillingMonth
THEN total >= 20
WHEN @prev_total < 20
THEN 1
ELSE 0
END 99457Elig,
CASE WHEN @prev_month != BillingMonth
THEN total >= 40
WHEN @prev_total < 40
THEN 1
ELSE 0
END 99458Elig,
@prev_month := BillingMonth BillingMonth,
@prev_total := total total
FROM (select AppointmentDateTime,
duration,
@cur_dur := ((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) +
(case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes,
CASE WHEN @year_month = date_format(AppointmentDateTime, '%Y-%m')
THEN @cum_sum := @cum_sum + @cur_dur
ELSE @cum_sum := @cur_dur
END total,
@year_month := date_format(AppointmentDateTime, '%Y-%m') BillingMonth
from PatientAppointment, (SELECT @year_month:='', @cum_sum:=0, @cur_dur:=0) variables
ORDER BY AppointmentDateTime) subquery,
(SELECT @prev_month:=0, @prev_total:=0) variable
ORDER BY AppointmentDateTime
查询太复杂,我也无法创建数据集。请帮助我的方法。至少给我一些建议。我自己试试。
将您的第一个查询视为 Query1
,将第二个查询视为 Query2
,您可以在这两个查询之间使用简单连接。
如你所说 PatientAppointment
table 在两者中都很常见,使用它的主键(CDRId
)作为这两者之间的连接。所以你的查询看起来像。
SELECT *
FROM ( Query1 ) AS table1
INNER JOIN ( Query2) AS table2 ON table1.CDRId = table2.CDRId;