按月、年和分组计算数据

Calculate data by each month,year and Group by something

我有 3 个 table,我想得到以下结果 在 book_table isbn 是 Pk 在 order_table orderid 是 Pk in order_detail_table orderid&itemno 是 Pk,isbn 和 orderid 是 Fk

book_table

isbn    title          edition_no   price
1001    Database Manage     2       95
1111    Fundamentals of     3       75
2002    Database System     2       90
2222    Database Princi     1       50
3003    Database System     4       100
3333    Principles of D     1       100
4004    Database Modeli     2       70
4444    Principles of D     2       60
5555    Object_Relation     1       75
6666    Principles of D     1       65
7777    Readings in Dat     1       65

order_table

orderid orderdate
1       1998-03-01
2       1999-03-01
3       1999-03-01
4       1999-03-12
5       1999-03-08
6       1999-03-10
7       1999-03-12
8       1999-03-11

order_details_table

orderid item_no isbn    quantity    customerid
1           1   2222    1               1
1           2   4444    2               1
1           3   6666    2               1
1           4   8888    1               1
1           5   1111    1               1
1           6   1001    1               1
1           7   2002    1               1
1           8   3003    1               1
1           9   4004    1               1
1           10  5555    1               1

结果必须是

Month_rank year_rank  Month_total   year_total    month       year
1            1        14580.00     17505.00       March       2004
2            1        2925.00      17505.00       January     2004
1            2        9320.00      17490.00       March       2002
2            2        7090.00      17490.00       August      2002
3            2        825.00       17490.00       September   2002
4            2        175.00       17490.00       January     2002
. . .

给出这个结果的查询。

和下面的结果的第二个查询 下面是什么查询给出的结果

AMOUNT_MONEY            MONTH            YEAR
14040.00                   -               -
130.00                     -             1998
975.00                     -             1999
1365.00                    -             2000
390.00                     -             2001
7930.00                    -             2002
2405.00                    -             2003
845.00                     -             2004
130.00                  March            1998
260.00                  January          1999

让您的 "year" 查询单独运行。然后让您的 "month" 查询单独工作。然后将 "year" 查询添加到 "month"。这可以通过将 "year" 查询作为 "join subquery".

添加到 "month" 查询来完成

还有许多其他方法,但这使一切都基于两个简单的查询。

是这样的吗?

WITH month_totals AS (
    SELECT t.order_month, month_total = SUM(t.amount)
    FROM (
        SELECT order_month = DATEADD(day, -(DAY(o.orderdate) - 1), o.orderdate), amount = od.quantity * b.price
        FROM book_table b
        INNER JOIN order_details_table od
        ON b.isbn = od.isbn
        INNER JOIN order_table o
        ON o.orderid = od.orderid
    ) t
    GROUP BY t.order_month
),
year_totals AS (
    SELECT order_year = YEAR(o.orderdate), year_total = SUM(od.quantity * b.price)
    FROM book_table b
    INNER JOIN order_details_table od
    ON b.isbn = od.isbn
    INNER JOIN order_table o
    ON o.orderid = od.orderid
    GROUP BY YEAR(o.orderdate)
)
SELECT mt.month_rank, yt.year_rank, mt.month_total, yt.year_total, 
    [month] = DATENAME(MONTH, mt.order_month), [year] = order_year
FROM (
    SELECT t.*, year_rank = RANK() OVER (ORDER BY t.year_total DESC)
    FROM year_totals t
) yt
INNER JOIN (
    SELECT t.*, month_rank = RANK() OVER (PARTITION BY YEAR(t.order_month) ORDER BY t.month_total DESC)
    FROM month_totals t
) mt
ON yt.order_year = YEAR(mt.order_month)
ORDER BY yt.year_rank, mt.month_rank