再次感谢Gurwinder Singh的帮助。真的很感激。我正在编辑我的样本数据记录,并希望解决问题。
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不同的结果感兴趣。再次感谢。乔。
答案 0 :(得分:2)
我猜你想显示一组列的不同行中的per_id是否有不同的值。您可以使用窗口函数MIN
和MAX
来检查相同per_id的不同列中是否存在多个不同的值。
select
t.*,
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(PER_MISC) over (partition by per_id) = min(PER_MISC) over (partition by per_id) then 'Yes' else 'No' end as same_per_misc,
case when max(PER_CLASS) over (partition by per_id) = min(PER_CLASS) over (partition by per_id) then 'Yes' else 'No' end as same_per_class,
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
from #temp t;
答案 1 :(得分:0)
也许你可以尝试从表到自己进行内部联接。类似的东西:
SELECT * FROM TEMP T1 INNER JOIN TEMP T2 ON T1.PER_SS = T2.PER_SS AND T1.PER_ID = T2.PER_ID - etc。
您也可以尝试这样的事情。
SELECT T1。* FROM TEMP T1,TEMP T2 WHERE T1.PER_SS = T2.PER_SS AND T1.PER_ID = T2.PER_ID - etc。