按月计算每个月,每年和每组的数据

时间:2015-01-27 21:03:42

标签: sql-server group-by

我有3张桌子,我想得到以下结果 在book_table isbn是Pk 在order_table orderid是Pk 在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

2 个答案:

答案 0 :(得分:0)

让您的“年份”查询单独工作。然后让您的“月份”查询单独工作。然后将“年”查询添加到“月”。这可以通过将“年”查询添加到“月”查询作为“加入子查询”来完成。

还有很多其他方法,但这可以基于两个简单的查询保留所有内容。

答案 1 :(得分:0)

这样的东西?

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