将两个select语句合并为一个

时间:2017-10-10 11:19:05

标签: sql-server sql-server-2014

如何加入这些语句以将结果一起返回?

查询1:

select scheduleentry.scheduledate,
    patientdemographics.accountnumber,
    patientdemographics.firstname,
    patientdemographics.middleinitial,
    patientdemographics.lastname,
    patientdemographics.address1,
    patientdemographics.address2,
    patientdemographics.city,
    patientdemographics.state,
    patientdemographics.zipcode,
    patientdemographics.dateofbirth,
    patientdemographics.sex,
    patientdemographics.hometelephone,
    patientinsuranceprofiles.insurance1name,
    patientinsuranceprofiles.insurance1policynumber,
    patientinsuranceprofiles.insurance1policygroupnumber,
    patientinsuranceprofiles.insurance1guarantorfirstname as insuredfirstname,
    patientinsuranceprofiles.insurance1guarantormiddleinitial as insuredmiddleinitial,
    patientinsuranceprofiles.insurance1guarantorlastname as insuredlastname,
    patientinsuranceprofiles.insurance1guarantoraddress1 as insuredaddress1,
    patientinsuranceprofiles.insurance1guarantoraddress2 as insuredaddress2,
    patientinsuranceprofiles.insurance1guarantorcity as insuredcity,
    patientinsuranceprofiles.insurance1guarantorstate as insuredstate,
    patientinsuranceprofiles.insurance1guarantorzipcode as insuredzip,
    patientinsuranceprofiles.insurance1guarantordateofbirth as insureddob,
    patientinsuranceprofiles.insurance1guarantorsex as insuredsex,
    patientinsuranceprofiles.insurance1guarantortelephonenumber as insuredhomenumber,
    insurances.eligibilitypayornumber
from patientdemographics
inner join patientinsuranceprofiles
on  patientdemographics.accountnumber = patientinsuranceprofiles.patientaccountnumber
inner join scheduleentry
on  patientinsuranceprofiles.patientaccountnumber = scheduleentry.patientaccount
inner join insurances
on  patientinsuranceprofiles.insurance1mnemonic = insurances.mnemonic
where datediff(d, getdate(), scheduledate) = 1
    and patientinsuranceprofiles.activeflag = 1
order by scheduledate asc

查询2:

select scheduleentry.scheduledate,
    patientdemographics.accountnumber,
    patientdemographics.firstname,
    patientdemographics.middleinitial,
    patientdemographics.lastname,
    patientdemographics.address1,
    patientdemographics.address2,
    patientdemographics.city,
    patientdemographics.state,
    patientdemographics.zipcode,
    patientdemographics.dateofbirth,
    patientdemographics.sex,
    patientdemographics.hometelephone,
    patientinsuranceprofiles.insurance2name,
    patientinsuranceprofiles.insurance2policynumber,
    patientinsuranceprofiles.insurance2policygroupnumber,
    patientinsuranceprofiles.insurance2guarantorfirstname as insuredfirstname,
    patientinsuranceprofiles.insurance2guarantormiddleinitial as insuredmiddleinitial,
    patientinsuranceprofiles.insurance2guarantorlastname as insuredlastname,
    patientinsuranceprofiles.insurance2guarantoraddress1 as insuredaddress1,
    patientinsuranceprofiles.insurance2guarantoraddress2 as insuredaddress2,
    patientinsuranceprofiles.insurance2guarantorcity as insuredcity,
    patientinsuranceprofiles.insurance2guarantorstate as insuredstate,
    patientinsuranceprofiles.insurance2guarantorzipcode as insuredzip,
    patientinsuranceprofiles.insurance2guarantordateofbirth as insureddob,
    patientinsuranceprofiles.insurance2guarantorsex as insuredsex,
    patientinsuranceprofiles.insurance2guarantortelephonenumber as insuredhomenumber,
    insurances.eligibilitypayornumber
from patientdemographics
inner join patientinsuranceprofiles
on  patientdemographics.accountnumber = patientinsuranceprofiles.patientaccountnumber
inner join scheduleentry
on  patientinsuranceprofiles.patientaccountnumber = scheduleentry.patientaccount
inner join insurances
on  patientinsuranceprofiles.insurance2mnemonic = insurances.mnemonic
where datediff(d, getdate(), scheduledate) = 1
    and patientinsuranceprofiles.activeflag = 1
order by scheduledate asc

2 个答案:

答案 0 :(得分:0)

您可以在两个查询之间使用UNION ALL,例如

SELECT ...
FROM ...
WHERE...

UNION ALL

SELECT ...
FROM ....
WHERE...

您可以在此处了解UNIONUNION ALL中的差异:What is the difference between UNION and UNION ALL?

您也可以使用CTE - 如果您有可以加入的列,例如

;WITH Query1 AS
(
 SELECT Column1
 FROM ...
)
,Query2 AS
( 
 SELECT Column1
 FROM ...
)
SELECT     *
FROM       Query1 AS Q1
INNER JOIN Query2 AS Q2 ON Q1.Column1 = Q2.Column1

答案 1 :(得分:0)

一种方法是使用UNION ALL方法,如其他人建议的那样。

ServletFilter

对于您的情况,可能会对连接条件进行大小写修改。请尝试以下方法:

(Query 1)
union all
(Query 2)