简体   繁体   English

获取最大日期的计数[MYSQL]

[英]Get count for the max date [MYSQL]

i have this schema 我有这个架构 EER图

I have to find the count of customers that in their latest account (capital*) balance (they might have multiple accounts), balance_value is over than 1.000.000 我必须找到在其最近的帐户(资本*)余额(他们可能有多个帐户)中,balance_value超过1.000.000的客户数

*capital: balance_type ='Capital' * capital:balance_type ='Capital'

I guess my Query for that returns wrong results and it its inefficient 我猜我的查询返回错误的结果,它效率低下

SELECT COUNT(DISTINCT customer.customer_id) AS customers
    FROM balance INNER JOIN account ON balance.account_id = account.account_id
    INNER JOIN contract ON account.contract_id = contract.contract_id
    INNER JOIN customer ON customer.customer_id = contract.customer_id
    WHERE  balance.balance_type like '%Capital%' AND balance.balance_value > 1000000
    ORDER BY balance.balance_date DESC

i guess that i need some help :S 我想我需要一些帮助:S

Download sample data (move Data folder to C:/) and script to create the schema https://drive.google.com/file/d/0BxVNamixm4W2SHVadndtWkNBajg/view?usp=sharing 下载示例数据(将数据文件夹移动到C:/)和脚本以创建架构https://drive.google.com/file/d/0BxVNamixm4W2SHVadndtWkNBajg/view?usp=sharing

what is your query result? 您的查询结果是什么?

what kind of 'correct' result did you expect? 您期望得到什么样的“正确”结果?

fyi, 仅供参考,

group by customer_id will make it faster than distinct does 按customer_id进行分组将使其速度更快

if you know the exact capital word, would better using EQUAL TO / = rather than LIKE (= is faster than LIKE) 如果您知道确切的大写字母,最好使用EQUAL TO / =而不是LIKE(=比LIKE更快)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM