sql按天计算查询

c48111926 2010-01-04 06:36:06
Inspect_name=姓名nvarchar(4)
Intime=录入时间 datetime
vdatetime1= 用户输入的检查开始时间 smalldatetime
vdatetime2= 用户输入的检查结束时间 smalldatetime
vtime = asp自动计算得出的时间合
jcnr= 用户输入的内容 text

Inspect_name intime vdatetime1 vdatetime2 vtime jcnr
王 2009-1-4 0:33:52 2009-12-12 11:00:00 2009-12-12 14:00:00 60 内容
王 2009-1-4 :0: 33:52 2009-12-12 12:00:00 2009-12-12 13:00:00 60 内容
王 2009-1-5:12:32:50 2009-12-14 13:00:00 2009-12-14 14:00:00 60 内容
我想查两个月内用户每天发布内容数量,如果用户每天输入的几条信息加在一起不超过120分钟那么就过不算了,如果超过了就按1条计算。就是说按天计算得出结果,如果每天不超过120分之就不算,超过了就算个记录
显示成
Inspect_name 内容数量
王 1
...全文
421 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2010-01-05
  • 打赏
  • 举报
回复
select *,
得分=数量*(case
when 数量 between 1 and 5 then 1
when 数量 between 6 and 8 then 3
when 数量 between 9 and 12 then 5
when 数量>=13 then 7
end
)
from
(
select Inspect_name,sum(记录数) as 数量
from
(
select
Inspect_name,
convert(varchar(10),intime,120) as intime,
1 as 记录数
from
tb
group by
Inspect_name,
convert(varchar(10),intime,120)
where
intime between '2009-01-01' and '2009-01-06'
having
sum(vtime)>120
) t
group by Inspect_name
) tt
c48111926 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 c48111926 的回复:]
引用 14 楼 josy 的回复:
引用 13 楼 c48111926 的回复:
引用 10 楼 josy 的回复:
SQL codeselect Inspect_name,sum(记录数)as 记录数from
(select
  Inspect_name,convert(varchar(10),intime,120)as intime,1as 记录数from
  tbgroupby
  Inspect_name,convert(varchar(10),intime,120)where
  intimebe?-
老师您太神了.,哈老师能不能帮我再加一句数量上
1-5件加1分,6-8件加3分,9-12件加5分,13件及其以上加7分


最后的结果显示成什么样的?
姓名 数量    得分
王  5      5分
张  7      21分
李  10    50分
孙  19    133分
[/Quote]老师分这么少我都不好意思了.刚才分数打错了,老师我重新建个帖子您再帮看下,分数不要了,我想细分下,老师帮下忙.我想再统计 a1 a2 a3 这三个字段的数量,要在超过120分钟的基础上
c48111926 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 josy 的回复:]
引用 13 楼 c48111926 的回复:
引用 10 楼 josy 的回复:
SQL codeselect Inspect_name,sum(记录数)as 记录数from
(select
  Inspect_name,convert(varchar(10),intime,120)as intime,1as 记录数from
  tbgroupby
  Inspect_name,convert(varchar(10),intime,120)where
  intimebe?-
老师您太神了.,哈老师能不能帮我再加一句数量上
1-5件加1分,6-8件加3分,9-12件加5分,13件及其以上加7分


最后的结果显示成什么样的?
[/Quote]姓名 数量 得分
王 5 5分
张 7 21分
李 10 50分
孙 19 133分
c48111926 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 josy 的回复:]
引用 13 楼 c48111926 的回复:
引用 10 楼 josy 的回复:
SQL codeselect Inspect_name,sum(记录数)as 记录数from
(select
  Inspect_name,convert(varchar(10),intime,120)as intime,1as 记录数from
  tbgroupby
  Inspect_name,convert(varchar(10),intime,120)where
  intimebe?-
老师您太神了.,哈老师能不能帮我再加一句数量上
1-5件加1分,6-8件加3分,9-12件加5分,13件及其以上加7分


最后的结果显示成什么样的?
[/Quote]姓名 数量 得分
王 5 5分
张 7 21分
李 10 50分
孙 19 133分
百年树人 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 c48111926 的回复:]
引用 10 楼 josy 的回复:
SQL codeselect Inspect_name,sum(记录数)as 记录数from
(select
  Inspect_name,convert(varchar(10),intime,120)as intime,1as 记录数from
  tbgroupby
  Inspect_name,convert(varchar(10),intime,120)where
  intimebe?-
老师您太神了.,哈老师能不能帮我再加一句数量上
1-5件加1分,6-8件加3分,9-12件加5分,13件及其以上加7分
[/Quote]

最后的结果显示成什么样的?
c48111926 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 josy 的回复:]
SQL codeselect Inspect_name,sum(记录数)as 记录数from
(select
Inspect_name,convert(varchar(10),intime,120)as intime,1as 记录数from
tbgroupby
Inspect_name,convert(varchar(10),intime,120)where
intimebe?-
[/Quote]老师您太神了.,哈老师能不能帮我再加一句数量上
1-5件加1分,6-8件加3分,9-12件加5分,13件及其以上加7分
dawugui 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 c48111926 的回复:]
老师您再加个insert into tb values('王' , '2009-1-5 12:32:50', 120)
看下效果可以么?麻烦您了老师.
[/Quote]
--你如果尽早把你的需求和结果说清楚,也许不用让你等这么久.
create table tb(Inspect_name varchar(10), intime datetime, vtime int)
insert into tb values('王' , '2009-1-4 0:33:52' , 60)
insert into tb values('王' , '2009-1-4 0:33:52' , 65)
insert into tb values('王' , '2009-1-5 12:32:50', 100)
insert into tb values('王' , '2009-1-5 12:32:50', 120)
go

