SQL-对重复行进行分组和计数

时间:2018-08-05 15:06:10

标签: mysql sql

我不知道如何在mysql上对重复行进行分组和计数

以下是我从查询中获得的结果

       ssn              +       checktime           +   nama            
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     
'196702031989031001'    +   '2018-08-03 07:33:02'   +   'FAJAR PERMADI'
'196810021993031001'    +   '2018-08-01 07:33:25'   +   'ANDRI ANGGORO, SH'
'196911052000031001'    +   '2018-08-03 07:47:22'   +   'SEMI TEDDY RORY, SS'
'196912221994032001'    +   '2018-08-01 08:03:59'   +   'AI SALATUN'
'196912221994032001'    +   '2018-08-02 09:34:11'   +   'AI SALATUN'
'196912221994032001'    +   '2018-08-03 07:33:18'   +   'AI SALATUN'
'197012051993031001'    +   '2018-08-01 07:58:47'   +   'AHMAD SODIKIN, SH'
'197012192001121001'    +   '2018-08-01 09:54:21'   +   'JUARA PAHALA MARBUN, ST'
'197012192001121001'    +   '2018-08-02 09:39:41'   +   'JUARA PAHALA MARBUN, ST'

以下是我的查询

SELECT a.ssn, a.checktime, b.nama
FROM hki_kepegawaian.fo_absensi a
left join hki_kepegawaian.fo_pegawai b on a.ssn = b.nip  
where (substring(cast(checktime as DATE), 6, 2) = '08') 
and (cast(a.checktime as TIME)) >= '07:30:00' and (cast(a.checktime as 
TIME)) <= '10:00:00'
and (substring(golongan, 1, 2)) NOT IN ('IV')
group by ssn, cast(a.checktime as date)

及以下是我期望的结果

        ssn             +       checktime           +   nama                        +   total
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                                                                                                   
'196702031989031001'    +   '2018-08-03 07:33:02'   +   'FAJAR PERMADI'             +   1
'196810021993031001'    +   '2018-08-01 07:33:25'   +   'ANDRI ANGGORO, SH'         +   1
'196911052000031001'    +   '2018-08-03 07:47:22'   +   'SEMI TEDDY RORY, SS'       +   1
'196912221994032001'    +   '2018-08-01 08:03:59'   +   'AI SALATUN'                +   3
'197012051993031001'    +   '2018-08-01 07:58:47'   +   'AHMAD SODIKIN, SH'         +   1
'197012192001121001'    +   '2018-08-01 09:54:21'   +   'JUARA PAHALA MARBUN, ST'   +   2

3 个答案:

答案 0 :(得分:0)

您的预期输出意味着您想报告每个ssn / nama记录组具有最早检查时间的记录。对于计数,它看起来就像每个组中的记录总数。

SELECT
    a.ssn,
    MIN(CAST(a.checktime AS date)) AS checktime,
    b.nama,
    COUNT(*) AS total
FROM hki_kepegawaian.fo_absensi a
LEFT JOIN hki_kepegawaian.fo_pegawai b
    ON a.ssn = b.nip  
WHERE
    MONTH(checktime) = 8 AND
    CAST(a.checktime AS TIME) BETWEEN '07:30:00' AND '10:00:00' AND
    SUBSTRING(golongan, 1, 2)) <> 'IV'
GROUP BY
    a.ssn, CAST(a.checktime AS date);

答案 1 :(得分:0)

我同意蒂姆的观点,您似乎想花最早的时间。在这种情况下,这是通过group by完成的。

但是,我建议对查询进行其他一些修复:

  • 请勿在日期/时间使用字符串操作。
  • 使用有意义的表别名,这些别名是表的缩写。
  • GROUP BY中包括所有未聚合的列。
  • 在适当的地方使用LIKE

所以,我建议:

SELECT a.ssn, a.checktime, p.nama
FROM hki_kepegawaian.fo_absensi a LEFT JOIN
     hki_kepegawaian.fo_pegawai b 
     ON a.ssn = p.nip  
WHERE MONTH(checktime) = 8 AND
      CAST(a.checktime as TIME) >= '07:30:00' AND
      CAST(a.checktime as TIME)) <= '10:00:00' AND
      golongan NOT LIKE 'IV%'
GROUP BY a.ssn, p.nama;

答案 2 :(得分:0)

查看count() function

我无法检查它是否有效,但是请尝试以下查询:

SELECT a.ssn, a.checktime, b.nama, count(*) as total
FROM hki_kepegawaian.fo_absensi a
left join hki_kepegawaian.fo_pegawai b on a.ssn = b.nip  
where (substring(cast(checktime as DATE), 6, 2) = '08') 
and (cast(a.checktime as TIME)) >= '07:30:00' and (cast(a.checktime as 
TIME)) <= '10:00:00'
and (substring(golongan, 1, 2)) NOT IN ('IV')
group by ssn, nama
Having total>=1