获取最大值或最新的最大值

huangapple go评论69阅读模式
英文:

How to get the Max value else latest max value

问题

以下是翻译好的部分:

日期 账户
2023-06-06 1234 100
2023-06-07 1234 120
2023-06-08 1234 80
2023-06-06 3456 40
2023-06-07 3456 60
2023-06-08 3456 80
2023-06-05 5648 600
2023-06-06 5648 800
2023-06-06 5648 650
2023-06-07 5648 0
2023-06-08 5648 0

我传递了当前日期并从表格中获取了最新的值。

set @curdate = '2023-06-08 ';
select DATE, ACCOUNT,
       CASE WHEN DATE = @curdate THEN VALUE 
            WHEN VALUE = 0 AND DATE = @curdate THEN MAX(VALUE)
            ELSE 0
       END AS MAX_VALUE
from table ;

实际上,我试图获取最新的值,例如,如果当前日期没有该值,那么我必须获取该账户的最大上一个值。

输出:

日期 账户
2023-06-08 1234 80
2023-06-08 3456 80
2023-06-06 5648 650

有人能对这个提出建议吗?

英文:
DATE ACCOUNT VALUE
2023-06-06 1234 100
2023-06-07 1234 120
2023-06-08 1234 80
2023-06-06 3456 40
2023-06-07 3456 60
2023-06-08 3456 80
2023-06-05 5648 600
2023-06-06 5648 800
2023-06-06 5648 650
2023-06-07 5648 0
2023-06-08 5648 0

I'm passing current date and getting the latest value from the table.

 set @curdate = '2023-06-08 ';
 select DATE,ACCOUNT,
        CASE WHEN DATE = @curdate THEN VALUE 
             WHEN VALUE = 0 AND DATE = @curdate THEN MAX(VALUE)
             ELSE 0
        END AS MAX_VALUE
from table ;

Actually I'm trying to get the latest value and for example if value is not present for current date then I have to get last maximum value for that account.

output :

DATE ACCOUNT VALUE
2023-06-08 1234 80
2023-06-08 3456 80
2023-06-06 5648 650

Can anyone suggest me on this one?

答案1

得分: 3

使用 ROW_NUMBER 我们可以尝试:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY ACCOUNT ORDER BY DATE DESC) rn
    FROM yourTable
    WHERE VALUE <> 0
)

SELECT DATE, ACCOUNT, VALUE
FROM cte
WHERE rn = 1;

在早期版本的 MySQL 中:

SELECT t1.DATE, t1.ACCOUNT, t1.VALUE
FROM yourTable t1
INNER JOIN
(
    SELECT ACCOUNT, MAX(DATE) AS MAX_DATE
    FROM yourTable
    WHERE VALUE <> 0
    GROUP BY ACCOUNT
) t2
    ON t2.ACCOUNT = t1.ACCOUNT AND
       t2.MAX_DATE = t1.DATE
WHERE
    t1.VALUE <> 0;
英文:

Using ROW_NUMBER we can try:

<!-- language: sql -->

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY ACCOUNT ORDER BY DATE DESC) rn
    FROM yourTable
    WHERE VALUE &lt;&gt; 0
)

SELECT DATE, ACCOUNT, VALUE
FROM cte
WHERE rn = 1;

On earlier versions of MySQL:

<!-- language: sql -->

SELECT t1.DATE, t1.ACCOUNT, t1.VALUE
FROM yourTable t1
INNER JOIN
(
    SELECT ACCOUNT, MAX(DATE) AS MAX_DATE
    FROM yourTable
    WHERE VALUE &lt;&gt; 0
    GROUP BY ACCOUNT
) t2
    ON t2.ACCOUNT = t1.ACCOUNT AND
       t2.MAX_DATE = t1.DATE
WHERE
    t1.VALUE &lt;&gt; 0;

答案2

得分: 1

可以使用以下查询来完成此操作:

使用cte来获取具有值的帐户,然后我们需要获取值为0的帐户的最大值和相关日期,最后使用union all来组合这些数据集。

set @curdate = '2023-06-08';

