使用sql查询加入两个表

时间:2016-01-11 08:42:28

标签: sql-server

我有两张记录表,一张是temp,第二张是temps, 我想按部门更新状态,这在第二个表中提供,

我需要第一个表计数将在第二个表中更新NotYetTaken,WorkInProgress,根据状态完成....

例如, 护理部门将更新,Temps表中有多少已完成的待处理工作正在替换NULL ..

临时记录

STATUS                    DEPARTMENT                 COUNT

COMPLETED                 ASSEMBLY                   1
COMPLETED                 CARE                       18
NOT YET TAKEN             CARE                       1
WORK IN PROGRESS          CARE                       1
COMPLETED                 CARE(QQ)                   6
NOT YET TAKEN             CARE(QQ)                   1
COMPLETED                 HUMAN RESOURCES            2
COMPLETED                 INFORMATION TECHNOLOGY     9
COMPLETED                 MARKETING                  1
COMPLETED                 QUALITY                    9
WORK IN PROGRESS          QUALITY                    2
COMPLETED                 RESEARCH                   1
WORK IN PROGRESS                                     1
COMPLETED                 SUB CONTRACT               3

临时记录

NOTYETTAKEN WORKINPROGRESS  COMPLETED   UNDERTESTING    NOTPOSSIBLE DEPARTMENT

NULL    NULL    NULL    NULL    NULL    ASSEMBLY
NULL    NULL    NULL    NULL    NULL    CARE
NULL    NULL    NULL    NULL    NULL    CARE(QQ)
NULL    NULL    NULL    NULL    NULL    HUMAN RESOURCES
NULL    NULL    NULL    NULL    NULL    INFORMATION TECHNOLOGY
NULL    NULL    NULL    NULL    NULL    MARKETING
NULL    NULL    NULL    NULL    NULL    QUALITY
NULL    NULL    NULL    NULL    NULL    RESEARCH
NULL    NULL    NULL    NULL    NULL    
NULL    NULL    NULL    NULL    NULL    SUB CONTRACT

给出sql查询解决方案。

预期输出:

NOTYETTAKEN| WORKINPROGRESS |COMPLETED |UNDERTESTING |NOTPOSSIBLE   |DEPARTMENT
1   0   0   0   1   ASSEMBLY
18  7   0   4   18  CARE
6   6   6   3   6   CARE(QQ)
2   2   2   2   2   HUMAN RESOURCES
9   0   0   4   9   INFORMATION TECHNOLOGY
0   0   1   0   1   MARKETING
0   9   9   9   0   QUALITY
1   1   1   1   1   RESEARCH
1   0   0   1   1   
3   0   3   0   0   SUB CONTRACT

查询:

--DROP TABLE #Temp

CREATE TABLE #Temp
(NOTYETTAKEN Int, 
WORKINPROGRESS Int, 
COMPLETED Int, 
UNDERTESTING Int,
NOTPOSSIBLE Int,
STATUSNAME Nvarchar(50),
DEPARTMENTNAME Nvarchar(50),
COUNTS Int)

INSERT INTO #Temp
SELECT 0,0,0,0,0,StatusName,DepartmentName,COUNT(*) AS 'COUNTS' 
FROM Test..Task
GROUP BY  StatusName,DepartmentName

--DROP TABLE #Temps

CREATE TABLE #Temps
(NOTYETTAKEN Int, 
WORKINPROGRESS Int, 
COMPLETED Int, 
UNDERTESTING Int,
NOTPOSSIBLE Int,
DEPARTMENTNAME Nvarchar(50),
COUNTS Int)

INSERT INTO #Temps(DEPARTMENTNAME)
SELECT DISTINCT DEPARTMENTNAME FROM Test..Task

SELECT * FROM #Temp
SELECT * FROM #Temps

UPDATE a SET 
a.NOTYETTAKEN=b.NOTYETTAKEN,
a.WORKINPROGRESS=b.WORKINPROGRESS,
a.COMPLETED=b.COMPLETED,
a.UNDERTESTING=b.UNDERTESTING,
a.NOTPOSSIBLE=b.NOTPOSSIBLE
FROM #Temps a
INNER JOIN #Temp b 
ON a.DEPARTMENTNAME=b.DEPARTMENTNAME

1 个答案:

答案 0 :(得分:1)

您的表格结构未规范化。

我希望您使用STATUS注册表创建一个表格,如下所示:

CREATE TABLE STATUS
(id varchar(36), description varchar(50))

然后,您在temp表中引用了status表上的外键约束

但同时你可以试试这个:

UPDATE temps
SET NOTYETTAKEN =
    (SELECT [t.COUNT] FROM temp t WHERE T.department = temps.department
    AND t.status = 'NOT YET TAKEN'),
WORKINPROGRESS =
    (SELECT [t.COUNT] FROM temp t WHERE T.department = temps.department
    AND t.status = 'WORK IN PROGRESS'),
COMPLETED =
    (SELECT [t.COUNT] FROM temp t WHERE T.department = temps.department
    AND t.status = 'COMPLETED'),
UNDERTESTING =
    (SELECT [t.COUNT] FROM temp t WHERE T.department = temps.department
    AND t.status = 'UNDER TESTING'),
NOTPOSSIBLE =
    (SELECT [t.COUNT] FROM temp t WHERE T.department = temps.department
    AND t.status = 'NOT POSSIBLE')