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

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

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

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

  1. set @curdate = '2023-06-08 ';
  2. select DATE, ACCOUNT,
  3. CASE WHEN DATE = @curdate THEN VALUE
  4. WHEN VALUE = 0 AND DATE = @curdate THEN MAX(VALUE)
  5. ELSE 0
  6. END AS MAX_VALUE
  7. 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.

  1. set @curdate = '2023-06-08 ';
  2. select DATE,ACCOUNT,
  3. CASE WHEN DATE = @curdate THEN VALUE
  4. WHEN VALUE = 0 AND DATE = @curdate THEN MAX(VALUE)
  5. ELSE 0
  6. END AS MAX_VALUE
  7. 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 我们可以尝试:

  1. WITH cte AS (
  2. SELECT *, ROW_NUMBER() OVER (PARTITION BY ACCOUNT ORDER BY DATE DESC) rn
  3. FROM yourTable
  4. WHERE VALUE <> 0
  5. )
  6. SELECT DATE, ACCOUNT, VALUE
  7. FROM cte
  8. WHERE rn = 1;

在早期版本的 MySQL 中:

  1. SELECT t1.DATE, t1.ACCOUNT, t1.VALUE
  2. FROM yourTable t1
  3. INNER JOIN
  4. (
  5. SELECT ACCOUNT, MAX(DATE) AS MAX_DATE
  6. FROM yourTable
  7. WHERE VALUE <> 0
  8. GROUP BY ACCOUNT
  9. ) t2
  10. ON t2.ACCOUNT = t1.ACCOUNT AND
  11. t2.MAX_DATE = t1.DATE
  12. WHERE
  13. t1.VALUE <> 0;
英文:

Using ROW_NUMBER we can try:

<!-- language: sql -->

  1. WITH cte AS (
  2. SELECT *, ROW_NUMBER() OVER (PARTITION BY ACCOUNT ORDER BY DATE DESC) rn
  3. FROM yourTable
  4. WHERE VALUE &lt;&gt; 0
  5. )
  6. SELECT DATE, ACCOUNT, VALUE
  7. FROM cte
  8. WHERE rn = 1;

On earlier versions of MySQL:

<!-- language: sql -->

  1. SELECT t1.DATE, t1.ACCOUNT, t1.VALUE
  2. FROM yourTable t1
  3. INNER JOIN
  4. (
  5. SELECT ACCOUNT, MAX(DATE) AS MAX_DATE
  6. FROM yourTable
  7. WHERE VALUE &lt;&gt; 0
  8. GROUP BY ACCOUNT
  9. ) t2
  10. ON t2.ACCOUNT = t1.ACCOUNT AND
  11. t2.MAX_DATE = t1.DATE
  12. WHERE
  13. t1.VALUE &lt;&gt; 0;

答案2

得分: 1

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

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

  1. set @curdate = '2023-06-08';
  2. with cte as (
  3. select ACCOUNT, DATE, max(VALUE) as max_value
  4. from mytable
  5. where DATE = @curdate
  6. group by ACCOUNT, DATE
  7. )
  8. select ACCOUNT, DATE, max_value as VALUE
  9. from cte where max_value > 0
  10. union all
  11. select t.ACCOUNT, t.DATE, t.VALUE
  12. from mytable t
  13. inner join (
  14. select t.ACCOUNT, max(t.VALUE) as VALUE
  15. from cte c
  16. inner join mytable t on t.ACCOUNT = c.ACCOUNT
  17. where max_value = 0
  18. group by t.ACCOUNT
  19. ) 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.

  1. set @curdate = &#39;2023-06-08&#39;;
  2. with cte as (
  3. select ACCOUNT, DATE, max(VALUE) as max_value
  4. from mytable
  5. where DATE = @curdate
  6. group by ACCOUNT, DATE
  7. )
  8. select ACCOUNT, DATE, max_value as VALUE
  9. from cte where max_value &gt; 0
  10. union all
  11. select t.ACCOUNT, t.DATE, t.VALUE
  12. from mytable t
  13. inner join (
  14. select t.ACCOUNT, max(t.VALUE) as VALUE
  15. from cte c
  16. inner join mytable t on t.ACCOUNT = c.ACCOUNT
  17. where max_value = 0
  18. group by t.ACCOUNT
  19. ) 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或不存在时),我们可以使用子查询和聚合,如下所示:

  1. set @curdate = '2023-06-08';
  2. select max(DATE) as '日期',
  3. ACCOUNT,
  4. coalesce(nullif(max(case when DATE = @curdate then VALUE end), 0),
  5. (select value from table_name d
  6. where d.ACCOUNT = t.ACCOUNT and
  7. d.DATE < @curdate and
  8. d.value > 0
  9. order by d.DATE DESC
  10. limit 1)
  11. ) 最大值
  12. from table_name t
  13. WHERE DATE <= @curdate and value > 0
  14. group by ACCOUNT

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

  1. set @curdate = '2023-06-08';
  2. set @rn = 0;
  3. set @acc = null;
  4. select DATE, ACCOUNT, value
  5. from
  6. (
  7. select *,
  8. if(@acc<>ACCOUNT, @rn:=1, @rn:=@rn+1) rn,
  9. @acc:=ACCOUNT
  10. from table_name
  11. where DATE <= @curdate and value > 0
  12. order by ACCOUNT, DATE desc
  13. ) t
  14. 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:

  1. set @curdate = &#39;2023-06-08&#39;;
  2. select max(DATE) as &#39;DATE&#39;,
  3. ACCOUNT,
  4. coalesce(nullif(max(case when DATE = @curdate then VALUE end), 0),
  5. (select value from table_name d
  6. where d.ACCOUNT = t.ACCOUNT and
  7. d.DATE &lt; @curdate and
  8. d. value &gt; 0
  9. order by d.DATE DESC
  10. limit 1)
  11. ) MAX_VALUE
  12. from table_name t
  13. WHERE DATE &lt;= @curdate and value &gt; 0
  14. group by ACCOUNT

Demo

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

  1. set @curdate = &#39;2023-06-08&#39;;
  2. set @rn =0;
  3. set @acc = null;
  4. select DATE, ACCOUNT, value
  5. from
  6. (
  7. select *,
  8. if(@acc&lt;&gt;ACCOUNT, @rn:=1, @rn:=@rn+1) rn,
  9. @acc:=ACCOUNT
  10. from table_name
  11. where DATE &lt;= @curdate and value &gt; 0
  12. order by ACCOUNT, DATE desc
  13. ) t
  14. 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:

确定