sql查询(max-min)显示错误的结果

luaexgnf  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(280)

我试图从我的数据库计算数据,但首先我注意到从我得到的结果奇怪的行为,其次,我有困难提出一个要求,考虑到重新填充。
我有一个表:name-datetime-content
我想按天分组的行,并选择不同的数字有消费。例如:

Name - DateTime - Content
Foo - 22-04-2018 6:00 - 120
Foo - 22-04-2018 10:00 - 119
Foo - 22-04-2018 16:00 - 118

含量有所下降,结果应为-2。我请求的输出=-2
另一个例子:

Name - DateTime - Content
Foo - 23-04-2018 6:00 - 50
Foo - 23-04-2018 10:00 - 90
Foo - 23-04-2018 16:00 - 120

在这里我们可以注意到这个数字增加了。这意味着我们没有消费,而是补充了储备,而且含量增加了。结果应该是-70。我的请求输出:30
我的请求:

SELECT day, 
       Abs(Sum(diffn)) AS totN 
FROM   (SELECT Date(datetime)              AS day, 
               Max(content) - Min(content) AS diffN 
        FROM   logs 
        WHERE  NAME = 'Foo' 
               AND datetime >= '2018-04-22 00:00:00' 
               AND datetime <= '2018-04-23 00:00:00' 
        GROUP  BY Date(datetime)) a 
GROUP  BY day;

但是对于第二个例子,我有30而不是70,我不知道为什么。。。我希望你能帮助我改变我的要求,并考虑到重新填充,以便我得到我想要的结果。
谢谢!

u4vypkhs

u4vypkhs1#

为什么要第二次分组:
理想情况下,这应该起作用:

SELECT Date(datetime)              AS day, 
       Max(content) - Min(content) AS diffN 
FROM   logs 
WHERE  NAME = 'Foo' 
       AND datetime >= '2018-04-22 00:00:00' 
       AND datetime <= '2018-04-23 00:00:00' 
GROUP  BY Date(datetime)

此查询的结果将只包含2行-1行用于第22天,1行用于第23天。没有必要再按天分组

wribegjk

wribegjk2#

您需要通过比较最高值和最低值(包括时间(小时))来确定前缀。我在这里使用带有两个子查询的'case'函数。
也许你需要把年-月-日改过来,因为我用的是德语的datetime格式。

SET @datetime = '2018-04-22';

SELECT date(datetime) as day
    ,(CASE WHEN
     (SELECT content FROM logs WHERE date(datetime) = @datetime ORDER BY datetime LIMIT 1)
        >
     (SELECT content FROM logs WHERE date(datetime) = @datetime ORDER BY datetime desc LIMIT 1)
       THEN min(content) - max(content)
       ELSE max(content) - min(content) END) as diffN
    FROM logs 

    WHERE Name = 'Foo' AND date(datetime) = @datetime
    GROUP BY day(datetime)
    ORDER BY datetime
    ;
ffvjumwh

ffvjumwh3#

这应该可以做到:

SELECT day(datetime) as day, max(content) - min(content) as diffN
FROM logs 
WHERE Name = 'Foo' 
AND datetime >= '2018-04-23 00:00:00' 
AND datetime <= '2018-04-24 00:00:00' 
GROUP BY day(datetime)

另外,更改日期过滤器它应该在23和24之间。

bkkx9g8r

bkkx9g8r4#

您可能需要建立第一个和最后一个datetime及其相关内容。例如

drop table if exists t;
create table t (name varchar(3), dt datetime, content int);
insert into t values
('Foo' , '2018-04-22 06:00:00', 120),
('Foo' , '2018-04-22 10:00:00', 119),
('Foo' , '2018-04-22 16:00:00', 118),
('Foo' , '2018-04-23 06:00:00', 50),
('Foo' , '2018-04-23 10:00:00', 90),
('Foo' , '2018-04-23 16:00:00', 120);

select s.name,lastinday,firstinday,lastinday - firstinday
from 
(
select name,dt, content lastinday
from t
where dt = (Select max(dt) from t t1 where t1.name = t.name and date(t1.dt) = date(t.dt))
) s
join
(
select name,dt, content firstinday
from t
where dt = (Select min(dt) from t t1 where t1.name = t.name and date(t1.dt) = date(t.dt))
) t
on t.name = s.name and date(t.dt) = date(s.dt);

+------+-----------+------------+------------------------+
| name | lastinday | firstinday | lastinday - firstinday |
+------+-----------+------------+------------------------+
| Foo  |       118 |        120 |                     -2 |
| Foo  |       120 |         50 |                     70 |
+------+-----------+------------+------------------------+
2 rows in set (0.00 sec)

相关问题