用于创建具有算术表达式的列的SQL查询

时间:2013-12-11 18:33:18

标签: sql-server tsql sql-server-2012

我有我的sql表和查询,如下所示:

CREATE TABLE #ABC([Year] INT, [Month] INT, Stores INT);
CREATE TABLE #DEF([Year] INT, [Month] INT, SalesStores INT);
CREATE TABLE #GHI([Year] INT, [Month] INT, Products INT);

INSERT #ABC VALUES (2013,1,1);
INSERT #ABC VALUES (2013,1,2);
INSERT #ABC VALUES (2013,2,3);

INSERT #DEF VALUES (2013,1,4);
INSERT #DEF VALUES (2013,1,5);
INSERT #DEF VALUES (2013,2,6);

INSERT #GHI VALUES (2013,1,7);
INSERT #GHI VALUES (2013,1,8);
INSERT #GHI VALUES (2013,2,9);
INSERT #GHI VALUES (2013,3,10);

我当前的查询是

SELECT T.[Year],
       T.[Month]
       -- select the sum for each year/month combination using a correlated subquery (each result from the main query causes another data retrieval operation to be run)
       ,
       (SELECT SUM(Stores)
        FROM   #ABC
        WHERE  [Year] = T.[Year]
               AND [Month] = T.[Month]) AS [Sum_Stores],
       (SELECT SUM(SalesStores)
        FROM   #DEF
        WHERE  [Year] = T.[Year]
               AND [Month] = T.[Month]) AS [Sum_SalesStores],
       (SELECT SUM(Products)
        FROM   #GHI
        WHERE  [Year] = T.[Year]
               AND [Month] = T.[Month]) AS [Sum_Products]
FROM   (
       -- this selects a list of all possible dates.
       SELECT [Year],
              [Month]
       FROM   #ABC
       UNION
       SELECT [Year],
              [Month]
       FROM   #DEF
        UNION
        SELECT [Year],
               [Month]
        FROM   #GHI) AS T; 

返回

+------+-------+------------+-----------------+--------------+
| Year | Month | Sum_Stores | Sum_SalesStores | Sum_Products |
+------+-------+------------+-----------------+--------------+
| 2013 |     1 | 3          | 9               |           15 |
| 2013 |     2 | 3          | 6               |            9 |
| 2013 |     3 | NULL       | NULL            |           10 |
+------+-------+------------+-----------------+--------------+

我想要做的是在我的查询中再添加两列,显示

Sum_SalesStores / Sum_Products& Sum_SalesStores / Sum_Stores每个月,然后根据这两个表达式对查询进行排序。谁能告诉我它是如何可能的?

2 个答案:

答案 0 :(得分:1)

一种方法是将整个现有查询放入CTE,然后您可以从中进行选择并执行计算。

;WITH CTE
     AS (
        /*Paste your existing query*/
        )
SELECT *,
       Sum_SalesStores / Sum_Products AS Foo,
       Sum_SalesStores / Sum_Stores   AS Bar
FROM   CTE
ORDER  BY Foo,
          Bar 

答案 1 :(得分:0)

我重新考虑了你的代码,将逻辑核心放在FROM语句中。

SELECT Dates.[Year]
    ,Dates.[Month]
    ,SumStores
    ,SumSalesStores
    ,SumProducts
    ,SumSalesStores/SumProducts
    ,SumSalesStores/SumStores
FROM
    (
        -- This selects a list of all possible dates.
        SELECT [Year],[Month] FROM ABC
        UNION SELECT [Year],[Month] FROM DEF
        UNION SELECT [Year],[Month] FROM GHI
    ) AS Dates
    Left Join
    (
        SELECT [Year]
            ,[Month]
            ,SumStores = Sum(Stores)
        FROM   ABC
        GROUP BY [Year]
            ,[Month]
    ) As Stores
    On Dates.[Year] = Stores.[Year]
    And Dates.[Month] = Stores.[Month]
    Left Join
    (
        SELECT [Year]
            ,[Month]
            ,SumSalesStores = Sum(SalesStores)
        FROM   DEF
        GROUP BY [Year]
            ,[Month]
    ) As SalesStores
    On Dates.[Year] = SalesStores.[Year]
    And Dates.[Month] = SalesStores.[Month]
    Left Join
    (
        SELECT [Year]
            ,[Month]
            ,SumProducts = Sum(Products)
        FROM   GHI
        GROUP BY [Year]
            ,[Month]
    ) As Products
    On Dates.[Year] = Products.[Year]
    And Dates.[Month] = Products.[Month]
ORDER BY 1, 2

这是SQL Fiddle