SQL查询,每月统计列中的值数

SQL查询,每月统计列中的值数,sql,sql-server,sql-server-2008,stored-procedures,Sql,Sql Server,Sql Server 2008,Stored Procedures,我需要的是计算每个月在给定日期范围内的销售额,这是基于用户输入的StartDate和EndDate。例如,用户输入StartDate-01/2015和EndDate-04/01/2015 输出是这样的 id date sales 1 01/01/2015 100 2 01/01/2015 100 3 02/01/2015 100 4 03/01/2015 100 Month StartMonth EndMonth

我需要的是计算每个月在给定日期范围内的销售额,这是基于用户输入的StartDate和EndDate。例如,用户输入StartDate-01/2015和EndDate-04/01/2015

输出是这样的

id   date          sales
1    01/01/2015    100
2    01/01/2015    100
3    02/01/2015    100
4    03/01/2015    100
Month  StartMonth  EndMonth     TotalSales
1      01/01/2015  01/31/2015   200
2      02/01/2015  02/28/2015   100
3      03/01/2015  03/31/2015   100
4      04/01/2015  04/30/2015   0
select [Month] = Month([date]), [date] as StartMonth, 
        EndMonth = convert(date, dateadd(month,datediff(month,0,'2017-02-11')+1,0)-1),
        TotalSales from (
    select [date],  sum(sales) as totalSales 
        from #yoursales
        group by [date]
        ) a
开始像这样

id   date          sales
1    01/01/2015    100
2    01/01/2015    100
3    02/01/2015    100
4    03/01/2015    100
Month  StartMonth  EndMonth     TotalSales
1      01/01/2015  01/31/2015   200
2      02/01/2015  02/28/2015   100
3      03/01/2015  03/31/2015   100
4      04/01/2015  04/30/2015   0
select [Month] = Month([date]), [date] as StartMonth, 
        EndMonth = convert(date, dateadd(month,datediff(month,0,'2017-02-11')+1,0)-1),
        TotalSales from (
    select [date],  sum(sales) as totalSales 
        from #yoursales
        group by [date]
        ) a

你可以这样做

set @Start_act = cast(DATEADD(month, DATEDIFF(month, 0, @StartDate), 0) as date)
    set @End_act = cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @Enddate)+1, 0)) as date)



    set @counter = DATEDIFF(month, @Start_act, @End_act)
    if(@counter = 1)
    begin
        set @counter = @counter
    end
    else
        set @counter = @counter + 1
    end

    set @count = 0

    CREATE TABLE #TempTableID
    (
        Month int,  
        StartMonth date, 
        EndMonth date,
        TotalSales  
    )

    while (@count <= @counter)
    begin

        set @count = @count + 1;

        if(@count = 1)
        begin
            set @Start = @Start_act
            set @End = cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @Start_act)+1, 0)) as date)
            set @plannedHorseCapacity = 123
        end
        else
        begin
            set @Start = cast(DATEADD(d, 1, @End)as date)
            set @End = cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @Start)+1, 0)) as date)
            set @plannedHorseCapacity = 456
        end

        Insert into #TempTableID
        (
            Month  
            StartMonth 
            EndMonth 
            TotalSales 
        )
        Values
        (
            @count,
            @Start,
            @End,
            @TotalSales

        )

        if(@count > @counter)
        begin
            break
        end
        else
        begin           
            continue
        end
    end

    Select * from #TempTableID

您可以使用以下脚本-

;WITH [CTE_DATE]
AS
(
    SELECT  MONTH(@fromdt) AS [Month]
            ,DATEADD(mm, DATEDIFF(mm, 0, @fromdt), 0)   AS StartMonth
            ,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@fromdt)+1,0))   AS EndMonth
    UNION ALL 
    SELECT  [Month] + 1 AS [Month]
            ,DATEADD(MONTH,1,StartMonth)    AS StartMonth
            ,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,DATEADD(MONTH,1,StartMonth))+1,0))       AS EndMonth
    FROM [CTE_DATE] WHERE [Month] < MONTH(@todt)
)


SELECT  *
        ,(SELECT SUM(sales) FROM yourtable where [date] between StartMonth and EndMonth) as  TotalSales
FROM [CTE_DATE]
输出将是

