2015-06-16 48 views
-3

我想要顯示每月和全年的銷售額總和SQL查詢詳情請參閱每個

例如, :

january 2000  
feb  5000 
... 
decem 4000  

而且
total sum =一月總和到12月。對於給定的一年

+3

目前還不清楚是什麼你正在嘗試做的。顯示格式良好的樣本數據和期望的結果幷包括列的類型。展示迄今爲止嘗試的內容總是很好。 –

回答

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] 
0
SELECT Year(DateOfSale), Month(DateOfSale), Sum(SaleAmount) From Sales 
group by Year(DateOfSale), Month(DateOfSale) 
Compute Sum(Sum(SaleAmount)) 

這樣的東西應該工作。

+2

'COMPUTE'折舊。 https://technet.microsoft.com/en-us/library/ms181708(v=sql.105).aspx –

+0

謝謝@VladimirOselsky –

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 
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