Microsoft SQL Server中的案例声明

时间:2017-05-18 13:06:24

标签: sql sql-server

我在下面有一些样本记录。

CREATE TABLE #TEMP
(
PER_ID INT,
PER_SS VARCHAR (11),
CHARGE_CD VARCHAR (25),
PER_CODE VARCHAR (20),
FILE_DT DATETIME,
PER_CASE_ID INT,
PER_PS VARCHAR (5),
CREATE_DT DATETIME


)

INSERT INTO #TEMP VALUES('6385789','9801745','21 B 1325(Q)(4)','NMT785989','11/11/2016 12:00 AM','957800','IDA','11/12/2016 11:00 PM')
INSERT INTO #TEMP VALUES('6385789','9801745','21 B 1325(Q)(4)','NMT785989','11/11/2016 12:00 AM','698080','QBC','11/12/2016 11:00 PM')
INSERT INTO #TEMP VALUES('6388788','9801746','21 B 1325(Q)(4)','NMT785990','11/11/2016 12:00 AM','957801','CAN','11/12/2016 11:00 PM')
INSERT INTO #TEMP VALUES('6388788','9801746','21 B 1326(Q)(5)','NMT785990','11/11/2016 12:00 AM','698081','ADA','11/12/2016 11:00 PM')
INSERT INTO #TEMP VALUES('6385790','9858888','21 B 1326(Q)(5)','NMT785990','11/11/2016 12:00 AM','698888','ADA','11/12/2016 11:00 PM')
INSERT INTO #TEMP VALUES('6385790','9858888','21 B 1326(Q)(5)','NMT785990','11/11/2016 12:00 AM','698888','CAN','11/12/2016 11:00 PM')



    SELECT 
    --the case station with partition script was written by Gurwinder Singh.
    CASE WHEN MAX(PER_ID) OVER (PARTITION BY PER_ID) = MIN(PER_ID) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_ID,
    CASE WHEN MAX(PER_SS) OVER (PARTITION BY PER_ID) = MIN(PER_SS) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_SS,
    CASE WHEN MAX(CHARGE_CD) OVER (PARTITION BY PER_ID) = MIN(CHARGE_CD) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_CHARGE_CD,
    CASE WHEN MAX(PER_CODE) OVER (PARTITION BY PER_ID) = MIN(PER_CODE) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_CODE,
    CASE WHEN MAX(FILE_DT) OVER (PARTITION BY PER_ID) = MIN(FILE_DT) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_FILE_DT,
    PER_ID, 
    PER_SS, 
    CHARGE_CD,
    PER_CASE_ID,
    PER_PS,
    PER_CODE,
    FILE_DT
    FROM #TEMP 

有没有办法可以过滤掉两条记录PER_CASE_ID =(698888)。基本上,我对返回这两条记录不感兴趣,因为PER_CASE_ID是相同的。我只对结果感兴趣 PER_CASE_ID不同的地方。

4 个答案:

答案 0 :(得分:1)

假设他们通过PER_ID链接,那么

SELECT ...
    FROM #TEMP t
WHERE EXISTS (
             SELECT * FROM #TEMP t2 
               WHERE t2.PER_ID = t.PER_ID 
               AND t2.PER_CASE_ID <> t.PER_CASE_ID
             )

答案 1 :(得分:1)

使用Row_Number()可以删除重复项

 SELECT *
    From
    (
    SELECT 
    --the case station with partition script was written by Gurwinder Singh.
    CASE WHEN MAX(PER_ID) OVER (PARTITION BY PER_ID) = MIN(PER_ID) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_ID,
    CASE WHEN MAX(PER_SS) OVER (PARTITION BY PER_ID) = MIN(PER_SS) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_SS,
    CASE WHEN MAX(CHARGE_CD) OVER (PARTITION BY PER_ID) = MIN(CHARGE_CD) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_CHARGE_CD,
    CASE WHEN MAX(PER_CODE) OVER (PARTITION BY PER_ID) = MIN(PER_CODE) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_CODE,
    CASE WHEN MAX(FILE_DT) OVER (PARTITION BY PER_ID) = MIN(FILE_DT) OVER (PARTITION BY PER_ID) THEN 'YES' ELSE 'NO' END AS SAME_PER_FILE_DT,
    PER_ID, 
    PER_SS, 
    CHARGE_CD,
    PER_CASE_ID,
    PER_PS,
    PER_CODE,
    FILE_DT
    ,ROW_NUMBER()OVER(Partition by PER_CASE_ID Order by PER_CASE_ID) As Seq
    FROM #TEMP 
    )dt
WHERE dt.Seq=1

答案 2 :(得分:0)

如果PER_PS值不重要且您始终可以获取此字段的所有重复值中较大或较小的值,则可以省略PER_PS。但是,如果PER_ID和PER_PS之外还有其他关系会产生多个记录,那么这个解决方案会变得更加麻烦。

varchar

答案 3 :(得分:0)

尝试:

with cte as (select PER_CASE_ID from #TEMP group by PER_CASE_ID having count(*) = 1)
select ... from #TEMP as src 
where exists (select * from cte where cte.PER_CASE_ID = src.PER_CASE_ID)
order by ...;