SQL连接包含Select数据的案例结果

时间:2016-08-08 18:52:39

标签: sql sql-server join case union

我想要完成的是让案例的结果包括数据的结果。我的情况是1和2,它们转到一个表,产生2列ID |状态|状态说明。我想加入数据结果以及CASE的结果。

我有一个结果表和2个正在案例中加入的表。

@ZipCode    varchar(5),
@Tribal     varchar(5)

as

DECLARE @Status varchar(30)
SELECT @Status = CASE
when exists (SELECT ZipCodeCoverage.ZipCode, ZipCodeCoverage.State, ServicePlan, plan_name
 FROM [LifelineReference].[dbo].[ZipCodeCoverage]
    JOIN [VcareData].[dbo].[tbl_plan] ON [ZipCodeCoverage].[State]= [plan_state]

where ZipCode = @ZipCode and is_tribal = @Tribal and life_line = 'Y')   then '1' 
ELSE 2
 END
Select * FROM [LifelineReference].[dbo].[LifelinePlanStatusCodes]
    where ID = @Status

状态代码表

ID |   STATUS     | StatusDescription
1  |   Success    | Transaction May Proceed
2  |   FAIL       | Do not continue

我需要的结果

ID |  STATUS | StatusDescription | ZipCode | State |  ServicePlan | plan_name

3 个答案:

答案 0 :(得分:0)

我想我明白了。这就是我想出来的

@ZipCode    varchar(5),
@Tribal     varchar(5)

as

DECLARE @Status varchar(30)

 SELECT @Status = CASE
when exists (SELECT ZipCodeCoverage.ZipCode, ZipCodeCoverage.State, ServicePlan, plan_name
 FROM [LifelineReference].[dbo].[ZipCodeCoverage]
    JOIN [VcareData].[dbo].[tbl_plan] ON [ZipCodeCoverage].[State]=[plan_state]
       where ZipCode = @ZipCode and is_tribal = @Tribal and life_line = 'Y')  then '1' 
 ELSE 2
 end


 Select *
 ,(SELECT ZipCodeCoverage.ZipCode  
 FROM [LifelineReference].[dbo].[ZipCodeCoverage]
    JOIN [VcareData].[dbo].[tbl_plan] ON [ZipCodeCoverage].[State]=[plan_state]
       where ZipCode = @ZipCode and is_tribal = @Tribal and life_line = 'Y') as ZipCode
 ,(SELECT ZipCodeCoverage.State 
 FROM [LifelineReference].[dbo].[ZipCodeCoverage]
    JOIN [VcareData].[dbo].[tbl_plan] ON [ZipCodeCoverage].[State]=[plan_state]
       where ZipCode = @ZipCode and is_tribal = @Tribal and life_line = 'Y') as State
    ,(SELECT ServicePlan 
 FROM [LifelineReference].[dbo].[ZipCodeCoverage]
    JOIN [VcareData].[dbo].[tbl_plan] ON [ZipCodeCoverage].[State]=[plan_state]
       where ZipCode = @ZipCode and is_tribal = @Tribal and life_line = 'Y') as ServicePlan
  ,(SELECT plan_name
 FROM [LifelineReference].[dbo].[ZipCodeCoverage]
    JOIN [VcareData].[dbo].[tbl_plan] ON [ZipCodeCoverage].[State]=[plan_state]
       where ZipCode = @ZipCode and is_tribal = @Tribal and life_line = 'Y') as plan_name
FROM [LifelineReference].[dbo].[LifelinePlanStatusCodes]
    where ID = @Status

给我结果:

 ID Status  StatusDescription   ZipCode State   ServicePlan plan_name
  1 SUCCESS Service is Available    48322   MI  ABC Michigan Wireless XXXX Service Free250

答案 1 :(得分:0)

这里有一个更清晰的解决方案,它不会重复相同的SQL语句并在一个查询中执行。没有完全理解所涉及的表的字段,数据和它们之间的关系有点困难。我假设ZipCodeCoverage将邮政编码与州相关联,像is_tribal和life_line这样的字段是tbl_plan的一部分,而且LifelinePlanStatusCodes中存在状态2记录。

更新

下面的解决方案使用公用表表达式(CTE),以便不重复查询。但是,在更好地理解您要查找的内容之后,使用if exists(...)可能更清楚,选择您想要的结果,否则选择FAIL记录。这会重复一次查询的一部分,但意图也很明确。或者,我不知道你如何消费结果,但我倾向于在调用应用程序中处理失败条件 - 只返回成功记录(使用第一个查询),如果没有恢复记录将其视为失败。

在任何情况下,CTE解决方案都会在FoundPlan_CTE中获取您的计划并加入状态代码,但如果FoundPlan_CTE返回任何记录,则选择的状态代码为1(成功),如果FoundPlan_CTE未返回任何记录,则为2(失败)

with FoundPlan_CTE (ZipCode, State, ServicePlan, plan_name)
as
(
    select
        z.ZipCode
        ,z.State
        ,p.ServicePlan
        ,p.plan_name
    from
        [LifelineReference].[dbo].[ZipCodeCoverage] z
        inner join [VcareData].[dbo].[tbl_plan] p on p.plan_state = z.State
    where
        z.ZipCode = @ZipCode
        and p.is_tribal = @Tribal
        and p.life_line = 'Y'
)
select
    s.ID
    ,s.Status
    ,s.StatusDescription
    ,p.ZipCode
    ,p.State
    ,p.ServicePlan
    ,p.plan_name
from
    [LifelineReference].[dbo].[LifelinePlanStatusCodes] s
    left outer join FoundPlan_CTE p on 1 = 1
where
    s.ID = isnull((select top 1 1 from FoundPlan_CTE),2)

答案 2 :(得分:0)

这是1时的结果,只显示第1行。当2 a失败时应显示没有其他信息.. 当2个NO数据显示全部时...几乎就像在案例之后运行结果......

klass