[英]How to combine two select query with different columns
I know that union
can be done to combine two queries if they have same number and type of columns.我知道如果它们具有相同的列数和类型,则可以
union
两个查询。 But I've a condition where I've to combine two select statement with different tables and different columns though 1 table is common in both ie PatientAppointment .但是我有一个条件,我必须将两个 select 语句与不同的表和不同的列组合在一起,尽管 1 个表在两者中都很常见,即PatientAppointment 。 Here are the two statements:
下面是两个声明:
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
UNION联盟
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
The query is too complex and I can't create the data set also.查询太复杂,我也无法创建数据集。 Please help me with the approach.
请帮助我的方法。 Give me some suggestion at least.
至少给我一些建议。 I'll try it myself.
我会自己试试。
Consider your first query as Query1
and second query as Query2
, you can use simple join between these two.将您的第一个查询视为
Query1
并将第二个查询视为Query2
,您可以在这两者之间使用简单的连接。
As you said PatientAppointment
table is common in both, use its primary key( CDRId
) as joining between these two.正如您所说,
PatientAppointment
表在两者中都很常见,请使用其主键( CDRId
)作为这两者之间的连接。 So your query would look like.所以你的查询看起来像。
SELECT *
FROM ( Query1 ) AS table1
INNER JOIN ( Query2) AS table2 ON table1.CDRId = table2.CDRId;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.