每个查询详细信息的SQL查询

时间:2015-06-16 13:07:31

标签: sql sql-server sql-server-2008

我想显示每个月以及全年的销售总额

例如:

january 2000     
feb     5000   
...
decem   4000     


total sum = 1月至12月的总和。在给定的一年

4 个答案:

答案 0 :(得分:1)

[Sample data]                 [output]
m    | price                  m    | price | total
-----+--------                -----+-------+-------
jan  | 2000                   jan  | 2000  | 100000
feb  | 5000                   feb  | 5000  | 100000
...  : ...                    ...  : ...   : ...
dec  | 4000                   dec  | 4000  | 100000

查询是:

SELECT
    m, price,
    SUM(price) OVER (PARTITION BY NULL) As total
FROM
    yourTable
[Sample data]                [output]
date       | price           monthName | monthPrice | yearPrice
-----------+------           ----------+------------+-----------
2015-01-01 | 1000            January   | 2000       | 100000
2015-01-15 | 1000            February  | 5000       | 100000
2015-01-01 | 1000            ...       : ...        : ...
2015-01-05 | 1500            December  | 4000       | 100000
2015-01-20 | 2500 
...        : ...
2015-12-01 | 4000

查询是:

SELECT
    [year], [monthName], [monthPrice],
    SUM(MonthPrice) OVER (PARTITION BY [year]) As total
FROM (
    SELECT 
        YEAR([date]) AS [year], MONTH([date]) as [month], {fn MONTHNAME([date])} As [monthName], SUM(price) as monthPrice
    FROM 
        t
    GROUP BY
        YEAR([date]), MONTH([date]), {fn MONTHNAME([date])}) dt
ORDER BY
    [year], [month]

答案 1 :(得分:0)

SELECT Year(DateOfSale), Month(DateOfSale), Sum(SaleAmount) From Sales
group by Year(DateOfSale), Month(DateOfSale)
Compute Sum(Sum(SaleAmount))

这样的事情应该有用。

答案 2 :(得分:0)

这应该适合你。

-- Create demo data
CREATE TABLE #temp(d date, sales int)

INSERT INTO #temp(d,sales)
VALUES  (N'2013/01/01',1000),(N'2013/02/01',1000),(N'2013/03/01',1000),(N'2013/04/01',1000),(N'2013/05/01',1000),(N'2013/06/01',1000),
        (N'2013/07/01',1000),(N'2013/08/01',1000),(N'2013/09/01',1000),(N'2013/10/01',1000),(N'2013/11/01',1000),(N'2013/12/01',1000),
        (N'2014/01/01',1000),(N'2014/02/01',1000),(N'2014/03/01',1000),(N'2014/04/01',1000),(N'2014/05/01',1000),(N'2014/06/01',1000),
        (N'2014/07/01',1000),(N'2014/08/01',1000),(N'2014/09/01',1000),(N'2014/10/01',1000),(N'2014/11/01',1000),(N'2014/12/01',1000),
        (N'2015/01/01',1000),(N'2015/02/01',1000),(N'2015/03/01',1000),(N'2015/04/01',1000),(N'2015/05/01',1000),(N'2015/06/01',1000),
        (N'2015/07/01',1000),(N'2015/08/01',1000),(N'2015/09/01',1000),(N'2015/10/01',1000),(N'2015/11/01',1000),(N'2015/12/01',1000),
        (N'2013/01/01',1000),(N'2013/02/01',1000),(N'2015/03/01',1000),(N'2015/04/01',1000),(N'2014/03/01',1000),(N'2014/04/01',1000);

SELECT *,  SUM(sales) OVER(PARTITION BY DATEPART(year,d), DATEPART(month,d)) as SalesPerMonth, SUM(sales) OVER(PARTITION BY DATEPART(year,d)) as SalesPerYear
FROM #temp as t

DROP TABLE #temp

答案 3 :(得分:0)

使用GROUPING SETS显示最后的总和。

<强>查询

select datename(month,[date]) as [Month],sum(price) as [Total]
from table_name
where datepart(year,[date])='2015'
group by grouping sets((datename(month,[date]),month([date])),())
order by case when month([date]) is null then 13 else month([date]) end;

Fiddle demo here