mysql - 具有条件的 MySQL 中 Row 中的值总和

标签 mysql database group-by

我有下表。我想通过对日期设置条件来对行中存在的值求和。

Table - a
  ID   Entry_date     weight  height   ID1      start_date  end_date
 111   2001-03-31      43      187
 111   2001-04-30      23      165
 111   2001-05-31      34      172     111      2001-04-30  2001-05-31 
 112   2001-06-30      54      183
 112   2001-07-31      26      188     112      2001-06-30  2001-07-31
 113   2001-04-30      23      165
 113   2001-05-31      34      172     113      2001-04-30  2001-05-31 
 114   2001-05-31      46      177
 114   2001-06-30      54      183
 114   2001-07-31      26      188     114      2001-06-30  2001-07-31

如果Entry_date >= start_date and Entry_date <= end_date那么它应该对那些 entry_dates 和 ID 的重量和高度求和。我不知道我是否以正确的方式解释了它。结果表应该是这样的:

Table - 

      ID   Entry_date     weight  height   ID1      start_date  end_date
     111   2001-03-31      43      187    
     111   2001-05-31      57      337     111      2001-04-30  2001-05-31 
     112   2001-07-31      80      371     112      2001-06-30  2001-07-31 
     113   2001-05-31      57      337     113      2001-04-30  2001-05-31 
     114   2001-05-31      46      177
     114   2001-07-31      80      371     114      2001-06-30  2001-07-31

我试过以下:

Select ID, Entry_date, sum(weight) as weight, sum(height) as height, ID1, start_date, end_date from table a 
where Entry_date >= start_date and Entry_date <= end_date group by ID, Entry_date;

但它没有给我想要的结果。它给了我以下结果:

  Table - a
          ID   Entry_date     weight  height   ID1      start_date  end_date
         111   2001-05-31      34      172     111      2001-04-30  2001-05-31 
         112   2001-07-31      26      188     112      2001-06-30  2001-07-31
         113   2001-05-31      34      172     113      2001-04-30  2001-05-31 
         114   2001-07-31      26      188     114      2001-06-30  2001-07-31

任何人都可以告诉我在这种情况下如何进行吗?

最佳答案

这有点复杂。这是您可以尝试的查询:

select
  id, entry_date,

  (select sum(weight) from test 
   where entry_date between a.start_date and a.end_date
   and id = a.id) as weight,

  (select sum(height) from test
   where entry_date between a.start_date and a.end_date
   and id = a.id) as height,

  id1, start_date, end_date
from test a
where start_date is not null and end_date is not null

union all

select a.* 
from test a
left join test b 
  on a.id = b.id
where 
  (a.start_date is null and a.end_date is null)
  and (b.start_date is not null and b.end_date is not null)
  and not a.entry_date between b.start_date and b.end_date

order by id, entry_date

UNION ALL 子句上方的查询将提取对体重和高度进行数学运算的记录。 UNION ALL 子句下方的查询仅关注不在所需范围内且日期为空的记录。您可以根据自己的舒适度对其进行调整。

示例:http://sqlfiddle.com/#!9/f9b54a/6

查询相同信息的替代方法

select e.id, 
    max(entry_date) as entry_date, sum(weight) as weight, sum(height) as height,
    em.start_date, em.end_date
from test e
inner join (
  select distinct id, start_date, end_date
  from test
  where start_date is not null and end_date is not null) em
  on em.id = e.id
  and e.entry_date between em.start_date and em.end_date
group by e.id, em.start_date, em.end_date

union all

select e.id, entry_date, weight, height, em.start_date, em.end_date
from test e
inner join (
  select distinct id, start_date, end_date
  from test
  where start_date is not null and end_date is not null) em
    on em.id = e.id
    and not e.entry_date between em.start_date and em.end_date

order by id, entry_date

示例:http://sqlfiddle.com/#!9/f9b54a/8

存储数据的更好方法

create table entrymaster (
  id int,
  start_date date,
  end_date date
);

create table entries (
  id int,
  entry_date date,
  weight int,
  height int
);

select e.id, 
    max(entry_date) as entry_date, sum(weight) as weight, sum(height) as height,
    em.start_date, em.end_date
from entries e
inner join entrymaster em
  on em.id = e.id
  and e.entry_date between em.start_date and em.end_date
group by e.id, em.start_date, em.end_date

union all

select e.id, entry_date, weight, height, em.start_date, em.end_date
from entries e
inner join entrymaster em
    on em.id = e.id
    and not e.entry_date between em.start_date and em.end_date

order by id, entry_date

示例:http://sqlfiddle.com/#!9/7eaaa/3

关于mysql - 具有条件的 MySQL 中 Row 中的值总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757481/

相关文章:

sql - 使用 GROUP BY 在计算列上聚合 SUM

MySQL SUM 不适用于 GROUP BY

sql - 将GROUP BY与AVG函数一起使用-错误的语法错误

mysql - Rails change_column 错误:Mysql2::Error: COLLATION 'utf8_general_ci' 对于字符集 'binary' 无效

MySQL RegExp - 高级用法

jquery - 使用 Jquery Bootgrid 的 Asp.Net MVC 应用程序不显示任何结果

mysql - 我可以不在查询中而只在 group by() 中使用 case 语句吗?

php - 为 octobercms 构建 Eloquent 查询 ownsTo

从数据库模式设计 C++ 类

php - 将动态 PHP/MySQL 论坛永久缓存为静态页面