首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将两个select语句连接到一个

将两个select语句连接到一个
EN

Stack Overflow用户
提问于 2017-10-10 11:19:04
回答 2查看 39关注 0票数 0

如何将这些语句连接到一起返回结果?

查询1:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-10 11:31:02

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

代码语言:javascript
复制
(Query 1)
union all
(Query 2)

对于您的情况,也许大小写和对联接条件的少量修改都会奏效。请试用以下方法:

代码语言:javascript
复制
select se.scheduledate,
    pd.accountnumber,
    pd.firstname,
    pd.middleinitial,
    pd.lastname,
    pd.address1,
    pd.address2,
    pd.city,
    pd.state,
    pd.zipcode,
    pd.dateofbirth,
    pd.sex,
    pd.hometelephone,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1name                     else pip.insurance2name end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1policynumber             else pip.insurance2policynumber end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1policygroupnumber        else pip.insurance2policygroupnumber end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantorfirstname       else pip.insurance2guarantorfirstname as insuredfirstname end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantormiddleinitial   else pip.insurance2guarantormiddleinitial as insuredmiddleinitial end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantorlastname        else pip.insurance2guarantorlastname as insuredlastname end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantoraddress1        else pip.insurance2guarantoraddress1 as insuredaddress1 end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantoraddress2        else pip.insurance2guarantoraddress2 as insuredaddress2 end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantorcity            else pip.insurance2guarantorcity as insuredcity end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantorstate           else pip.insurance2guarantorstate as insuredstate end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantorzipcode         else pip.insurance2guarantorzipcode as insuredzip end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantordateofbirth     else pip.insurance2guarantordateofbirth as insureddob end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantorsex             else pip.insurance2guarantorsex as insuredsex end,
    case when i.mnemonic = pip.insurance1mnemonic then pip.insurance1guarantortelephonenumber else pip.insurance2guarantortelephonenumber as insuredhomenumber end,
    i.eligibilitypayornumber
from patientdemographics pd
inner join patientinsuranceprofiles pip
on  pd.accountnumber = pip.patientaccountnumber
inner join scheduleentry se
on  pip.patientaccountnumber = se.patientaccount
inner join insurances i
on  i.mnemonic in (pip.insurance1mnemonic, pip.insurance2mnemonic)
where datediff(d, getdate(), scheduledate) = 1
    and pip.activeflag = 1
order by scheduledate asc
票数 0
EN

Stack Overflow用户

发布于 2017-10-10 11:24:10

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

代码语言:javascript
复制
SELECT ...
FROM ...
WHERE...

UNION ALL

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

您可以在这里阅读UNIONUNION ALL的差异:What is the difference between UNION and UNION ALL?

你也可以使用CTE -如果有一个列你可以加入,例如。

代码语言:javascript
复制
;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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46665439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档