通过匹配两列来计算是否具有某个数字

时间:2017-11-21 16:14:54

标签: sql-server

TABLE-A

EMP_ID  BENEFIT_Per   Yes/No
  A       20    
  A       40    
  A      100    
  B       30    
  B       30    
  C      100    
  C       10    
  C       20    
  D       30    
  D       40    
  D       50    

TABLE-A(有答案)

EMP_ID  BENEFIT_Per   Yes/No
  A       20            yes
  A       40            yes
  A      100            yes
  B       30            no
  B       30            no
  C      100            yes
  C       10            yes
  C       20            yes
  D       30            no
  D       40            no
  D       50            no

如果员工已获得100%的福利,是或否,如何更新(在同一张表中)?

4 个答案:

答案 0 :(得分:1)

update tableA set yes_no = case when max_benefit = 100 then 'yes' else 'no' end from (select emp_id, max(benefit_per) as max_benefit from tableA group by emp_id) t1 where t1.emp_id = tableA.emp_id 中找到最大值,如果它是100,则更新所有派生查询emp_id和tableA的emp_id匹配的列为yes:

service docker start
docker run --rm -it -v /home/you/projets/project:/usr/local/share/common-lisp/source  daewok/lisp-devel:latest bash

rextester示例

答案 1 :(得分:0)

你去吧

DECLARE @tab TABLE (EMP_ID CHAR(1), BENEFIT_Per INT, [Yes/No] NVARCHAR(3) NULL )
INSERT INTO @tab
(EMP_ID, BENEFIT_Per)
SELECT 'A', 20 UNION ALL
SELECT 'A', 40 UNION ALL
SELECT 'A',100 UNION ALL
SELECT 'B', 30 UNION ALL
SELECT 'B', 30 UNION ALL
SELECT 'C',100 UNION ALL
SELECT 'C', 10 UNION ALL
SELECT 'C', 20 UNION ALL
SELECT 'D', 30 UNION ALL
SELECT 'D', 40 UNION ALL
SELECT 'D', 50

;WITH cteZ
AS(
    SELECT T.EMP_ID
         , MAX(T.BENEFIT_Per) 'MxBen'
    FROM @tab T 
    GROUP BY T.EMP_ID
)
UPDATE T
SET T.[Yes/No] = CASE WHEN Z.MxBen = 100 THEN 'Yes' ELSE 'No' END
FROM @tab T
LEFT JOIN  cteZ Z ON Z.EMP_ID = T.EMP_ID


Output

enter image description here

答案 2 :(得分:0)

调用原始表t1。您首先想要这个子表:

{
"dependentFileProviders": {
"addedExtension": {},
"pathSegment": {
  "*": [
    ".js",
    ".css",
    ".html",
    ".htm",
    ".less",
    ".scss",
    ".coffee",
    ".iced",
    ".config",
    ".cs",
    ".vb",
    ".json"
  ]
},
"extensionToExtension": {
  ".js": [
    ".coffee",
    ".iced",
    ".ts",
    ".tsx",
    ".jsx"
  ],
  ".ts": [
    ".html"
  ],
  ".css": [
    ".less",
    ".scss",
    ".sass",
    ".styl",
    ".html"
  ],
  ".html": [
    ".md",
    ".mdown",
    ".markdown",
    ".mdwn",
  ],
  ".map": [
    ".js",
    ".css"
  ],
  ".svgz": [
    ".svg"
  ],
  ".designer.cs": [
    ".resx"
  ],
  ".cs.d.ts": [
    ".cs"
  ]
},
"fileToFile": {
  ".bowerrc": [
    "bower.json"
  ],
  ".npmrc": [
    "package.json"
  ],
  "npm-shrinkwrap.json": [
    "package.json"
  ],
  "yarn.lock": [
    "package.json"
  ],
  ".yarnclean": [
    "package.json"
  ],
  ".yarnignore": [
    "package.json"
  ],
  ".yarn-integrity": [
    "package.json"
  ],
  ".yarnrc": [
    "package.json"
  ]
},
"filePartToExtension": {
  "-vsdoc.js": [
    ".js"
  ]
},
"allExtensions": {
  "*": [
    ".tt"
  ]
}
}
}

然后加入原始表格

select EMP_ID, max(BENEFIT_Per) bene_max from t1 group by EMP_ID

答案 3 :(得分:0)

UPDATE  A
SET     A.[Yes/No] = CASE WHEN EXISTS(
                                        SELECT TOP 1 1 
                                        FROM [Table] 
                                        WHERE EMP_ID = A.EMP_ID AND BENEFIT_Per = 100
                                    ) THEN 'Yes' ELSE 'NO' END
FROM    [Table] A
WHERE   1=1