合并并细化两个查询结果sql server

时间:2014-06-10 04:02:10

标签: sql sql-server sql-server-2008

我有两个查询请告诉我如何合并这两个查询,以及如何根据GroupID优化这些查询.Merge查询。

                      (SELECT     count(idlee.ObjectId) AS 'Count', idlee.GroupId, idlee.Name
                        FROM          (SELECT     CONVERT(int, Sum(idle.distance)) AS distance, idle.ObjectId, idle.GroupId, idle.Name
                                                FROM          (SELECT     Message.ObjectId, fn_GpsUtil_Distance(Message.x, Message.y, lead(Message.x)
                                                                                                OVER (partition BY Message.objectid
                                                                        ORDER BY Message.GpsTime), lead(Message.y) OVER (partition BY Message.objectid
                                                ORDER BY Message.GpsTime)) AS distance, [Group].GroupId, [Group].Name
                        FROM          [Group] INNER JOIN
                                               GroupObject ON [Group].GroupId = GroupObject.GroupId INNER JOIN
                                               Message ON GroupObject.ObjectId = Message.ObjectId INNER JOIN
                                               Object ON GroupObject.ObjectId = Object.ObjectId
                        WHERE      (Object.Enabled = 1) AND (Object.ClientId = 5) AND (Message.GpsTime >= GETDATE() - 1) AND 
                                               (Message.GpsTime <= GETDATE())) AS idle
     GROUP BY idle.ObjectId, idle.GroupId, idle.Name) AS idlee
     WHERE     idlee.distance < 10
    GROUP BY idlee.GroupId, idlee.Name) 

输出

  Count  GroupID  Group
      36    15  DC-1
      30    16  DC-2
      13    17  DC-3
      64    13  LC-1
      16    14  LC-2

我用来检索数据的第二个查询

        (SELECT     count(idlee.ObjectId) AS 'Count', idlee.GroupId, idlee.Name
  FROM          (SELECT     CONVERT(int, Sum(idle.distance)) AS distance, idle.ObjectId, idle.GroupId, idle.Name
                          FROM          (SELECT     Message.ObjectId, fn_GpsUtil_Distance(Message.x, Message.y, lead(Message.x) 
                                                                         OVER (partition BY Message.objectid
                                                  ORDER BY Message.GpsTime), lead(Message.y) OVER (partition BY Message.objectid
                          ORDER BY Message.GpsTime)) AS distance, [Group].GroupId, [Group].Name
  FROM          [Group] INNER JOIN
                         GroupObject ON [Group].GroupId = GroupObject.GroupId INNER JOIN
                         Message ON GroupObject.ObjectId = Message.ObjectId INNER JOIN
                         Object ON GroupObject.ObjectId = Object.ObjectId
  WHERE      (Object.Enabled = 1) AND (Object.ClientId = 5) AND (Message.GpsTime >= GETDATE() - 1) AND 
                         (Message.GpsTime <= GETDATE())) AS idle
 GROUP BY idle.ObjectId, idle.GroupId, idle.Name) AS idlee
 WHERE     idlee.distance >= 100 AND idlee.distance <= 300
 GROUP BY idlee.GroupId, idlee.Name) 



  Count  GroupID  Group
      40    15  DC-1
      50    16  DC-2
      20    17  DC-3
      64    13  LC-1
      16    14  LC-2

但我希望在群组基础上输出这样的内容。

      GroupID  Group Count 0<10  Count 100 To 300
       15       DC-1   36             40
       16       DC-2   30             50
       17       DC-3   13             20
       13       LC-1   64             64
       14       LC-2   16             16

1 个答案:

答案 0 :(得分:0)

尝试这个...用UNION加入SQL,在第一个计数(idlee.ObjectId)AS count1,0作为count2,在第二个0作为count1,count(idlee.ObjectId)AS count2。

然后将整个SQL作为临时表包含在内,并将Count1和Count2相加。

SELECT Temp.GroupID, Temp.Group, Sum(Temp.Count1) as CountLess10, Sum(Temp.Count2) as Count100300 FROM
(
                  (SELECT     count(idlee.ObjectId) AS count1, 0 as count2, idlee.GroupId as GroupID, idlee.Name as Group
                    FROM          (SELECT     CONVERT(int, Sum(idle.distance)) AS distance, idle.ObjectId, idle.GroupId, idle.Name
                                            FROM          (SELECT     Message.ObjectId, fn_GpsUtil_Distance(Message.x, Message.y, lead(Message.x)
                                                                                            OVER (partition BY Message.objectid
                                                                    ORDER BY Message.GpsTime), lead(Message.y) OVER (partition BY Message.objectid
                                            ORDER BY Message.GpsTime)) AS distance, [Group].GroupId, [Group].Name
                    FROM          [Group] INNER JOIN
                                           GroupObject ON [Group].GroupId = GroupObject.GroupId INNER JOIN
                                           Message ON GroupObject.ObjectId = Message.ObjectId INNER JOIN
                                           Object ON GroupObject.ObjectId = Object.ObjectId
                    WHERE      (Object.Enabled = 1) AND (Object.ClientId = 5) AND (Message.GpsTime >= GETDATE() - 1) AND 
                                           (Message.GpsTime <= GETDATE())) AS idle
 GROUP BY idle.ObjectId, idle.GroupId, idle.Name) AS idlee
 WHERE     idlee.distance < 10
 GROUP BY idlee.GroupId, idlee.Name)


UNION



    (SELECT     0 as count1, count(idlee.ObjectId) AS count2, idlee.GroupId as GroupID, idlee.Name as Name
  FROM          (SELECT     CONVERT(int, Sum(idle.distance)) AS distance, idle.ObjectId, idle.GroupId, idle.Name
                      FROM          (SELECT     Message.ObjectId, fn_GpsUtil_Distance(Message.x, Message.y, lead(Message.x) 
                                                                     OVER (partition BY Message.objectid
                                              ORDER BY Message.GpsTime), lead(Message.y) OVER (partition BY Message.objectid
                      ORDER BY Message.GpsTime)) AS distance, [Group].GroupId, [Group].Name
  FROM          [Group] INNER JOIN
                     GroupObject ON [Group].GroupId = GroupObject.GroupId INNER JOIN
                     Message ON GroupObject.ObjectId = Message.ObjectId INNER JOIN
                     Object ON GroupObject.ObjectId = Object.ObjectId
  WHERE      (Object.Enabled = 1) AND (Object.ClientId = 5) AND (Message.GpsTime >= GETDATE() - 1) AND 
                     (Message.GpsTime <= GETDATE())) AS idle
 GROUP BY idle.ObjectId, idle.GroupId, idle.Name) AS idlee
 WHERE     idlee.distance >= 100 AND idlee.distance <= 300
 GROUP BY idlee.GroupId, idlee.Name)    
 ) Temp Group By Temp.GroupID, Temp.Group

希望这有帮助。