如何使用SQL在4个不同的列中查找重复值

时间:2018-03-19 12:20:53

标签: sql

我想使用SQL找到下图中提到的最后一列,有什么帮助吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那就简单了:

; WITH CTE AS (
SELECT UserName,
       Case WHEN Workflow1 = '' THEN 0 ELSE 1 END AS Workflow1,
       Case WHEN Workflow2 = '' THEN 0 ELSE 1 END AS Workflow2,
       Case WHEN Workflow3 = '' THEN 0 ELSE 1 END AS Workflow3,
       Case WHEN Workflow4 = '' THEN 0 ELSE 1 END AS Workflow4 
FROM Table )

 SELECT UserName,
        Workflow1,
        Workflow2,
        Workflow3,
        Workflow4, 
        CAST (Workflow1 AS INT) + 
        CAST (Workflow2 AS INT) + 
        CAST (Workflow3 AS INT) + 
        CAST (Workflow4 AS INT) AS Count_Avail
 FROM CTE AS C
 INNER JOIN Table AS T ON C.UserName = T.UserName