日期:2014-05-17 浏览次数:20612 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (Day datetime,Qty int)
insert into [TB]
select '2012-2-1',11 union all
select '2012-2-2',10 union all
select '2012-2-3',2 union all
select '2012-2-4',5 union all
select '2012-2-14',6 union all
select '2012-2-15',4 union all
select '2012-2-16',8 union all
select '2012-2-26',8 union all
select '2012-3-16',1 union all
select '2012-3-17',1 union all
select '2012-3-18',5 union all
select '2012-3-19',6
select * from [TB]
SELECT SUBSTRING(CONVERT(VARCHAR(10), DAY, 120), 6, 2) + '月'
+ CASE WHEN SUBSTRING(CONVERT(VARCHAR(10), DAY, 120), 9, 2) > '15'
THEN '15日后'
ELSE '15日前'
END AS '日期' ,
SUM(qty) AS qty
FROM dbo.TB
GROUP BY SUBSTRING(CONVERT(VARCHAR(10), DAY, 120), 6, 2) + '月'
+ CASE WHEN SUBSTRING(CONVERT(VARCHAR(10), DAY, 120), 9, 2) > '15'
THEN '15日后'
ELSE '15日前'
END
/*
日期 qty
02月15日后 16
02月15日前 38
03月15日后 13*/
select Day,sum(Qty) from (
select Day=(case when 这里先转换一下格式为时间,在截取“日”<15 then),Qty from 表
)a
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (Day datetime,Qty int)
insert into [TB]
select '2012-2-1',11 union all
select '2012-2-2',10 union all
select '2012-2-3',2 union all
select '2012-2-4',5 union all
select '2012-2-14',6 union all
select '2012-2-15',4 union all
select '2012-2-16',8 union all
select '2012-2-26',8 union all
select '2012-3-16',1 union all
select '2012-3-17',1 union all
select '2012-3-18',5&