【问题标题】:max date for individual customer id单个客户 ID 的最大日期
【发布时间】:2014-07-15 12:45:10
【问题描述】:

我需要单个客户的最长日期。具有唯一客户 ID 的表中有 1 个客户超过 1 次,这意味着客户在表中重复。我想要他们的最新日期。

这是查询

SELECT  sum(receipt_table.installment_amount) total_installment,
        receipt_table.customer_id, 
        customer_info.name,
        customer_info.mobile,
        customer_info.plot_number,
        customer_info.amount, 
        max(receipt_table.date_time) last_date 
FROM    receipt_table,customer_info 
WHERE   customer_info.installment_type = 'monthly-12'
AND     customer_info.customer_id = receipt_table.customer_id 
GROUP BY customer_id 
HAVING  (customer_info.amount - sum(receipt_table.installment_amount) != 0) 

这样的表结构(receipt_table)

customer_id     receipt_id  installment_amount  date_time       
 41                115         1000              2014-07-27
 42                 116        1000              2014-07-27
 41                 117        1000              2014-07-30
 42                 118        1000              2014-07-30   

我得到的个人客户的最大日期是 2014-07-27

【问题讨论】:

  • 我可以看到您的查询中已经有Max(date_time)...这不是给您正确的结果吗?你目前得到什么结果,你期望得到什么结果?
  • 这是一个常见问题(也许是最常见的问题),因此它可能会在几分钟内作为副本关闭。如果没有...并且如果您愿意,请考虑遵循以下简单的两步操作: 1. 如果您还没有这样做,请提供适当的 DDL(和/或 sqlfiddle),以便我们可以更容易地复制问题。 2. 如果您尚未这样做,请提供与步骤 1 中提供的信息相对应的所需结果集。
  • 是的,我没有得到想要的结果,我想要根据每个客户的最大日期

标签: php mysql sql date


【解决方案1】:
Try this instead of group by id group by name:
SELECT  sum(receipt_table.installment_amount) total_installment,receipt_table.  
customer_info.name, max(receipt_table.date_time)last_date FROM 
receipt_table,customer_info where customer_info.installment_type='monthly-12'and      
customer_info.customer_id=receipt_table.customer_id GROUP BY customer_name 

【讨论】:

  • 名称通常没有区别(即'John Smith'),因此结果可能会很差。如果它们唯一的,则与使用 id 没有区别(好吧,除了它可能会更慢,因为 name 不是连接列)
猜你喜欢
  • 1970-01-01
  • 2023-01-21
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 2023-03-03
  • 1970-01-01
相关资源
最近更新 更多