SQL查询以选择最新的重复项和最大值

时间:2018-03-07 09:37:20

标签: mysql sql

我有下面的客户表

ID|Cust_ID|Name|Amount|  PaidDate
 1|     01|Tina|   100|2018-03-07
 2|     02|Lisa|    50|2018-03-05
 3|     01|Tina|   180|2018-03-07
 4|     01|Tina|    20|2018-03-01

所以我当前的查询是选择最近日期的记录:

select ID, Name, Amount, PaidDate from Customer C1
where PaidDate in ( select max(PaidDate) from Customer C2
where C1.ID=C2.ID)

结果如下所示

ID|Cust_ID|Name|Amount|  PaidDate
 1|     01|Tina|   100|2018-03-07
 2|     02|Lisa|    50|2018-03-05
 3|     01|Tina|   180|2018-03-07

现在,如果有2个相同的PaidDate

,我想选择最高金额

请协助,以便我得到如下结果 结果(A)

ID|Cust_ID|Name|Amount|  PaidDate
 2|     02|Lisa|    50|2018-03-05
 3|     01|Tina|   180|2018-03-07

更新: 我已经尝试了@Strawberry和@scaisEdge建议的查询,但发现了新案例

ID|Cust_ID|Name|Amount|  PaidDate
 1|     01|Tina|   100|2018-03-07
 2|     02|Lisa|    50|2018-03-05
 3|     01|Tina|   180|2018-03-07
 4|     01|Tina|    20|2018-03-01
 5|     02|Lisa|   100|2018-03-01

例如,这里有2条Lisa记录,假设查询结果为 结果(A)
然而,结果如下所示,因为Lisa的数量最高而没有先看PaidDate

ID|Cust_ID|Name|Amount|  PaidDate
 3|     01|Tina|   180|2018-03-07          
 5|     02|Lisa|   100|2018-03-01

要求是,
如果PaidDate<> NULL然后
    获取PaidDate最新价值和
          如果PaidDate最新值> 1
             以最大金额获得PaidDate 结束如果

谢谢&此致

3 个答案:

答案 0 :(得分:0)

您应该使用几个nestes子查询进行分组

select * from 
customer c2
inner join 
( 

    select c.name, max(c.Amount) max_amount , c.paidDate
    from Customer c
    inner join (
      select name, max(paidDate) max_paiddate
      from Customer
      group by name
    ) t on t.name  = c.name and t.max_paiddate = c.paidDate
    group by c.name, c.paidDate
) t2 on t2.name = c2.name 
        and t2.max_amount = c2.amount 
              and t2.paidDate = c2.paidDate

答案 1 :(得分:0)

考虑以下内容......

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table 
(payment_id SERIAL PRIMARY KEY
,user_id INT NOT NULL
,payment_date DATE NOT NULL
,amount DECIMAL(6,2) NOT NULL
);

INSERT INTO my_table VALUES
(1,1,'2018-03-07',100),
(2,2,'2018-03-05', 50),
(3,1,'2018-03-07',180),
(4,1,'2018-03-01', 20);


SELECT x.*
  FROM my_table x 
  JOIN 
     ( SELECT a.user_id
            , a.payment_date
            , MAX(a.amount) amount 
         FROM my_table a
         JOIN 
            ( SELECT user_id
                   , MAX(payment_date) payment_date
                FROM my_table 
               GROUP 
                  BY user_id 
            ) b
           ON b.user_id = a.user_id
          AND b.payment_date = a.payment_date
        GROUP  
           BY user_id
            , payment_date
     ) y 
    ON y.user_id = x.user_id 
   AND y.payment_date = x.payment_date
   AND y.amount = x.amount;
+------------+---------+--------------+--------+
| payment_id | user_id | payment_date | amount |
+------------+---------+--------------+--------+
|          2 |       2 | 2018-03-05   |  50.00 |
|          3 |       1 | 2018-03-07   | 180.00 |
+------------+---------+--------------+--------+

虽然快速执行,因为这种解决方案可能变得啰嗦,你经常会看到人们使用未记录的黑客,而不是像这样......

SELECT a.* 
  FROM 
     ( SELECT * FROM my_table ORDER BY user_id, payment_date DESC, amount DESC ) a 
 GROUP 
    BY user_id;
+------------+---------+--------------+--------+
| payment_id | user_id | payment_date | amount |
+------------+---------+--------------+--------+
|          3 |       1 | 2018-03-07   | 180.00 |
|          2 |       2 | 2018-03-05   |  50.00 |
+------------+---------+--------------+--------+
2 rows in set (0.00 sec)

无法保证此类解决方案能够正常运行。实际上,文档暗示它不应该。但实际上,它一直都是这样做的(到目前为止)。

答案 2 :(得分:-1)

根据上述问题的描述,请尝试执行以下SQL查询

SELECT ID,Name,max(Amount),PaidDate
    FROM `Customer` group by  ID 
    order by ID desc