name time
李李 2011-02-13 11:07:13.000
丽丽 2011-02-14 12:07:08.000
李李 2011-02-14 12:10:02.000
李李 2011-02-14 18:03:26.000
李李 2011-02-14 18:05:27.000
李李 2011-02-14 20:07:08.000
丽丽 2011-02-15 06:01:01.000
丽丽 2011-02-15 12:07:08.000
丽丽 2011-02-15 17:55:06.000
要求:(1)一个人在时间段11:00分到13:00分 和 17:00到19:00为有效时间,其它时间段视为无效。
(2)在有效的时间段内,上午段和下午段各只能出现一次时间。
(3)得到表格内容如下:
日期 人数
2011-02-13 1
2011-02-14 3
2011-02-15 2
请问怎样写sql语句才能实现效果如上的表格呢?在线等待。。。。。。。。。。。。。。。。
12 个解决方案
#1
select convert(char(10),time,120),count(1)
from table where (11<=datepart(hour,time) and <=13) or 17<=datepart(hour,time) and <=19
group by convert(char(10),time,120)
#2
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-04-20 17:07:47
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(4),[time] datetime)
insert [tb]
select '李李','2011-02-13 11:07:13.000' union all
select '丽丽','2011-02-14 12:07:08.000' union all
select '李李','2011-02-14 12:10:02.000' union all
select '李李','2011-02-14 18:03:26.000' union all
select '李李','2011-02-14 18:05:27.000' union all
select '李李','2011-02-14 20:07:08.000' union all
select '丽丽','2011-02-15 06:01:01.000' union all
select '丽丽','2011-02-15 12:07:08.000' union all
select '丽丽','2011-02-15 17:55:06.000'
--------------开始查询--------------------------
select
convert(varchar(10),time,120) as 日期,
count(distinct time) as 人数
from
tb
where
convert(varchar(5),time,108) between '11:00' and '13:00' or convert(varchar(5),time,108) between '17:00' and '19:00'
group by
convert(varchar(10),time,120)
----------------结果----------------------------
/* 日期 人数
---------- -----------
2011-02-13 1
2011-02-14 4
2011-02-15 2
(3 行受影响)
*/
#3
select convert(varchar(10),日期,120) 日期,
人数=sum(case when (datepart(hh,日期) between 11 and 13) or
(datepart(hh,日期) between 17 and 19) then count(distinct name)
else 0 end)
from tb group by convert(varchar(10),日期,120)
#4
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(4),[time] datetime)
insert [tb]
select '李李','2011-02-13 11:07:13.000' union all
select '丽丽','2011-02-14 12:07:08.000' union all
select '李李','2011-02-14 12:10:02.000' union all
select '李李','2011-02-14 18:03:26.000' union all
select '李李','2011-02-14 18:05:27.000' union all
select '李李','2011-02-14 20:07:08.000' union all
select '丽丽','2011-02-15 06:01:01.000' union all
select '丽丽','2011-02-15 12:07:08.000' union all
select '丽丽','2011-02-15 17:55:06.000'
select convert(varchar(10),[time],120) [time],
人数=count (distinct (case when (datepart(hh,[time]) between 11 and 13) or
(datepart(hh,[time]) between 17 and 19) then name end))
from tb group by convert(varchar(10),[time],120)
/*
time 人数
---------- -----------
2011-02-13 1
2011-02-14 2
2011-02-15 1
#5
我测试测试先!真的非常感谢各位的指教。Thank very much!
#6
有点问题 没有按照姓名分组
#7
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] nvarchar(4),[time] datetime)
insert [tb]
select N'李李','2011-02-13 11:07:13.000' union all
select N'丽丽','2011-02-14 12:07:08.000' union all
select N'李李','2011-02-14 12:10:02.000' union all
select N'李李','2011-02-14 18:03:26.000' union all
select N'李李','2011-02-14 18:05:27.000' union all
select N'李李','2011-02-14 20:07:08.000' union all
select N'丽丽','2011-02-15 06:01:01.000' union all
select N'丽丽','2011-02-15 12:07:08.000' union all
select N'丽丽','2011-02-15 17:55:06.000'
go
--SQL:
SELECT [date], cnt = SUM(cnt) FROM
(
SELECT
[date] = CONVERT(CHAR(10), [time], 121),
N'时间段' = CASE
WHEN (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) THEN N'上午'
WHEN (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19) THEN N'下午'
END,
cnt = COUNT(DISTINCT [name])
FROM TB
WHERE (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) OR (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19)
GROUP BY CONVERT(CHAR(10), [time], 121),
CASE
WHEN (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) THEN N'上午'
WHEN (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19) THEN N'下午'
END
) t
GROUP BY [date]
/*
2011-02-13 1
2011-02-14 3
2011-02-15 2
*/
#8
或者用CTE:
;WITH cte AS
(
SELECT *,
N'时间段' = CASE
WHEN (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) THEN N'上午'
WHEN (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19) THEN N'下午'
END
FROM tb
)
SELECT [date], cnt = SUM(cnt) FROM
(
SELECT
[date] = CONVERT(CHAR(10), [time], 121),
时间段,
cnt = COUNT(DISTINCT [name])
FROM cte
WHERE 时间段 IS NOT NULL
GROUP BY CONVERT(CHAR(10), [time], 121), 时间段
) t
GROUP BY [date]
#9
--修改下
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(4),[time] datetime)
insert [tb]
select '李李','2011-02-13 11:07:13.000' union all
select '丽丽','2011-02-14 12:07:08.000' union all
select '李李','2011-02-14 12:10:02.000' union all
select '李李','2011-02-14 18:03:26.000' union all
select '李李','2011-02-14 18:05:27.000' union all
select '李李','2011-02-14 20:07:08.000' union all
select '丽丽','2011-02-15 06:01:01.000' union all
select '丽丽','2011-02-15 12:07:08.000' union all
select '丽丽','2011-02-15 17:55:06.000'
select convert(varchar(10),[time],120) [time],
人数=count (distinct (case when (datepart(hh,[time]) between 11 and 13) then name+'1'
when (datepart(hh,[time]) between 17 and 19) then name+'2' end))
from tb group by convert(varchar(10),[time],120)
/*
time 人数
---------- -----------
2011-02-13 1
2011-02-14 3
2011-02-15 2
#10
我再补充一点数据库记录内容:
name time
李李 2011-02-13 11:07:13.000
丽丽 2011-02-14 12:07:08.000
李李 2011-02-14 12:10:02.000
李李 2011-02-14 18:03:26.000
李李 2011-02-14 18:05:27.000
李李 2011-02-14 20:07:08.000
丽丽 2011-02-15 06:01:01.000
丽丽 2011-02-15 12:07:08.000
丽丽 2011-02-15 17:55:06.000
李李 2011-03-01 12:05:06.000
丽丽 2011-03-01 15:05:06.000
李李 2011-04-02 12:05:06.000
李李 2011-04-02 12:06:06.000
李李 2011-04-02 17:05:06.000
还有一个条件:
要求:(4)统计2011年2月的每一天人数
(1)一个人在时间段11:00分到13:00分 和 17:00到19:00为有效时间,其它时间段视为无效。
(2)在有效的时间段内,上午段和下午段各只能出现一次时间。
(3)得到表格内容如下:
日期 人数
2011-02-13 1
2011-02-14 3
2011-02-15 2
name time
李李 2011-02-13 11:07:13.000
丽丽 2011-02-14 12:07:08.000
李李 2011-02-14 12:10:02.000
李李 2011-02-14 18:03:26.000
李李 2011-02-14 18:05:27.000
李李 2011-02-14 20:07:08.000
丽丽 2011-02-15 06:01:01.000
丽丽 2011-02-15 12:07:08.000
丽丽 2011-02-15 17:55:06.000
李李 2011-03-01 12:05:06.000
丽丽 2011-03-01 15:05:06.000
李李 2011-04-02 12:05:06.000
李李 2011-04-02 12:06:06.000
李李 2011-04-02 17:05:06.000
还有一个条件:
要求:(4)统计2011年2月的每一天人数
(1)一个人在时间段11:00分到13:00分 和 17:00到19:00为有效时间,其它时间段视为无效。
(2)在有效的时间段内,上午段和下午段各只能出现一次时间。
(3)得到表格内容如下:
日期 人数
2011-02-13 1
2011-02-14 3
2011-02-15 2
#11
某一些功能实现到,但并没实现到某一年某一月这个要求!
#12
谢谢,这个方法可行!
#1
select convert(char(10),time,120),count(1)
from table where (11<=datepart(hour,time) and <=13) or 17<=datepart(hour,time) and <=19
group by convert(char(10),time,120)
#2
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-04-20 17:07:47
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(4),[time] datetime)
insert [tb]
select '李李','2011-02-13 11:07:13.000' union all
select '丽丽','2011-02-14 12:07:08.000' union all
select '李李','2011-02-14 12:10:02.000' union all
select '李李','2011-02-14 18:03:26.000' union all
select '李李','2011-02-14 18:05:27.000' union all
select '李李','2011-02-14 20:07:08.000' union all
select '丽丽','2011-02-15 06:01:01.000' union all
select '丽丽','2011-02-15 12:07:08.000' union all
select '丽丽','2011-02-15 17:55:06.000'
--------------开始查询--------------------------
select
convert(varchar(10),time,120) as 日期,
count(distinct time) as 人数
from
tb
where
convert(varchar(5),time,108) between '11:00' and '13:00' or convert(varchar(5),time,108) between '17:00' and '19:00'
group by
convert(varchar(10),time,120)
----------------结果----------------------------
/* 日期 人数
---------- -----------
2011-02-13 1
2011-02-14 4
2011-02-15 2
(3 行受影响)
*/
#3
select convert(varchar(10),日期,120) 日期,
人数=sum(case when (datepart(hh,日期) between 11 and 13) or
(datepart(hh,日期) between 17 and 19) then count(distinct name)
else 0 end)
from tb group by convert(varchar(10),日期,120)
#4
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(4),[time] datetime)
insert [tb]
select '李李','2011-02-13 11:07:13.000' union all
select '丽丽','2011-02-14 12:07:08.000' union all
select '李李','2011-02-14 12:10:02.000' union all
select '李李','2011-02-14 18:03:26.000' union all
select '李李','2011-02-14 18:05:27.000' union all
select '李李','2011-02-14 20:07:08.000' union all
select '丽丽','2011-02-15 06:01:01.000' union all
select '丽丽','2011-02-15 12:07:08.000' union all
select '丽丽','2011-02-15 17:55:06.000'
select convert(varchar(10),[time],120) [time],
人数=count (distinct (case when (datepart(hh,[time]) between 11 and 13) or
(datepart(hh,[time]) between 17 and 19) then name end))
from tb group by convert(varchar(10),[time],120)
/*
time 人数
---------- -----------
2011-02-13 1
2011-02-14 2
2011-02-15 1
#5
我测试测试先!真的非常感谢各位的指教。Thank very much!
#6
有点问题 没有按照姓名分组
#7
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] nvarchar(4),[time] datetime)
insert [tb]
select N'李李','2011-02-13 11:07:13.000' union all
select N'丽丽','2011-02-14 12:07:08.000' union all
select N'李李','2011-02-14 12:10:02.000' union all
select N'李李','2011-02-14 18:03:26.000' union all
select N'李李','2011-02-14 18:05:27.000' union all
select N'李李','2011-02-14 20:07:08.000' union all
select N'丽丽','2011-02-15 06:01:01.000' union all
select N'丽丽','2011-02-15 12:07:08.000' union all
select N'丽丽','2011-02-15 17:55:06.000'
go
--SQL:
SELECT [date], cnt = SUM(cnt) FROM
(
SELECT
[date] = CONVERT(CHAR(10), [time], 121),
N'时间段' = CASE
WHEN (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) THEN N'上午'
WHEN (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19) THEN N'下午'
END,
cnt = COUNT(DISTINCT [name])
FROM TB
WHERE (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) OR (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19)
GROUP BY CONVERT(CHAR(10), [time], 121),
CASE
WHEN (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) THEN N'上午'
WHEN (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19) THEN N'下午'
END
) t
GROUP BY [date]
/*
2011-02-13 1
2011-02-14 3
2011-02-15 2
*/
#8
或者用CTE:
;WITH cte AS
(
SELECT *,
N'时间段' = CASE
WHEN (datepart(HOUR,[time]) >= 11 AND datepart(HOUR,[time]) <= 13) THEN N'上午'
WHEN (datepart(HOUR,[time]) >= 17 AND datepart(HOUR,[time]) <= 19) THEN N'下午'
END
FROM tb
)
SELECT [date], cnt = SUM(cnt) FROM
(
SELECT
[date] = CONVERT(CHAR(10), [time], 121),
时间段,
cnt = COUNT(DISTINCT [name])
FROM cte
WHERE 时间段 IS NOT NULL
GROUP BY CONVERT(CHAR(10), [time], 121), 时间段
) t
GROUP BY [date]
#9
--修改下
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(4),[time] datetime)
insert [tb]
select '李李','2011-02-13 11:07:13.000' union all
select '丽丽','2011-02-14 12:07:08.000' union all
select '李李','2011-02-14 12:10:02.000' union all
select '李李','2011-02-14 18:03:26.000' union all
select '李李','2011-02-14 18:05:27.000' union all
select '李李','2011-02-14 20:07:08.000' union all
select '丽丽','2011-02-15 06:01:01.000' union all
select '丽丽','2011-02-15 12:07:08.000' union all
select '丽丽','2011-02-15 17:55:06.000'
select convert(varchar(10),[time],120) [time],
人数=count (distinct (case when (datepart(hh,[time]) between 11 and 13) then name+'1'
when (datepart(hh,[time]) between 17 and 19) then name+'2' end))
from tb group by convert(varchar(10),[time],120)
/*
time 人数
---------- -----------
2011-02-13 1
2011-02-14 3
2011-02-15 2
#10
我再补充一点数据库记录内容:
name time
李李 2011-02-13 11:07:13.000
丽丽 2011-02-14 12:07:08.000
李李 2011-02-14 12:10:02.000
李李 2011-02-14 18:03:26.000
李李 2011-02-14 18:05:27.000
李李 2011-02-14 20:07:08.000
丽丽 2011-02-15 06:01:01.000
丽丽 2011-02-15 12:07:08.000
丽丽 2011-02-15 17:55:06.000
李李 2011-03-01 12:05:06.000
丽丽 2011-03-01 15:05:06.000
李李 2011-04-02 12:05:06.000
李李 2011-04-02 12:06:06.000
李李 2011-04-02 17:05:06.000
还有一个条件:
要求:(4)统计2011年2月的每一天人数
(1)一个人在时间段11:00分到13:00分 和 17:00到19:00为有效时间,其它时间段视为无效。
(2)在有效的时间段内,上午段和下午段各只能出现一次时间。
(3)得到表格内容如下:
日期 人数
2011-02-13 1
2011-02-14 3
2011-02-15 2
name time
李李 2011-02-13 11:07:13.000
丽丽 2011-02-14 12:07:08.000
李李 2011-02-14 12:10:02.000
李李 2011-02-14 18:03:26.000
李李 2011-02-14 18:05:27.000
李李 2011-02-14 20:07:08.000
丽丽 2011-02-15 06:01:01.000
丽丽 2011-02-15 12:07:08.000
丽丽 2011-02-15 17:55:06.000
李李 2011-03-01 12:05:06.000
丽丽 2011-03-01 15:05:06.000
李李 2011-04-02 12:05:06.000
李李 2011-04-02 12:06:06.000
李李 2011-04-02 17:05:06.000
还有一个条件:
要求:(4)统计2011年2月的每一天人数
(1)一个人在时间段11:00分到13:00分 和 17:00到19:00为有效时间,其它时间段视为无效。
(2)在有效的时间段内,上午段和下午段各只能出现一次时间。
(3)得到表格内容如下:
日期 人数
2011-02-13 1
2011-02-14 3
2011-02-15 2
#11
某一些功能实现到,但并没实现到某一年某一月这个要求!
#12
谢谢,这个方法可行!