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

时间:2015-04-10 07:33:36

标签: sql sql-server sql-server-2005

我想加入这两个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.

请有人帮我解决此错误

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

从第二个查询中删除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

答案 2 :(得分:1)

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

试试这个......

        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

答案 3 :(得分:0)

您可以将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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

您也可以通过使用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