计算列SQL的行数

时间:2017-11-21 08:46:22

标签: mysql sql

如何计算表中特定列的行数:

ReportID  Reader  ReadTime
100        A       12:00
100        A       12:10
100        A       12:15 
200        B       15:00
200        B       15:00
200        B       15:05

预期OutCome:

ReportID  Reader  ReadTime  Count Read by Reader and Time
100        A       12:00    1
100        A       12:10    1
100        A       12:15    1
200        B       15:00    2
200        B       15:00    2
200        B       15:05    1

5 个答案:

答案 0 :(得分:0)

简单做txtright.setText("" + result + "/" + "" + numberques); ...

count(*) over()

答案 1 :(得分:0)

您希望count没有group by,这是通过over完成的(所谓的窗口函数

COUNT(*) OVER (PARTITION BY ReportID, Reader, ReadTime)

这是否适用于您的数据库,我无法分辨(因为您没有标记)。

但是,这里有一些幻灯片解释窗口函数,并显示哪些DB支持它们。

https://www.slideshare.net/MarkusWinand/modern-sql/75

答案 2 :(得分:0)

如果你的dbms不支持窗口函数,那么一个简单的相关子查询就可以解决这个问题:

select t1.ReportID, t1.Reader, t1.ReadTime,
       (select count(*) from tablename t2
        where t2.ReportID = t1.ReportID
          and t2.Reader = t1.Reader
          and t2.ReadTime = t1.ReadTime) as cnt
from tablename t1

或者,使用派生表加入:

select t1.ReportID, t1.Reader, t1.ReadTime, t2.cnt
from tablename t1
join (select ReportID, Reader, ReadTime, count(*) as cnt
      from tablename
      group by ReportID, Reader, ReadTime) t2
    on  t2.ReportID = t1.ReportID
    and t2.Reader = t1.Reader
    and t2.ReadTime = t1.ReadTime 

答案 3 :(得分:0)

您可以使用:

Select reportid,reader,readtime,count(*) over (partition by reportid,reader,readtime) from table;

答案 4 :(得分:0)

尝试

Select reportid,reader,readtime,count(*) from table1
group by Reader,readtime

http://sqlfiddle.com/#!9/2b938d/12