需要逻辑来查找MySQL同一表中另一列的最小值的一列的值

时间:2019-10-25 13:34:36

标签: mysql

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

arr = np.array([7441.5, 7445.12, 7446.17, 7441.81, 7441.56, 7441.2, 7440.33, 7446.93, 7448.07, 7442.92, 7448.42, 7450.12, 7460.35, 7456.22, 7450.71, 7449.74, 7445.41, 7447.37, 7444.9, 7442.76, 7440.11, 7445.36, 7440.48, 7443.32, 7443.03, 7443.97, 7445.42, 7444.48, 7442.95, 7439.68, 7441.68, 7443.33, 7444.03, 7447.52, 7443.99, 7443.47, 7444.29, 7448.01, 7447.5, 7451.22, 7450.66, 7451.64, 7450.49, 7449.06, 7447.65, 7451.92, 7450.0, 7451.57, 7450.64, 7450.0, 7448.27, 7448.64, 7448.19, 7445.09, 7442.06, 7443.49, 7440.62, 7438.81, 7438.92, 7443.25, 7443.49, 7445.81, 7440.87, 7442.09, 7441.48, 7443.05, 7445.15, 7443.78, 7442.85, 7444.33, 7443.58, 7446.96, 7454.49, 7453.25, 7453.93, 7452.53, 7448.32, 7448.84, 7448.66, 7450.12, 7448.78, 7447.28, 7447.81, 7447.54, 7449.03, 7453.83, 7457.14, 7457.3, 7456.27, 7456.87, 7459.0, 7462.51, 7463.2, 7464.8, 7462.19, 7458.84, 7461.06, 7462.22, 7458.29, 7456.01, 7456.39, 7452.45, 7451.59, 7450.89, 7448.07, 7447.12, 7448.3, 7448.51, 7451.03, 7449.42, 7451.51, 7451.01, 7450.64, 7451.77, 7451.62, 7456.78, 7455.0, 7456.56, 7456.02, 7456.87, 7457.06, 7456.12, 7454.63, 7454.1, 7456.45, 7457.54, 7458.9, 7462.79, 7464.2, 7464.9, 7465.66, 7466.22, 7463.78, 7467.25, 7468.45, 7470.34, 7472.76, 7469.48, 7468.66, 7461.69, 7459.6, 7455.01, 7453.74, 7457.08, 7454.99, 7455.0, 7454.07, 7451.0, 7447.45, 7449.14, 7447.6, 7448.79, 7456.93, 7452.14, 7451.43, 7451.71, 7450.16, 7456.18, 7453.77, 7455.0, 7455.73, 7461.42, 7464.22, 7464.39, 7468.93, 7464.26, 7460.81, 7459.01, 7455.22, 7455.86, 7454.82, 7452.06, 7448.3, 7448.0, 7447.06, 7447.66, 7449.25, 7449.99, 7450.78, 7451.0, 7449.13, 7447.86, 7446.52, 7445.18, 7447.52, 7445.63, 7445.53, 7444.61, 7444.1, 7443.06, 7442.24, 7449.86, 7451.68, 7449.78, 7456.82, 7452.73, 7450.21, 7451.17, 7447.99, 7447.58, 7456.91, 7454.96, 7455.0, 7452.01, 7446.36, 7450.3, 7450.66, 7457.03, 7452.5, 7456.81, 7454.02, 7455.25, 7450.81, 7447.67, 7450.42, 7453.24, 7451.37, 7447.56, 7443.17, 7444.71, 7449.08, 7447.15, 7446.78, 7444.62, 7449.17, 7447.11, 7447.43, 7446.39, 7445.45, 7446.13, 7448.75, 7446.45, 7447.36, 7446.71, 7450.9, 7448.62, 7453.63, 7451.32, 7452.14, 7451.62, 7451.27, 7448.85, 7447.09, 7447.95, 7447.59, 7446.91, 7446.23, 7450.8, 7459.14, 7455.11, 7450.01, 7450.68, 7453.6, 7456.19, 7456.5, 7456.12, 7449.64, 7454.07, 7453.06, 7451.14, 7450.35, 7452.72, 7454.74, 7456.4, 7455.68, 7461.65, 7465.55, 7464.14, 7467.1, 7469.03, 7468.09, 7462.62, 7461.85, 7465.76, 7467.41, 7471.29, 7494.03, 7482.29, 7507.32, 7512.99, 7518.08, 7514.59, 7499.95, 7502.13, 7502.36, 7492.58, 7484.07, 7490.81, 7485.87, 7492.0, 7503.42, 7508.07, 7518.99, 7537.78, 7526.03, 7540.8, 7565.91, 7575.0, 7548.14, 7566.46, 7587.75, 7569.83, 7590.92, 7586.58, 7594.67, 7573.29, 7577.17, 7583.92, 7588.55, 7586.7, 7579.3, 7575.32, 7568.09, 7579.52, 7588.03, 7583.27, 7584.99, 7590.74, 7600.96, 7597.08, 7593.53, 7604.15, 7611.99, 7613.49, 7633.72, 7620.0, 7624.71, 7618.0, 7611.25, 7618.86, 7613.47, 7605.01, 7600.88, 7607.51, 7604.82, 7610.15, 7611.83, 7608.05, 7597.32, 7600.03, 7602.1, 7598.03, 7592.9, 7601.34, 7598.72, 7589.16, 7592.57, 7586.38, 7587.19, 7594.99, 7595.58, 7594.28, 7592.7, 7594.52, 7588.63, 7592.0, 7590.53, 7593.77, 7593.68, 7587.04, 7586.54, 7584.22, 7585.25, 7583.78, 7577.43, 7574.7, 7568.03, 7581.1, 7588.0, 7591.91, 7596.84, 7601.0, 7604.74, 7605.64, 7599.75, 7595.06, 7606.26, 7615.0, 7610.27, 7613.05, 7610.5, 7608.57, 7614.09, 7602.72, 7600.02, 7605.85, 7613.8, 7605.11, 7607.28, 7609.91, 7608.84, 7610.44, 7611.0, 7627.0, 7625.52, 7630.95, 7631.58, 7622.78, 7631.74, 7630.12, 7636.2, 7641.72, 7641.22, 7631.24, 7634.83, 7629.6, 7628.88, 7622.34, 7623.94, 7621.13, 7621.12, 7622.66, 7624.69, 7620.02, 7621.7, 7616.28, 7619.33, 7617.79, 7618.99, 7626.87, 7631.58, 7634.12, 7636.67, 7632.5, 7629.6, 7625.02, 7623.06, 7619.04, 7633.73, 7631.13, 7629.81, 7624.33, 7629.22, 7635.73, 7632.38, 7627.79, 7612.69, 7618.51, 7617.99, 7622.68, 7617.61, 7616.49, 7614.0, 7618.02, 7624.99, 7624.11, 7623.68, 7618.75, 7620.05, 7619.89, 7618.89, 7625.29, 7624.36, 7625.39, 7623.69, 7623.75, 7629.52, 7630.17, 7629.06, 7628.95, 7631.44, 7633.58, 7635.21, 7633.25, 7631.77, 7630.02, 7625.27, 7625.07, 7627.5, 7628.34, 7629.85, 7632.71, 7633.0, 7633.06, 7638.81, 7644.12, 7660.0, 7655.18, 7664.29, 7670.01, 7667.35, 7668.54, 7682.48, 7679.2, 7669.93, 7679.79, 7676.21, 7668.34, 7671.45, 7678.32, 7686.23, 7687.47, 7681.56, 7670.71, 7681.12, 7675.63, 7665.06, 7653.43, 7653.23, 7658.87, 7664.59, 7668.87, 7664.52, 7663.67, 7669.31, 7666.76, 7663.56, 7659.05, 7661.13, 7660.02, 7663.35, 7657.97, 7654.0, 7646.29, 7646.19, 7646.0, 7650.0, 7643.65, 7652.91, 7658.65, 7655.71, 7662.92, 7667.63, 7670.65, 7666.03, 7659.09, 7660.0, 7664.31, 7662.66, 7663.63, 7662.33, 7665.96, 7670.49, 7676.31, 7665.48, 7663.99, 7658.61, 7664.22, 7658.9])
peaks = np.array([1, 2, 7, 8, 10, 11, 12, 13, 14, 15, 17, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 72, 73, 74, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 90, 91, 92, 93, 94, 95, 96, 97, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 143, 152, 157, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 194, 200, 201, 202, 207, 208, 209, 210, 211, 215, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 194, 278, 279, 280, 281, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 280, 293, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 343, 344, 318, 319, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 377, 379, 382, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 411, 412, 419, 420, 421, 422, 423, 424, 428, 429, 430, 432, 433, 434, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 420, 421, 422, 423, 428, 433, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 500, 501, 502, 503, 504, 505, 506, 523, 533])
valleys = np.array([5, 6, 19, 20, 22, 23, 24, 28, 29, 30, 31, 32, 34, 35, 36, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 76, 77, 78, 80, 81, 82, 83, 84, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 123, 141, 142, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 77, 78, 79, 80, 81, 82, 83, 84, 85, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 147, 148, 149, 150, 151, 154, 155, 156, 172, 173, 174, 175, 176, 177, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 198, 199, 204, 213, 217, 218, 219, 221, 222, 223, 225, 226, 227, 228, 229, 213, 214, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 277, 286, 287, 288, 289, 242, 243, 244, 245, 246, 247, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 277, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 309, 310, 311, 312, 313, 314, 315, 316, 342, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 374, 375, 383, 384, 385, 387, 388, 436, 437, 438, 440, 441, 442, 443, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 385, 387, 388, 389, 390, 391, 392, 415, 416, 417, 418, 426, 427, 431, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 512, 513, 514, 515, 516, 517, 518, 519, 520, 525, 526, 528, 530, 535, 536, 537, 538])

我执行上述查询时得到了此输出

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

4 个答案:

答案 0 :(得分: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     |

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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