【问题标题】:Need the logic to find the value of one column for the min of another column in same table in mysql需要逻辑来为mysql中同一表中另一列的最小值找到一列的值
【发布时间】:2020-02-21 20:51:36
【问题描述】:

我写了下面的代码来得到这样的

SELECT `temp2`.`UserId` AS `UserId`,
              `temp2`.`ScheduleDate` AS `ScheduleDate`,
             `temp2`.`daystodue`
  FROM (
SELECT
      `temp1`.`UserId` AS `UserId`,
      `temp1`.`ScheduleDate` AS `ScheduleDate`,
      `temp1`.`DueDate` AS `DueDate`,
      `temp1`.`CompletedState` AS `CompletedState`,
      IF(((`temp1`.`CompletedState` = 0) AND ((`temp1`.`ScheduleDate` - CURDATE()) <= 0)), (TO_DAYS(`temp1`.`DueDate`) - TO_DAYS(CURDATE())), 0) AS `daystodue`
    FROM (SELECT
        `fd_dw`.`ComplianceFactTable`.`UserId` AS `UserId`,
        CAST(`fd_dw`.`ComplianceFactTable`.`CourseModule_ScheduleDateID` AS date) AS `ScheduleDate`,
        CAST(`fd_dw`.`ComplianceFactTable`.`CourseModule_dueDateID` AS date) AS `DueDate`,
        `fd_dw`.`ComplianceFactTable`.`CourseModuleComplete_completionstate` AS `CompletedState`
      FROM `fd_dw`.`ComplianceFactTable`
      WHERE ((`fd_dw`.`ComplianceFactTable`.`CourseModule_dueDateID` > 0)
      AND ((CAST(`fd_dw`.`ComplianceFactTable`.`CourseModule_dueDateID` AS date) - CURDATE()) > 0))) `temp1`) `temp2` 
  WHERE `temp2`.`UserId` IN (223699,223741,223780,223678,243988,380316,388737,121896,491562)

我在执行上述查询时得到了这个输出

ID       Date      value
121896  2019-12-06  0
121896  2019-11-06  0
121896  2020-01-06  0
223678  2019-12-23  0
223678  2019-11-23  0
223678  2020-01-23  0
223678  2019-10-23  43
223699  2019-12-23  0
223699  2019-11-23  0
223699  2020-01-23  0
223699  2019-10-23  43
223741  2019-12-23  0
223741  2019-11-23  0
223741  2020-01-23  0
223741  2019-10-23  43
223780  2019-12-23  0
223780  2019-11-23  0
223780  2020-01-23  0
223780  2019-10-23  43
243988  2019-10-15  21
243988  2020-01-15  0
243988  2019-12-15  0
380316  2019-10-05  0
380316  2019-11-05  0
380316  2019-12-05  0
380316  2020-01-05  0
388737  2019-10-23  29
388737  2019-11-23  0
388737  2020-01-23  0
388737  2019-12-23  0
491562  2019-10-17  7
491562  2019-10-17  7
491562  2019-10-17  23
491562  2019-11-17  0
491562  2019-12-17  0
491562  2020-01-17  0
491562  2019-10-17  7
491562  2019-10-17  7

但我想要这样的东西

121896  2019-11-06  0
223678  2019-10-23  43
223699  2019-10-23  43
223741  2019-10-23  43
223780  2019-10-23  43
243988  2019-10-15  21
380316  2019-10-05  0
388737  2019-10-23  29
491562  2019-10-17  7

【问题讨论】:

  • 解释该输出的逻辑。向我们展示数据库架构、示例数据、当前和预期输出。请阅读How-to-Ask
  • 你的mysql版本是多少?
  • 你指的代码在哪里?
  • SELECT Col1, min(Col2), min(Col3) from table group by Col1
  • 逻辑是针对每个用户的,对于我想要对应值的最小日期字段

标签: mysql database date greatest-n-per-group window-functions


【解决方案1】:

我了解,对于每个 id,您都希望以最短日期记录。当有多个具有最短日期的记录时,您需要具有最短日期的记录。

在 MySQL 8.0 中,您可以使用窗口函数:

select id, date, value
from (
    select 
        t.*,
        row_number() over(partition by id order by date, value) rn
    from mytable t
) t
where rn = 1

在早期版本中,您可以使用相关的 suquery 进行过滤:

select id, date, min(value) value
from mytable t
where t.date = (
    select t1.date from mytable t1 where t1.id = t.id order by t1.date, t1.value limit 1
)
group by id, date

或者:

select id, date, min(value) value
from mytable t
where t.date = (select min(t1.date) from mytable t1 where t1.id = t.id)
group by id, date

在这个 demo on DB Fiddle 中,所有 3 个查询都返回:

| id     | date       | value |
| ------ | ---------- | ----- |
| 121896 | 2019-11-06 | 0     |
| 223678 | 2019-10-23 | 43    |
| 223699 | 2019-10-23 | 43    |
| 223741 | 2019-10-23 | 43    |
| 223780 | 2019-10-23 | 43    |
| 243988 | 2019-10-15 | 21    |
| 380316 | 2019-10-05 | 0     |
| 388737 | 2019-10-23 | 29    |
| 491562 | 2019-10-17 | 7     |

【讨论】:

  • 您应该有一行作为 id,并且不确定是否需要每个 col 的 min 或仅与 min date 相关的数据
  • 我想要与最小日期相关的值(第三列)
  • 我认为 min(date) 更干净。
  • 谢谢胡安!!你能帮我为每个用户只记录一条记录吗
  • @Sai:哪里有多个最短日期的记录,你要显示哪一个?
【解决方案2】:

您的预期输出与问题的标题不同,因为对于第一列的每个值,它都包含第二列(日期)中具有最小值的行。
使用NOT EXISTS:

select t.* from tablename t
where not exists (
  select 1 from tablename
  where id = t.id and date < t.date
)

【讨论】:

    【解决方案3】:

    如果您没有 mysql 8,请使用相关查询

      SELECT *
      FROM yourTable t1
      WHERE date = (SELECT MIN(date)
                    FROM yourTable t2
                    WHERE t1.user_id = t2.user_id)
    

    【讨论】:

      【解决方案4】:

      从你的表名中选择 min(column1), min(column2)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2021-07-25
        • 1970-01-01
        • 2021-02-22
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多