Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询每个数据库的详细信息和_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

SQL查询每个数据库的详细信息和

SQL查询每个数据库的详细信息和,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我想显示每个月和全年的销售总额 例如: january 2000 feb 5000 ... decem 4000 和 总金额=1月至12月的金额。当年 SELECT Year(DateOfSale), Month(DateOfSale), Sum(SaleAmount) From Sales group by Year(DateOfSale), Month(DateOfSale) Compute Sum(Sum(SaleAmount)) 这样的办法应该行

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

例如:

january 2000     
feb     5000   
...
decem   4000     

总金额
=1月至12月的金额。当年

SELECT Year(DateOfSale), Month(DateOfSale), Sum(SaleAmount) From Sales
group by Year(DateOfSale), Month(DateOfSale)
Compute Sum(Sum(SaleAmount))
这样的办法应该行得通

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

查询是:

SELECT
    m, price,
    SUM(price) OVER (PARTITION BY NULL) As total
FROM
    yourTable
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]

这应该对你有好处

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

使用分组集在末尾显示总和

查询

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;

不清楚您想做什么。显示格式良好的示例数据和所需结果,并包括列的类型。展示您迄今为止所做的尝试也是一件好事。
COMPUTE
被贬低了。谢谢你@VladimirOselsky