with cte as (
  select ACCOUNT, DATE, max(VALUE) as max_value
  from mytable
  where DATE = @curdate
  group by ACCOUNT, DATE
)
select ACCOUNT, DATE, max_value as VALUE
from cte where max_value > 0
union all
select t.ACCOUNT, t.DATE, t.VALUE
from mytable t
inner join (
  select t.ACCOUNT, max(t.VALUE) as VALUE
  from cte c
  inner join mytable t on t.ACCOUNT = c.ACCOUNT
  where max_value = 0
  group by t.ACCOUNT
) as s on s.ACCOUNT = t.ACCOUNT and s.VALUE = t.VALUE

结果:

帐户 日期
1234 2023-06-08 80
3456 2023-06-08 80
5648 2023-06-06 600

示例链接

英文:

This can be done using this query :

The cte was used to get accounts with values, then we need to get the maximum value and the related date for accounts with value = 0, and finally we use union all to combine those datasets.

set @curdate = &#39;2023-06-08&#39;;

with cte as (
  select ACCOUNT, DATE, max(VALUE) as max_value
  from mytable
  where DATE = @curdate
  group by ACCOUNT, DATE
)
select ACCOUNT, DATE, max_value as VALUE
from cte where max_value &gt; 0
union all
select t.ACCOUNT, t.DATE, t.VALUE
from mytable t
inner join (
  select t.ACCOUNT, max(t.VALUE) as VALUE
  from cte c
  inner join mytable t on t.ACCOUNT = c.ACCOUNT
  where max_value = 0
  group by t.ACCOUNT
) as s on s.ACCOUNT = t.ACCOUNT and s.VALUE = t.VALUE

Result :

ACCOUNT DATE VALUE
1234 2023-06-08 80
3456 2023-06-08 80
5648 2023-06-06 600

Demo here

答案3

得分: 1

以下是翻译好的部分:

要获取第一个不等于0的前一个值(当@curdate上的值等于0或不存在时),我们可以使用子查询和聚合,如下所示:

set @curdate = '2023-06-08';

select max(DATE) as '日期',
       ACCOUNT,
       coalesce(nullif(max(case when DATE = @curdate then VALUE end), 0), 
                (select value from table_name d 
                 where d.ACCOUNT = t.ACCOUNT and
                       d.DATE < @curdate and
                       d.value > 0
                 order by d.DATE DESC
                 limit 1)
               ) 最大值
from table_name t
WHERE DATE <= @curdate and value > 0
group by ACCOUNT

另一种方法,在MySQL 5.7中,您可以模拟row_number功能,如下所示:

set @curdate = '2023-06-08';
set @rn = 0;
set @acc = null;

select DATE, ACCOUNT, value 
  from
(
  select *,
     if(@acc<>ACCOUNT, @rn:=1, @rn:=@rn+1) rn,
     @acc:=ACCOUNT 
  from table_name 
  where DATE <= @curdate and value > 0
  order by ACCOUNT, DATE desc
) t
where rn = 1
英文:

To get the first previous value that is not equal to 0,(when the value on @curdate is equal to 0 or not exists) we could use a subquery and aggregate as the following:

set @curdate = &#39;2023-06-08&#39;;

select max(DATE) as &#39;DATE&#39;,
       ACCOUNT,
       coalesce(nullif(max(case when DATE = @curdate then VALUE end), 0), 
                (select value from table_name d 
                 where d.ACCOUNT = t.ACCOUNT and
                       d.DATE &lt; @curdate and
                       d. value &gt; 0
                 order by d.DATE DESC
                 limit 1)
               ) MAX_VALUE
from table_name t
WHERE DATE &lt;= @curdate and value &gt; 0
group by ACCOUNT

Demo

Another approach, for mysql 5.7 you could simulate the row_number functionality as the following:

set @curdate = &#39;2023-06-08&#39;;
set @rn =0;
set @acc = null;

select DATE, ACCOUNT, value 
  from
(
  select *,
     if(@acc&lt;&gt;ACCOUNT, @rn:=1, @rn:=@rn+1) rn,
     @acc:=ACCOUNT 
  from table_name 
  where DATE &lt;= @curdate and value &gt; 0
  order by ACCOUNT, DATE desc
) t
where rn = 1

Demo

huangapple
  • 本文由 发表于 2023年6月8日 17:22:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定