加入两个 SQL select 语句并将两个结果合并为一个

Join two SQL select statements and get both results in to single

我想加入这两个 SQL select 语句并将两个结果作为一个结果。

--1st select statement
SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
FROM TT_TaskAssign TA
INNER JOIN HR_EmployeeMaster1 EM
on TA.DevID=EM.EmpNo 
INNER JOIN dbo.TT_TaskAsignKPI TAKPI
ON TAKPI.AssignID = TA.AssignID 
WHERE TA.DevID='1435'

--2nd select statement
SELECT  TT_TaskAssign.DevID AS DevID  ,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
FROM TT_TaskAssign inner join Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code Inner Join HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
WHERE TT_TaskAssign.DevID='1435'
GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName
ORDER BY SUM(CONVERT(int, TAKPI.KPINum)) DESC

我是按照下面的方式做的

SELECT T1.*, T2.*
FROM 
  (SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
  FROM TT_TaskAssign TA
  INNER JOIN HR_EmployeeMaster1 EM
  on TA.DevID=EM.EmpNo 
  INNER JOIN dbo.TT_TaskAsignKPI TAKPI
  ON TAKPI.AssignID = TA.AssignID 
  WHERE TA.DevID='1435') T1

INNER JOIN
  (SELECT  TT_TaskAssign.DevID AS DevID  ,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
   FROM TT_TaskAssign inner join Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code Inner Join HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
  WHERE TT_TaskAssign.DevID='1435'
  GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName
  ORDER BY SUM(CONVERT(int, TAKPI.KPINum)) DESC) T2

ON T1.DevID=T2.DevID 

现在出现以下错误

    Msg 1033, Level 15, State 1, Line 17 
    The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

请有人帮我解决这个错误

正如错误信息所说。子查询中不允许 ORDER BY。如果您需要 order by 子句,则必须将其用于 join.

的整个结果

从第二个查询中删除 order by

SELECT T1.*, T2.*
FROM 
  (SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
  FROM TT_TaskAssign TA
  INNER JOIN HR_EmployeeMaster1 EM
  on TA.DevID=EM.EmpNo 
  INNER JOIN dbo.TT_TaskAsignKPI TAKPI
  ON TAKPI.AssignID = TA.AssignID 
  WHERE TA.DevID='1435') T1

INNER JOIN
  (SELECT  TT_TaskAssign.DevID AS DevID  ,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
   FROM TT_TaskAssign inner join Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code Inner Join HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
  WHERE TT_TaskAssign.DevID='1435'
  GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName
  ) T2

ON T1.DevID=T2.DevID

您可以将 order by 移出内部查询

SELECT T1.*, T2.*
FROM 
  (SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
  FROM TT_TaskAssign TA
  INNER JOIN HR_EmployeeMaster1 EM
  on TA.DevID=EM.EmpNo 
  INNER JOIN dbo.TT_TaskAsignKPI TAKPI
  ON TAKPI.AssignID = TA.AssignID 
  WHERE TA.DevID='1435') T1
INNER JOIN
  (SELECT  TT_TaskAssign.DevID AS DevID  ,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
   FROM TT_TaskAssign inner join Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code Inner Join HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
  WHERE TT_TaskAssign.DevID='1435'
  GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName
  ) T2
ON T1.DevID=T2.DevID
ORDER BY T2.KPINum DESC

使用 (#) 个临时表来存储中间结果..

试试这个...

        SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
        INTO    #t1
        FROM TT_TaskAssign TA
        INNER JOIN HR_EmployeeMaster1 EM
        on TA.DevID=EM.EmpNo 
        INNER JOIN dbo.TT_TaskAsignKPI TAKPI
        ON TAKPI.AssignID = TA.AssignID 
        WHERE TA.DevID='1435'


        SELECT  TT_TaskAssign.DevID AS DevID  ,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
        INTO        #t2
        FROM TT_TaskAssign inner join Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code Inner Join HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
        WHERE TT_TaskAssign.DevID='1435'
        GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName
        ORDER BY SUM(CONVERT(int, TAKPI.KPINum)) DESC

        SELECT  T1.*, T2.*
        FROM        #t1 T1
        INNER JOIN  #t2 T2
            ON T1.DevID=T2.DevID
SELECT T1.*, T2.*
FROM 
  (SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
  FROM TT_TaskAssign TA
  INNER JOIN HR_EmployeeMaster1 EM
  on TA.DevID=EM.EmpNo 
  INNER JOIN dbo.TT_TaskAsignKPI TAKPI
  ON TAKPI.AssignID = TA.AssignID 
  WHERE TA.DevID='1435') T1

INNER JOIN
  (SELECT  TT_TaskAssign.DevID AS DevID  ,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
   FROM TT_TaskAssign inner join Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code Inner Join HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
  WHERE TT_TaskAssign.DevID='1435'
  GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName) T2

ON T1.DevID=T2.DevID
ORDER BY SUM(CONVERT(int, T2.KPINum)) DESC

您也可以使用 CTE 加入,并将 ORDER BY 移至主决赛 SELECT。

;WITH T1
AS
(
    SELECT TA.DevID, TA.Task, TA.AssignDate,  TA.DevStart,TA.TaskType, TA.EstimateTime, TA.Status, TA.DevFinish, TAKPI.actAsignTime,  TAKPI.actTime, TAKPI.KPI
        FROM TT_TaskAssign TA
        INNER JOIN HR_EmployeeMaster1 EM on TA.DevID=EM.EmpNo 
        INNER JOIN dbo.TT_TaskAsignKPI TAKPI  ON TAKPI.AssignID = TA.AssignID 
    WHERE TA.DevID='1435'
),
T2 
AS
(
    SELECT  TT_TaskAssign.DevID AS DevID  ,
        HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName as Developer, 
        SUM( CASE WHEN TT_TaskAssign.Status='Done' THEN 1 ELSE 0 END ) as DoneProjects , 
        SUM( CASE WHEN TT_TaskAssign.Status='Developing' THEN 1 ELSE 0 END ) as DevelopingPrjects, 
        SUM( CASE WHEN TT_TaskAssign.Status='Assign' THEN 1 ELSE 0 END ) as AssignedPending, 
        SUM(CONVERT(int, TAKPI.KPINum)) AS KPINum 
    FROM TT_TaskAssign 
        INNER JOIN Project_Master on TT_TaskAssign.ProID=Project_Master.Project_Code 
        INNER JOIN HR_EmployeeMaster1 on TT_TaskAssign.DevID=HR_EmployeeMaster1.EmpNo 
        INNER JOIN dbo.TT_TaskAsignKPI TAKPI ON TAKPI.AssignID = TAKPI.AssignID
    WHERE TT_TaskAssign.DevID='1435'
    GROUP BY  TT_TaskAssign.DevID,HR_EmployeeMaster1.EmpFirstName+' '+HR_EmployeeMaster1.EmpMiddleName+' '+HR_EmployeeMaster1.EmpLastName   
)
SELECT T1.*, T2.*
FROM T1 
    JOIN T2 ON T1.DevID=T2.DevID
ORDER BY T2.KPINum DESC