窗口函数计数聚合

时间:2019-07-05 10:02:22

标签: sql sql-server count common-table-expression

我这里有两个非常复杂的查询。两者都应返回给定国家/地区(在这种情况下为荷兰)在整个12个月中的每月使用量以及占该12个月中该月份的百分比。

但是我将count用作窗口函数的那一行比不将count用作窗口函数的那一行返回多一行。

enter image description here

左侧图片的查询为:

;WITH MonthUsage AS
(
  SELECT customer_id, country_name, [Month], [Year], SUM(ItemsPerMonth) 
AS ItemsPerMonth
  FROM (
    SELECT cs.country_name, c.customer_id, YEAR(mol.date_watched) AS Year, MONTH(mol.date_watched) AS [Month], COUNT(*) AS ItemsPerMonth
    FROM Customer c
    JOIN Customer_Subscription CS ON C.customer_id = CS.customer_id
    JOIN Movie_Order_Line mol ON mol.customer_id = c.customer_id
    WHERE (mol.date_watched BETWEEN '2017-07-01' AND '2018-07-01') AND cs.country_name = 'Nederland'
    GROUP BY c.customer_id,cs.country_name, YEAR(mol.date_watched), 
MONTH(mol.date_watched)

UNION ALL

SELECT cs.country_name, c.customer_id, YEAR(sol.date_watched) AS Year, 
MONTH(sol.date_watched), COUNT(*) AS ItemsPerMonth
      FROM Customer c
      JOIN Customer_Subscription CS ON C.customer_id = CS.customer_id
      JOIN Show_Order_Line sol ON sol.customer_id = c.customer_id
      WHERE sol.date_watched BETWEEN '2017-07-01' AND '2018-07-01'
      GROUP BY c.customer_id, cs.country_name, YEAR(sol.date_watched), 
    MONTH(sol.date_watched)
) AS MonthItems
WHERE country_name = 'Nederland'
GROUP BY customer_id, country_name, [Month], [Year]
),
Months(MonthNumber) AS 
(
  SELECT 1
  UNION ALL
  SELECT MonthNumber + 1 
  FROM months
  WHERE MonthNumber < 12
)
SELECT cmb.[Year], ISNULL(cmb.[Month], m.MonthNumber) AS [Month], 
ISNULL(ItemsPerMonth, 0) AS ItemsPerMonth, 
ISNULL(FORMAT(((CAST(ItemsPerMonth AS decimal) / CAST((
    SELECT SUM(ItemsPerMonth) 
    FROM MonthUsage) AS decimal
))),'P0'), '0%') AS [PercentageOfTotal]
FROM MonthUsage cmb
JOIN Customer c ON c.customer_id = cmb.customer_id
JOIN Months m ON m.MonthNumber = cmb.[Month]
ORDER BY cmb.[Year] ASC, [Month] ASC

对正确图片的查询是:

;WITH MonthUsage AS (
SELECT *
FROM (
    SELECT 
        YEAR(date_watched) AS [Year], 
        MONTH(date_watched) AS [Month], 
        COUNT(order_id) OVER(PARTITION BY CONCAT(YEAR(date_watched), MONTH(date_watched))) AS ItemsPerMonth
    FROM Movie_Order_Line mov
    JOIN Customer c ON c.customer_id = mov.customer_id
    JOIN Customer_Subscription cs ON C.customer_id = cs.customer_id
    WHERE date_watched BETWEEN '2017-07-01' AND '2018-07-01' AND cs.country_name = 'Nederland'

    UNION ALL

    SELECT YEAR(date_watched) AS [Year], MONTH(date_watched) AS [Month], COUNT(order_id) OVER(PARTITION BY CONCAT(YEAR(date_watched), MONTH(date_watched))) AS ItemsPerMonth
    FROM Show_Order_Line sol
    JOIN Customer c ON c.customer_id = sol.customer_id
    JOIN Customer_Subscription cs ON C.customer_id = cs.customer_id
    WHERE date_watched BETWEEN '2017-07-01' AND '2018-07-01' AND cs.country_name = 'Nederland'
) AS Combined
GROUP BY [YEAR], [Month], ItemsPerMonth
)
SELECT *, ISNULL(FORMAT(((CAST(ItemsPerMonth AS decimal) / CAST((SELECT 
SUM(ItemsPerMonth) FROM MonthUsage) AS decimal))),'P0'), '0%') AS 
[PercentageOfTotal]
FROM MonthUsage
ORDER BY [Year] ASC, [Month] ASC

我似乎无法弄清楚为什么我得到不同的结果。任何帮助深表感谢。预先感谢您的宝贵时间。

0 个答案:

没有答案