如何将两个结果集合并到一个结果集中

时间:2016-05-12 07:28:01

标签: sql-server

第一个结果集

SELECT
  LEFT(DATENAME(MONTH, CONVERT(datetime, TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, CONVERT(datetime, TRIPDATE, 103)), 2) AS [MonthYear],
  SUM(ISNULL(CAST(TOTALFREIGHT AS float), 0)) AS FreightAmount
FROM OFFLINETRANS
GROUP BY LEFT(DATENAME(MONTH, CONVERT(datetime, TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, CONVERT(datetime, TRIPDATE, 103)), 2)
ORDER BY MAX(TRIPDATE)

第二个结果集

SELECT
  LEFT(DATENAME(MONTH, TRIPDATE), 3) + ' ' + RIGHT(DATENAME(YEAR, TRIPDATE), 2) AS [MonthYear],
  SUM(TRIPCOST) AS FreightAmount
FROM TRANSVIEW
GROUP BY DATENAME(MONTH, TRIPDATE),
         DATENAME(YEAR, TRIPDATE)
ORDER BY MAX(TRIPDATE)

如何将两个结果集合并为一个结果集

3 个答案:

答案 0 :(得分:0)

您可以像这样使用UNION运算符:

SELECT
  LEFT(DATENAME(MONTH, CONVERT(datetime, TRIPDATE, 103)), 3) + ' ' 
  + RIGHT(DATENAME(YEAR, CONVERT(datetime, TRIPDATE, 103)), 2) AS [MonthYear],
  SUM(ISNULL(CAST(TOTALFREIGHT AS float), 0)) AS FreightAmount,
  'OFFLINETRANS ' AS FROMTABLE
FROM OFFLINETRANS
GROUP BY LEFT(DATENAME(MONTH, CONVERT(datetime, TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, CONVERT(datetime, TRIPDATE, 103)), 2)

UNION

SELECT
  LEFT(DATENAME(MONTH, TRIPDATE), 3) + ' ' 
  + RIGHT(DATENAME(YEAR, TRIPDATE), 2) AS [MonthYear],
  SUM(TRIPCOST) AS FreightAmount
  'TRANSVIEW' AS FROMTABLE
FROM TRANSVIEW
GROUP BY DATENAME(MONTH, TRIPDATE),DATENAME(YEAR, TRIPDATE)
ORDER BY MAX(TRIPDATE)

您还可以在subyelect和grup中包含上述查询,以便按月分组两个结果...

答案 1 :(得分:0)

尝试以下操作,将第二个SUM放入子查询中,然后将其添加到原始查询中。

SELECT
LEFT(DATENAME(MONTH, CONVERT(DATETIME, o.TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, CONVERT(DATETIME, o.TRIPDATE, 103)), 2) AS [MonthYear],
SUM(ISNULL(CAST(o.TOTALFREIGHT AS float), 0)) + 
(SELECT SUM(t.TRIPCOST)
 FROM TRANSVIEW t
 WHERE DATENAME(MONTH, t.TRIPDATE), DATENAME(YEAR, t.TRIPDATE) = LEFT(DATENAME(MONTH, CONVERT(DATETIME, o.TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, CONVERT(DATETIME, o.TRIPDATE, 103)), 2)
 GROUP BY DATENAME(MONTH, t.TRIPDATE), DATENAME(YEAR, t.TRIPDATE)) AS FreightAmount
FROM OFFLINETRANS o
GROUP BY LEFT(DATENAME(MONTH, CONVERT(DATETIME, o.TRIPDATE, 103)), 3) + ' ' + RIGHT(DATENAME(YEAR, CONVERT(DATETIME, o.TRIPDATE, 103)), 2)
ORDER BY MAX(o.TRIPDATE)

答案 2 :(得分:0)

如果

  

运费金额应为每月两个结果集的总和,不得重复

然后使用:

SELECT  CAST([Month] as nvarchar(3))+' '+CAST([Year] as nvarchar(2)) as [MonthYear],
        SUM(DISTINCT FreightAmount) as FreightAmount
FROM (
    SELECT
      LEFT(DATENAME(MONTH, CONVERT(datetime, TRIPDATE, 103)), 3) AS [Month],
      RIGHT(DATENAME(YEAR, CONVERT(datetime, TRIPDATE, 103)), 2) AS [Year],
      SUM(ISNULL(CAST(TOTALFREIGHT AS float), 0)) AS FreightAmount
    FROM OFFLINETRANS
    GROUP BY LEFT(DATENAME(MONTH, CONVERT(datetime, TRIPDATE, 103)), 3),
             RIGHT(DATENAME(YEAR, CONVERT(datetime, TRIPDATE, 103)), 2)
    UNION ALL
    SELECT
      LEFT(DATENAME(MONTH, TRIPDATE), 3) AS [Month],
      RIGHT(DATENAME(YEAR, TRIPDATE), 2) AS [Year],
      SUM(TRIPCOST) AS FreightAmount
    FROM TRANSVIEW
    GROUP BY DATENAME(MONTH, TRIPDATE),
             DATENAME(YEAR, TRIPDATE)
) as t
GROUP BY [Year],[Month]
ORDER BY [Year],[Month]