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

时间:2021-12-31 14:22:27

I want to display sum of sales for each month and also all the year

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

For e.g. :

对于例如:

january 2000     
feb     5000   
...
decem   4000     

And
total sum = sum of january to december. for the given year

总和= 1月到12月的总和。在给定的一年

4 个解决方案

#1


1  

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

Query is:

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

Query is:

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]

#2


0  

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

Something like that should work.

这样的事情应该有效。

#3


0  

this should do the trick for you.

这应该为你做的伎俩。

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

#4


0  

Use GROUPING SETS to show the sum at the end.

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

Query

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

小提琴演示在这里

#1


1  

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

Query is:

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

Query is:

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]

#2


0  

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

Something like that should work.

这样的事情应该有效。

#3


0  

this should do the trick for you.

这应该为你做的伎俩。

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

#4


0  

Use GROUPING SETS to show the sum at the end.

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

Query

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

小提琴演示在这里