select Inspect_name , sum(记录数) 记录数 from
(
select Inspect_name ,记录数 = 1
from tb
where intime between '2009-01-01' and '2009-01-06'
group by Inspect_name ,convert(varchar(10),intime,120)
having sum(vtime) > 120
) t
group by Inspect_name

drop table tb

/*
Inspect_name 记录数
------------ -----------
王 2

(所影响的行数为 1 行)
*/
c48111926 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dawugui 的回复:]
引用 6 楼 c48111926 的回复:
老师能不能不显示时间直接王      2这个样子啊


SQL codecreatetable tb(Inspect_namevarchar(10), intimedatetime, vtimeint)insertinto tbvalues('王' ,'2009-1-4 0:33:52' ,60)insertinto tbvalues('王' ,'2009-1-4 0:33:52' ,65)insertinto tbvalues('王' ,'2009-1-5 12:32:50',100)goselect Inspect_name ,记录数=1from tbwhere intimebetween'2009-01-01'and'2009-01-06'groupby Inspect_name ,convert(varchar(10),intime,120)havingsum(vtime)>120droptable tb/*
Inspect_name 记录数
------------ -----------
王 1

(所影响的行数为 1 行)*/
[/Quote]老师您再加个insert into tb values('王' , '2009-1-5 12:32:50', 120)
看下效果可以么?麻烦您了老师.
百年树人 2010-01-04
  • 打赏
  • 举报
回复
select Inspect_name,sum(记录数) as 记录数
from
(
select
Inspect_name,
convert(varchar(10),intime,120) as intime,
1 as 记录数
from
tb
group by
Inspect_name,
convert(varchar(10),intime,120)
where
intime between '2009-01-01' and '2009-01-06'
having
sum(vtime)>120
) t
group by Inspect_name
百年树人 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 c48111926 的回复:]
引用 2 楼 dawugui 的回复:
SQL codeselect Inspect_name ,convert(varchar(10),intime,120) intime , 记录数=1from tbwhere intimebetween'2009-01-01'and'2009-01-06'groupby  Inspect_name ,convert(varchar(10),intime,120)havingsum(vtime)>1?-
老师,我现在显示的是
王    2009-01-04    1
王    2009-01-05    1
老师能不能不显示时间直接王      2这个样子啊
[/Quote]

select Inspect_name,sum(记录数) as 记录数
from
(
select
Inspect_name,
convert(varchar(10),intime,120) as intime,
1 as 记录数
from
tb
group by
Inspect_name,
convert(varchar(10),intime,120)
having
sum(vtime)>120
) t
group by Inspect_name
dawugui 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 c48111926 的回复:]
老师能不能不显示时间直接王      2这个样子啊
[/Quote]

