当前位置: 代码迷 >> Sql Server >> 求某个年月内每日的销售总和
  详细解决方案

求某个年月内每日的销售总和

热度:84   发布时间:2016-04-24 10:03:39.0
求某个年月内每天的销售总和
订单表,有以下几个主要主段

ID     主键
PTIME 时间,格式如2013/9/11 15:17:01
PNUM 金额

记录如:

1          2014/8/1 15:17:01            125
2          2014/8/2 15:17:01            100.10
3          2014/8/2 15:17:01            200
4          2014/8/3 15:17:01            158
5          2013/8/5 15:17:01             130
............................

页面传过来两个值.一个是年份,一个是月份

比如:a.aspx?year=2014&month=8


想要得到2014年8月.1-31号每天的销售总额?

想要的结果是

1号  125
2号  300.10
3号  158
4号  0
5号  130


求大神帮忙!

------解决方案--------------------
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-08-27 15:43:45
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
-- May 14 2014 18:34:29 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[Tb]
if object_id('[Tb]') is not null drop table [Tb]
go 
create table [Tb]([id] int,[ptime] datetime,[pnum] numeric(5,2))
insert [Tb]
select 1,'2014/8/1 15:17:01',125 union all
select 2,'2014/8/2 15:17:01',100.10 union all
select 3,'2014/8/2 15:17:01',200 union all
select 4,'2014/8/3 15:17:01',158 union all
select 5,'2013/8/5 15:17:01',130
--------------开始查询--------------------------
DECLARE @startdate DATETIME ,    @enddate DATETIME
SET @startdate = '2014-01-01'
SET @enddate = '2014-12-31'
DECLARE @year CHAR(4)
SET @year='2014'
DECLARE @month VARCHAR(2)
SET @month='08'
;WITH cte AS (
SELECT 
    DISTINCT
        YEAR(CONVERT(VARCHAR(10), DATEADD(day, number, @startdate), 120)) AS [year] ,
        MONTH(CONVERT(VARCHAR(10), DATEADD(day, number, @startdate), 120)) AS [month] ,
        day(CONVERT(VARCHAR(10), DATEADD(day, number, @startdate), 120)) AS [day]
FROM    master..spt_values
WHERE   DATEDIFF(day, DATEADD(day, number, @startdate), @enddate) >= 0
        AND number >= 0
        AND type = 'p' AND YEAR(CONVERT(VARCHAR(10), DATEADD(day, number, @startdate), 120))=@year AND 
MONTH(CONVERT(VARCHAR(10), DATEADD(day, number, @startdate), 120))=@month)
select CAST(cte.[day] AS VARCHAR(2))+'号' 日期,SUM(ISNULL([pnum],0))[pnum]
from [Tb] RIGHT JOIN cte ON DATEPART(YEAR,ptime)=cte.year AND DATEPART(MONTH,ptime)=cte.MONTH
AND DATEPART(day,ptime)=cte.day
GROUP BY CAST(cte.[day] AS VARCHAR(2))+'号', cte.[day]
ORDER BY  cte.[day]
----------------结果----------------------------
/* 
日期   pnum
---- ---------------------------------------
1号   125.00
2号   300.10
3号   158.00
4号   0.00
5号   0.00
6号   0.00
7号   0.00
8号   0.00
9号   0.00
10号  0.00
11号  0.00
12号  0.00
13号  0.00
14号  0.00
15号  0.00
16号  0.00
17号  0.00
18号  0.00
19号  0.00
20号  0.00
21号  0.00
22号  0.00
23号  0.00
24号  0.00
25号  0.00
26号  0.00
27号  0.00
28号  0.00
29号  0.00
30号  0.00
31号  0.00

*/

------解决方案--------------------
之前有个网友就说过时间处理效率的问题导致查询大量数据会很慢,帮你优化了下


declare @year varchar(5)
declare @month varchar(5)
declare @begin_time datetime
declare @end_time datetime
declare @sql varchar(256)
set @year='2014'
set @month='8'
set @begin_time=@year+'/'+@month+'/'+'1 00:00:00'
select @end_time= dateadd(ss,-1,DATEADD(M,1,@begin_time))
  相关解决方案