日期:2014-05-16 浏览次数:20445 次
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-07 16:12:33
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([销售时间] datetime,[销售商品] varchar(1),[销售数量] int,[单价] numeric(2,1))
insert [tb]
select '2014-01-01 09:01','a',2,3.0 union all
select '2014-01-01 09:05','a',1,3.0 union all
select '2014-01-01 10:03','b',2,3.0 union all
select '2014-01-01 14:03','a',1,3.0 union all
select '2014-01-02 08:25','a',2,3.0 union all
select '2014-01-02 09:03','a',2,3.0 union all
select '2014-01-02 10:03','b',1,3.0
--------------开始查询--------------------------
SELECT
CONVERT(VARCHAR(10),销售时间,120) AS 时间,
SUM(CASE WHEN CONVERT(VARCHAR(5),销售时间,108) BETWEEN '08:25' AND '09:25' THEN 销售数量 ELSE 0 END) AS 销售数量,
' 8:25 - 9:25' AS 时间段
FROM
tb
GROUP BY
CONVERT(VARCHAR(10),销售时间,120)
----------------结果---