MySQL查询根据日期(字段总和)检索数据

时间:2019-02-18 12:21:11

标签: mysql

我是MySQL的新手,请耐心等候:)

我有一张桌子,我想根据日期检索数据并求和

这是我的桌子

client    datescanned  problem   title
 abc      2019-02-02    12345a         High      xyz
 abc      2019-02-02    12345b         High      xyz 
 abc      2019-02-02    12345c         High      xyz
 abc      2019-02-02    12345d         Medium    xyz
 abc      2019-02-09    12345e         High      xyz 
 abc      2019-02-09    12345f         High      xyz
 abc      2019-02-09    12345g         Low       xyz
 abc      2019-02-09    12345h         Low       xyz
 abc      2019-02-09    12345j         Low       xyz
 abc      2019-02-16    12345x         High      xyz
 abc      2019-02-16    12345s         High      xyz
 abc      2019-02-16    12345w         High      xyz
 abc      2019-02-16    12345bs        Medium    xyz

我想要的输出是

 client   datescanned  problem         High   Medium   Low 
 abc      2019-02-02    12345x          3       1       0
 abc      2019-02-09    12345s          2       0       3
 abc      2019-02-16    12345w          3       1       0 

这是我的代码

select client,datescanned, problem, severity, 

         count(case when severity = 'High' then 1 end) as High,
         count(case when severity = 'Medium' then 1 end) as Medium,
         count(case when severity = 'Low' then 1 end) as Low

 from ssstest where client = "myuser"
 group by client,datescanned, problem

这将正确地提取所有数字,但是我想对每个日期“合计”高,中,低。

现在我明白了。

client    datescanned  problem   severity      High  Medium  Low
 abc      2019-02-02    12345a      High        1      0      0
 abc      2019-02-02    12345b      High        1      0      0
 abc      2019-02-02    12345c      High        1      0      0 
 abc      2019-02-02    12345d      Medium      0      1      0 
 abc      2019-02-09    12345e      High        1      0      0 
 abc      2019-02-09    12345f      High        1      0      0 
 abc      2019-02-09    12345g      Low         0      0      1
 abc      2019-02-09    12345h      Low         0      0      1
 abc      2019-02-09    12345j      Low         0      0      1
 abc      2019-02-16    12345x      High        1      0      0
 abc      2019-02-16    12345s      High        1      0      0
 abc      2019-02-16    12345w      High        1      0      0
 abc      2019-02-16    12345bs     Medium      0      1      0 

所以基本上,我只想将“高”,“中”,“低”相加,将同一日期分组为一个日期。

 client   datescanned  problem         High   Medium   Low 
 abc      2019-02-02    12345x          3       1       0
 abc      2019-02-09    12345s          2       0       3
 abc      2019-02-16    12345w          3       1       0 

再次感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

您可以尝试使用conditional aggregation

select client,dateadded, computername, severity, 
       count(case when severity='High' then 1 end) as High,
       count(case when severity='Medium' then 1 end) as Medium,
       count(case when severity='Low' then 1 end) as Low,
from mytable where client =%CURRENT_USER_LOGIN%
group by client,dateadded, computername

答案 1 :(得分:0)

更新(1):

在MySQL / MariaDB查询中,以下内容没有多大意义:

=%CURRENT_USER_LOGIN%

如果您要搜索的子字符串为CURRENT_USER_LOGIN,请使用 like '%CURRENT_USER_LOGIN%'

select client,dateadded, computername, severity, 
       count(case when severity='High' then 1 end) as High,
       count(case when severity='Medium' then 1 end) as Medium,
-- comma mark ',' before 'from' !!! produces error
       count(case when severity='Low' then 1 end) as Low,
from mytable where client =%CURRENT_USER_LOGIN%
group by client,dateadded, computername