在SELECT子句中引用FROM子查询

时间:2015-11-20 08:24:55

标签: sql sql-server

我有SELECT这样的查询:

SELECT
    SubQuery.Year            AS Year,
    SubQuery.Customer        AS Customer,
    SUM(SubQuery.OrderCount) AS OrderCount
FROM
    {complexquery} AS SubQuery
GROUP BY
    SubQuery.Year, SubQuery.Customer

给出了这样的结果:

+--------+----------+------------+
| year   | customer | ordercount |
+--------+----------+------------+
| 2015   | John     | 10         |
+--------+----------+------------+
| 2015   | Amy      | 20         |
+--------+----------+------------+
| 2015   | Bob      | 10         |
+--------+----------+------------+
| 2014   | John     | 200        |
+--------+----------+------------+
| 2014   | Amy      | 200        |
+--------+----------+------------+
| 2014   | Bob      | 200        |
+--------+----------+------------+

我想添加"比率"将显示客户的全年订单数量的列,如下所示:

+--------+----------+------------+-------+
| year   | customer | ordercount | ratio |
+--------+----------+------------+-------+
| 2015   | John     | 10         | 0.25  |
+--------+----------+------------+-------+
| 2015   | Amy      | 20         | 0.5   |
+--------+----------+------------+-------+
| 2015   | Bob      | 10         | 0.25  |
+--------+----------+------------+-------+
| 2014   | John     | 200        | 0.33  |
+--------+----------+------------+-------+
| 2014   | Amy      | 200        | 0.33  |
+--------+----------+------------+-------+
| 2014   | Bob      | 200        | 0.33  |
+--------+----------+------------+-------+

我尝试过类似的事情:

SELECT
    SubQuery.Year            AS Year,
    SubQuery.Customer        AS Customer,
    SUM(SubQuery.OrderCount) AS OrderCount
    SUM(SubQuery.OrderCount) / (SELECT SUM(OrderCount) FROM SubQuery WHERE...) AS Ratio
FROM
    {complexquery} AS SubQuery
GROUP BY
    SubQuery.Year, SubQuery.Customer

但是没有成功 - 我无法在SubQuery条款中引用SELECT

我认为应该有一些优雅的方法来解决它,但我没有看到它。

我不想在{complexquery}中重复SELECT,因为{complexquery}很长。

我无法修改数据库架构。

我正在使用MS SQL服务器。

1 个答案:

答案 0 :(得分:4)

您可以将select语句放入另一个子查询中,然后使用window function获得总计并除以此:

WITH Subquery AS
(   SELECT
        SubQuery.[Year]          AS [Year],
        SubQuery.Customer        AS Customer,
        SUM(SubQuery.OrderCount) AS OrderCount
    FROM
        {complexquery} AS SubQuery
    GROUP BY
        SubQuery.[Year], SubQuery.Customer
)
SELECT  [Year],
        Customer,
        OrderCount,
        1.0 * OrderCount / SUM(OrderCount) OVER(PARTITION BY [Year]) AS Ratio
FROM    Subquery;

工作示例

DECLARE @ComplexSubquery TABLE ([year] INT, Customer VARCHAR(10), OrderCount INT);

INSERT @ComplexSubquery ([year], Customer, OrderCount)
VALUES
    (2015, 'John', 10),
    (2015, 'Amy', 20),
    (2015, 'Bob', 10),
    (2014, 'John', 200),
    (2014, 'Amy', 200),
    (2014, 'Bob', 200);

WITH Subquery AS
(   SELECT
        SubQuery.[Year]          AS [Year],
        SubQuery.Customer        AS Customer,
        SUM(SubQuery.OrderCount) AS OrderCount
    FROM
        @ComplexSubquery AS SubQuery
    GROUP BY
        SubQuery.[Year], SubQuery.Customer
)
SELECT  [Year],
        Customer,
        OrderCount,
        1.0 * OrderCount / SUM(OrderCount) OVER(PARTITION BY [Year]) AS Ratio
FROM    Subquery;