WITH cte
AS
(SELECT
CONVERT(VARCHAR(20), MONTH([date]))
+ '-' + CONVERT(VARCHAR(20), YEAR([date])) monthyear
,([sales])

FROM [sales])
--add where condition for from and to date here
SELECT
monthyear
,SUM(sales) totalsales
FROM cte
GROUP BY monthyear
带有开始和结束日期的选项2

输出将是

monthyear Month Year StartDate              EndDate                   totalsales
01-2015   1   2015  2015-01-01 00:00:00.000 2015-01-31 00:00:00.000   400
02-2015   2   2015  2015-02-01 00:00:00.000 2015-02-28 00:00:00.000   50
03-2015   3   2015  2015-03-01 00:00:00.000 2015-03-31 00:00:00.000   50
11-2015  11   2015  2015-11-01 00:00:00.000 2015-11-30 00:00:00.000   100
编辑排序

如果您有多年的数据,那么日期将不会按顺序在最后的[StartDate]之前修复该添加订单

输出将是

monthyear Month Year StartDate              EndDate                   totalsales
01-2015   1   2015  2015-01-01 00:00:00.000 2015-01-31 00:00:00.000   400
02-2015   2   2015  2015-02-01 00:00:00.000 2015-02-28 00:00:00.000   50
03-2015   3   2015  2015-03-01 00:00:00.000 2015-03-31 00:00:00.000   50
11-2015  11   2015  2015-11-01 00:00:00.000 2015-11-30 00:00:00.000   100

您可以在下面找到月末:

monthyear Month Year    StartDate    EndDate                   totalsales
01-2015   1   2015  2015-01-01 00:00:00  2015-01-31 00:00:00.000    400
02-2015   2   2015  2015-02-01 00:00:00  2015-02-28 00:00:00.000    50
03-2015   3   2015  2015-03-01 00:00:00  2015-03-31 00:00:00.000    50
11-2015  11   2015  2015-11-01 00:00:00  2015-11-30 00:00:00.000    100
01-2016   1   2016  2016-01-01 00:00:00  2016-01-31 00:00:00.000    125
11-2016  11   2016  2016-11-01 00:00:00  2016-11-30 00:00:00.000    55

你可以用这样的CTE

id   date          sales
1    01/01/2015    100
2    01/01/2015    100
3    02/01/2015    100
4    03/01/2015    100
Month  StartMonth  EndMonth     TotalSales
1      01/01/2015  01/31/2015   200
2      02/01/2015  02/28/2015   100
3      03/01/2015  03/31/2015   100
4      04/01/2015  04/30/2015   0
select [Month] = Month([date]), [date] as StartMonth, 
        EndMonth = convert(date, dateadd(month,datediff(month,0,'2017-02-11')+1,0)-1),
        TotalSales from (
    select [date],  sum(sales) as totalSales 
        from #yoursales
        group by [date]
        ) a
演示链接:

输出

;With cte(id,date,sales)
AS
(
SELECT 1,'01/01/2015',100 UNION ALL
SELECT 2,'01/01/2015',100 UNION ALL
SELECT 3,'02/01/2015',100 UNION ALL
SELECT 4,'03/01/2015',100 UNION ALL
SELECT 5,'04/01/2015',NULL 
)
SELECT [Id],CONVERT(VARCHAR(10),[DATE], 101) AS [DATE],
CONVERT(VARCHAR(10),[EndMonth],101) AS [EndMonth],
[SumOfSale] From
(
SELECT id,[DATE],EndMonth,SUM(sales) OVER(Partition by [DATE],EndMonth order by EndMonth) AS SumOfSale,
Row_NUmber ()OVER(Partition by [DATE],EndMonth order by id)As Seq From
(
SELECT id, CAST([DATE] AS DATE)[DATE], EOMONTH(date)AS EndMonth,ISNULL(sales,0)As Sales from cte
)Dt 
)Final 
WHERE Final.Seq=1

您的sql server版本是什么sql server 2008Hi,month,Start和EndMonth应该是自动的,动态的,而不是手动编码我制作的日期列别名Start date和End of month hi,month,Start和EndMonth应该是自动的,动态的,而不是手动编码当您在我的回答中看到什么是非自动的,还是非动态的?