create table tb(Inspect_name varchar(10), intime datetime, vtime int)
insert into tb values('王' , '2009-1-4 0:33:52' , 60)
insert into tb values('王' , '2009-1-4 0:33:52' , 65)
insert into tb values('王' , '2009-1-5 12:32:50', 100)
go

select Inspect_name ,记录数 = 1
from tb
where intime between '2009-01-01' and '2009-01-06'
group by Inspect_name ,convert(varchar(10),intime,120)
having sum(vtime) > 120

drop table tb

/*
Inspect_name 记录数
------------ -----------
王 1

(所影响的行数为 1 行)
*/
c48111926 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acmain_chm 的回复:]
select Inspect_name,count(*)
from(
select Inspect_name,convert(varchar(10),intime,120)
from table1
where Inspect_name='王'
and intime between '2009-1-1' and '2009-1-6'
group by Inspect_name,convert(varchar(10),intime,120)
having sum(vtime) > 120
)
group by Inspect_name

[/Quote]老师可以去掉Inspect_name='王'这个么?因为我的姓名太多了,这样我~~~~
c48111926 2010-01-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
SQL codeselect Inspect_name ,convert(varchar(10),intime,120) intime , 记录数=1from tbwhere intimebetween'2009-01-01'and'2009-01-06'groupby Inspect_name ,convert(varchar(10),intime,120)havingsum(vtime)>1?-
[/Quote]老师,我现在显示的是
王 2009-01-04 1
王 2009-01-05 1
老师能不能不显示时间直接王 2这个样子啊
ACMAIN_CHM 2010-01-04
  • 打赏
  • 举报
回复
select Inspect_name,count(*)
from(
select Inspect_name,convert(varchar(10),intime,120)
from table1
where Inspect_name='王'
and intime between '2009-1-1' and '2009-1-6'
group by Inspect_name,convert(varchar(10),intime,120)
having sum(vtime) > 120
)
group by Inspect_name
百年树人 2010-01-04
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Inspect_name] varchar(2),[intime] datetime,[vtime] int,[jcnr] varchar(4))
insert [tb]
select '王','2009-1-4 00:33:52',60,'内容' union all
select '王','2009-1-4 000:33:52',65,'内容' union all
select '王','2009-1-5 12:32:50',100,'内容'

---查询---
select
Inspect_name,
convert(varchar(10),intime,120) as intime,
1 as 记录数
from
tb
group by
Inspect_name,
convert(varchar(10),intime,120)
having
sum(vtime)>=120

---结果---
Inspect_name intime 记录数
------------ ---------- -----------
王 2009-01-04 1

(所影响的行数为 1 行)
dawugui 2010-01-04
  • 打赏
  • 举报
回复
create table tb(Inspect_name varchar(10), intime datetime, vtime int)
insert into tb values('王' , '2009-1-4 0:33:52' , 60)
insert into tb values('王' , '2009-1-4 0:33:52' , 65)
insert into tb values('王' , '2009-1-5 12:32:50', 100)
go

select Inspect_name ,convert(varchar(10),intime,120) intime , 记录数 = 1
from tb
where intime between '2009-01-01' and '2009-01-06'
group by Inspect_name ,convert(varchar(10),intime,120)
having sum(vtime) > 120

drop table tb

/*
Inspect_name intime 记录数
------------ ---------- -----------
王 2009-01-04 1

(所影响的行数为 1 行)
*/
dawugui 2010-01-04
  • 打赏
  • 举报
回复
select Inspect_name ,convert(varchar(10),intime,120) intime , 记录数 = 1
from tb
where intime between '2009-01-01' and '2009-01-06'
group by Inspect_name ,convert(varchar(10),intime,120)
having sum(vtime) > 120
c48111926 2010-01-04
  • 打赏
  • 举报
回复
补充一下
Inspect_name intime vtime jcnr
王 2009-1-4 0:33:52 60 内容
王 2009-1-4 0:33:52 65 内容
王 2009-1-5:12:32:50 100 内容
这是我库里的内容, intime 是录入时间 ,比如我where 时间 between '2009-1-1' and '2009-1-6'
数据则显示:

inspect_name intime 记录数
王 2009-1-4 1
也就是说,按天统计后查询.每天不管王输入多少条信息,只要vtime当天记录相加和不大于120那么就不显示,如果王输入1000条记录当天vtime相加大于120那么就算1条记录而不是1000条

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