SQL中的CASE和CONCAT

时间:2019-01-03 22:52:59

标签: sql sql-server

我有一个场景,其中有2张桌子。

表1:

enter image description here

表2:

enter image description here

因此,CID是客户端ID,PID是父ID。有4个条件,我已经使每个CID都符合每个条件:

  1. 如果表1的客户ID,父ID和SSC相同,并且表2上没有结束日期,则将表1和表2的数据连接起来

  2. 如果table1客户端ID,父ID和SSC相同,并且两个表上都没有该CID的结束日期,则显示表1的数据

  3. 如果表1的客户ID,父ID和SSC相同,并且表1中没有该CID的结束日期,但是表2中存在该CID的结束日期,则将两个表的数据连接起来

  4. 如果table1客户端ID,SSC相同但PID不同,则在两行中分别显示数据。

下面是所需的输出:

enter image description here

上个月的链接:

https://rextester.com/GBY12899

我仍在试用代码,并将很快发布。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:1)

您的条件和所需的输出之间存在矛盾:

  

如果table1客户端ID,父ID和SSC相同,并且没有结尾   两个表上该CID的日期,然后显示表1的数据

所以在1003/1116行中,ssc的值应该是OR20121005,对吧?
我基于rextester链接中的数据将其作为解决方案:

select 
 t1.CID CID, t1.PID PID,
 case 
   when (t1.End_Date is null) and (t2.End_Date is null) then t1.Data 
   when (t1.End_Date is null) or (t2.End_Date is null) then t1.Data + ' ' + t2.Data
 end SSC
from #table1 t1 inner join #table2 t2 on t1.CID = t2.CID and t1.SSC = t2.SSC and t1.PID = t2.PID
union
select t1.CID, t1.PID, t1.Data SSC 
from #table1 t1 
where exists (select 1 from #table2 t2 where t1.CID = t2.CID and t1.SSC = t2.SSC and t1.PID <> t2.PID)
union
select t2.CID, t2.PID, t2.Data SSC 
from #table2 t2 
where exists (select 1 from #table1 t1 where t1.CID = t2.CID and t1.SSC = t2.SSC and t1.PID <> t2.PID)

请参见demo

答案 1 :(得分:1)

使用CTE的另一种可能的解决方案。

;with dataset as(
select 
    t1.* 
    ,case when (t1.End_date is null and t2.End_date is null) then t1.data
        when  (t2.End_date is null or t1.End_date is null) then t1.data + ' - ' + t2.data 
        else null 
    end AS DataConditions


from #table1 t1
    left join #table2 t2    on t1.CID = t2.CID
                                AND t1.PID = t2.PID
                                AND t1.SSC = t2.SSC

union all

select 
    t2.*
    ,case when (t1.End_date is null and t2.End_date is null) then t1.data
        when  (t2.End_date is null or t1.End_date is null) then t1.data + ' - ' + t2.data 
        else null 
    end

from #table2 t2
    left join #table1 t1    on t1.CID = t2.CID
                                AND t1.PID = t2.PID
                                AND t1.SSC = t2.SSC

WHERE t1.PID IS NULL
)

select
    CID, PID, Coalesce(DataConditions, dataset.Data) AS SSC
from dataset

Demo

答案 2 :(得分:1)

非常遗憾的是,您的问题有错误在所有4个问题中,第1个问题是毫无意义的“如果table1客户端ID,父ID和SSC相同”,因为它们是ID,因此在使用Join时它们是相同的。

我试图更改您的问题并尝试解决

我回答了以下两个问题,但老实说,这只是在CONCAT列表和条件中添加列的问题,否则您可以使用此查询来获取答案

查询情况:1_如果表1中有EndDate,表2中没有EndDate,则将Table1 SSC + Table1 StartDate + Table1 EndDate串联起来

2_如果两个表上都没有EndDate,则在Table2数据列中显示Table1 SSC值+ Table2 StartDate空间值

SELECT T1.CID
,T1.PID
,CASE
    WHEN T1.EndDate IS NOT NULL AND ISNULL(T2.EndDate,'') = '' THEN CONCAT(T1.SSC, T1.StartDate,T1.EndDate)
    WHEN ISNULL(T1.EndDate,'') =  '' AND ISNULL(T2.EndDate,'') =  ''  THEN CONCAT(T1.SSC, T2.StartDate, '  ', T2.Data)
ELSE NULL END AS SSC


FROM temp.dbo.TABLE1 AS T1
LEFT JOIN temp.dbo.TABLE2 AS T2 ON T1.CID = T2.CID AND T1.PID = T2.PID AND T1.SSC = T2.SSC

它给出以下结果

enter image description here

答案 3 :(得分:1)

这会在演示数据上产生您正在寻找的确切结果。

select 
    t1.cid
    ,ot.pid
    ,ot.ssc 
from #table1 t1
outer apply (
    SELECT 
        t1.PID
        ,case when t2.pid <> t1.pid or (isnull(t1.end_date, t2.end_date) is null) then t1.data   -- rule 2 and part A of rule 4
            else concat(t1.data, '     ', t2.data)      -- rules 1 and 3
        end as SSC
    FROM #table2 t2
    where t2.cid = t1.cid
     and t2.ssc = t1.ssc

    union 

    -- this is to get the 2nd row for CID 1004 - part B of rule 4
    SELECT 
        t2.PID
        ,t2.data as SSC
    FROM #table2 t2
    where t2.cid = t1.cid
     and t2.ssc = t1.ssc
     and t2.pid <> t1.pid

) ot

但是,您没有描述2个表之间的关系,所以我不确定它是否适用于实际数据-1表中CID + SSC组合的记录是否比表中的记录多?其他? 例如,Table2可能看起来像这样-为CID 1004添加了另一行:

create table #table2(CID int,PID int,SSC varchar(5),Start_date varchar(30),End_date varchar(30), Data varchar(20))
insert #table2
values
(1001,  1333,   'OP','20111015',NULL,'OP20111015'),
(1002,  1245,   'OR','20131005',NULL,'OR20131005'),
(1003,  1116,   'OP','20131415',NULL,'OP2013141520131516'),
(1004,  1235,   'OP','20121015',NULL,'OP20121015'),
(1004,  1234,   'OP','20151015',NULL,'WHAT')