SQL:选择基于其他重复值的重复值的精确最大值

时间:2017-09-29 01:40:44

标签: mysql sql select

Table with repeating data in each column
|  userid   |   code   |    date    |
|3          |  YYYYYY  | 2017-09-20 |
|3          |  YYYYYY  | 2017-09-21 |
|3          |  YYYYYY  | 2017-09-22 |
|1          |  XXXXXX  | 2017-09-22 |
|1          |  XXXXXX  | 2017-09-23 |
|3          |  XXXXXX  | 2017-09-23 |
|3          |  XXXXXX  | 2017-09-23 |
|2          |  ZZZZZZ  | 2017-09-23 |
|3          |  ZZZZZZ  | 2017-09-23 |
|1          |  ZZZZZZ  | 2017-09-24 |

我需要为每个'代码'获取MAX(日期),并为此MAX(日期)和'代码'获取'userid',并按'代码'分组。

SELECT
t1.userid,
t1.code,
t1.date
FROM codes AS t1

INNER JOIN (
            SELECT 
              userid,
              code,
              MAX(date) as maxdate
            FROM codes
            GROUP BY code
            ) AS t2
ON (t1.code = t2.code AND t1.date = t2.maxdate)
ORDER BY date

但是,MAX(日期)值应该基于用户ID。

如果代码有用户1和2和3,则最大日期应基于用户3代码。

如果代码有用户2和3,则最大日期应基于用户3代码。

如果代码有用户1和2,则最大日期应基于用户2代码。

如果代码有用户3,则最大日期应基于用户3代码。

如果代码有用户2,则最大日期应基于用户2代码。

如果代码有用户1,则最大日期应基于用户1代码。

响应shuld包含:代码,用户ID和MAX(日期)基于'userid'值和gpoup by'code'(每个代码一行)

如何添加此选项? 谢谢你的帮助) SQL Fiddle

2 个答案:

答案 0 :(得分:1)

使用ANSI标准窗口函数:

Ttkinter

您的SQL Fiddle使用不支持窗口功能的MySQL。在该数据库中,您可以执行以下操作:

F=figure()
F.line('x', 'y', source=source, name='line1')
F.line('x', 'z', source=source, name='line2')

%%in callback
selected_line_name = 'line1' # this would be determined by checkbox
selected_line = F.children[selected_line_name]
delete(selected_line)

这是SQL Fiddle

请注意,您仍会获得select c.* from (select c.*, row_number() over (partition by code order by date desc, userid desc ) as seqnum from c ) c where seqnum = 1; 的重复项,因为其最大值会在数据中多次出现。

答案 1 :(得分:0)

SELECT 
    t1.userid,
    t1.code,
    t1.date
    -- SOME OTHER ROWS FROM TABLE
    FROM 
      codes AS t1 
      -- JOIN TABLE WITH SEARСH ROWS
      INNER JOIN (
        -- BEGIN SELECT MAX LOGIC
        SELECT 
          code, 
          CASE WHEN max3date IS NOT NULL THEN max3date WHEN max2date IS NOT NULL THEN max2date ELSE max1date END AS maxdate
        FROM 
          (
            -- BEGIN SELECT MAX FOR EACH GROUP
            SELECT 
              all1.code, 
              u3.max3date, 
              u2.max2date, 
              u1.max1date 
            FROM 
              codes AS all1 
              LEFT JOIN (
                SELECT 
                  code,  
                  MAX(date) AS max3date 
                FROM 
                  codes 
                WHERE userid LIKE '3%' 
                GROUP BY 
                  code
              ) AS u3 ON (all1.code = u3.code)
              LEFT JOIN (
                SELECT 
                  code, 
                  MAX(date) AS max2date 
                FROM 
                  codes 
                WHERE userid LIKE '2%'
                GROUP BY 
                  code
              ) AS u2 ON (all1.code = u2.code) 
              LEFT JOIN (
                SELECT 
                  code,  
                  MAX(date) AS max1date  
                FROM 
                  codes 
                WHERE userid LIKE '1%' 
                GROUP BY 
                  code
              ) AS u1 ON (all1.code = u1.code) 
            GROUP BY 
              code
          ) t3
      ) AS t2 ON (
        t1.code = t2.code 
        AND t1.date = t2.maxdate
      )  
    ORDER BY 
      t1.date

24M行对我来说很好用。日期(时间)应该是唯一的。 SQL Fiddle