查询在表中的间隔内计数

时间:2017-01-30 18:15:00

标签: php html sql sql-server

我有这张桌子:

CREATE TABLE pgoals (
gname varchar(100) REFERENCES pplayers(name),
minute integer,
date date,
PRIMARY KEY (gname,minute,date) ) ;

该表显然已经充满了许多目标。

除此之外,我想得到一个查询,显示在十分钟(1-10, 11-20, 21-30 etc.)的间隔内得分的目标数。但是,我不知道我怎么能这样做,除了我应该使用COUNT命令。

有什么想法吗?

提前致谢!

5 个答案:

答案 0 :(得分:4)

这就是你所需要的,小而优雅的:

SELECT gname, date, CEILING(minute/10) AS minuteInterval
FROM     pgoals
GROUP BY minuteInterval;

答案 1 :(得分:2)

这是一个带有左连接和ad-hoc计数表的选项

Declare @pgoals  table (gname varchar(100) , minute integer,date date)
Insert Into @pgoals values
 ('aa',4,'2016-01-01')
,('aa',10,'2016-01-01')
,('aa',15,'2016-01-01')
,('aa',25,'2016-01-01')

Select Interval = concat(N-9,' - ',N)
      ,Goals = count(minute)
 From (Select Top 10  N=10*Row_Number() Over (Order By Number) From master..spt_values ) A
 Left Join @pgoals B
   on minute between N-9 and N
 Group By N

返回

Interval    Goals
1 - 10      2
11 - 20     1
21 - 30     1
31 - 40     0
41 - 50     0
51 - 60     0
61 - 70     0
71 - 80     0
81 - 90     0
91 - 100    0

答案 2 :(得分:1)

根据我提出的最新信息,请查看是否有用

    declare  @goal as table (Id int ,name nvarchar(5) , [minute] int, [date] datetime )


         insert into @goal (Id ,name  ,[minute] , [date])values (1,'A',1,GETDATE())      
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'B',3,GETDATE())
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'B',4,GETDATE()) 
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'B',5,GETDATE())          
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'A',11,GETDATE())         
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'A',13,GETDATE())
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'B',14,GETDATE())
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'B',15,GETDATE())
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'A',23,GETDATE())
         insert into @goal (Id ,name  ,[minute] , [date])values (1,'A',33,GETDATE())



         select count(*)as goals,name,
        case when ([minute]/10) = 0 then 10 else (minute/10)*10 end as mints,

         date from @goal group by minute,name,date

答案 3 :(得分:1)

drop table pgoals;

CREATE TABLE pgoals (
gname varchar(100),
minute integer,
date date,
PRIMARY KEY (gname,minute,date) ) ;

insert into pgoals (gname  ,[minute] , [date])values ('A',1,GETDATE())      
         insert into pgoals (gname  ,[minute] , [date])values ('B',3,GETDATE());
         insert into pgoals (gname  ,[minute] , [date])values ('B',4,GETDATE()) ;
         insert into pgoals (gname  ,[minute] , [date])values ('B',5,GETDATE())   ;       
         insert into pgoals (gname  ,[minute] , [date])values ('A',11,GETDATE()) ;        
         insert into pgoals (gname  ,[minute] , [date])values ('A',13,GETDATE());
         insert into pgoals (gname  ,[minute] , [date])values ('B',14,GETDATE());
         insert into pgoals (gname  ,[minute] , [date])values ('B',15,GETDATE());
         insert into pgoals (gname  ,[minute] , [date])values ('A',23,GETDATE());
         insert into pgoals (gname  ,[minute] , [date])values ('A',33,GETDATE());

select date,
       minute/10*10 at,
       count(*)
  from pgoals
 group by date,
       minute/10;

date    at  goals
2017-01-30  0   4
2017-01-30  10  4
2017-01-30  20  1
2017-01-30  30  1

答案 4 :(得分:1)

rextester链接:windows

create table pgoals (
    gname varchar(100)
  , minute integer
  , date date
  , PRIMARY KEY (gname,minute,date) 
);
insert into pgoals values
 ('a',1,'20170130')
,('a',9,'20170130')
,('a',12,'20170130')
,('a',14,'20170130')
,('a',34,'20170130')
,('a',54,'20170130');

查询

with m as (
  select nf, nt 
    from (values (1,10),(11,20),(21,30),(31,40),(41,50),(51,60)) t(nf, nt)
)
select 
    [interval] ='('+convert(varchar(3),m.nf)+ '-' +convert(varchar(3),m.nt)+')'
  , goals      = count(g.minute)
  from m
    left join pgoals g on g.minute between m.nf and m.nt
  group by m.nf, m.nt
  order by m.nf

结果:

+----------+-------+
| interval | goals |
+----------+-------+
| (1-10)   |     2 |
| (11-20)  |     2 |
| (21-30)  |     0 |
| (31-40)  |     1 |
| (41-50)  |     0 |
| (51-60)  |     1 |
+----------+-------+