[英]MySQL Get counts in subquery without grouping
I've been trying to find a solution to having a count appear for each entry there is a duplicate.我一直在试图找到一个解决方案,让每个条目都出现一个重复项。 But every query I've tried either groups all the entries into one row or one count is incorrect.
但是我尝试过的每个查询要么将所有条目分组为一行,要么一次计数不正确。
Here is my table without a count:这是我的表,没有计数:
------------------------------------------------------
| id | last_name | last_4_ssn | type | complete_date |
------------------------------------------------------
| 1 | TEST | 1234 | CC | NULL |
| 2 | TEST | 1234 | CC | NULL |
| 3 | TEST | 1234 | CC | 2016-09-11 |
| 4 | TEST | 1234 | CC | 2016-09-06 |
| 5 | TEST | 1234 | CC | NULL |
| 6 | TEST | 1234 | FE | 2016-08-30 |
| 7 | TEST | 1234 | FE | 2016-09-01 |
| 8 | TEST | 1234 | FE | 2016-09-02 |
------------------------------------------------------
I'm trying to get a count that checks 'last_name', 'last_4_ssn' and 'type' with a complete_date that is NOT NULL.我正在尝试使用非空的 complete_date 来检查“last_name”、“last_4_ssn”和“type”的计数。
I would like my resulting table to look like this:我希望我的结果表如下所示:
--------------------------------------------------------------
| id | last_name | last_4_ssn | type | complete_date | count |
--------------------------------------------------------------
| 1 | TEST | 1234 | CC | NULL | 0 |
| 2 | TEST | 1234 | CC | NULL | 0 |
| 3 | TEST | 1234 | CC | 2016-09-11 | 2 |
| 4 | TEST | 1234 | CC | 2016-09-06 | 2 |
| 5 | TEST | 1234 | CC | NULL | 0 |
| 6 | TEST | 1234 | FE | 2016-08-30 | 3 |
| 7 | TEST | 1234 | FE | 2016-09-01 | 3 |
| 8 | TEST | 1234 | FE | 2016-09-02 | 3 |
--------------------------------------------------------------
Notice how there are 2 CC with complete dates and 3 FEs with complete dates?请注意如何有 2 个具有完整日期的 CC 和 3 个具有完整日期的 FE? This is the count I am trying to get;
这是我想要得到的计数; where last_name, last_4_ssn and type all equal the same and their complete_dates are NOT NULL.
其中 last_name、last_4_ssn 和 type 都相同,并且它们的 complete_dates 不是 NULL。
The current SQL that I am trying is (but not quite there):我正在尝试的当前 SQL 是(但不完全存在):
SELECT
clients.id,
clients.session_type,
clients.last_name,
clients.complete_date,
clients.last_4_ssn,
dups.the_count
FROM clients
LEFT JOIN (
SELECT id,
COUNT(*) AS the_count,
session_type,
last_4_ssn,
last_name,
complete_date
FROM `clients`
GROUP BY last_name, last_4_ssn, session_type
HAVING the_count > 1 AND complete_date IS NOT NULL ) AS dups
ON (dups.last_4_ssn = clients.last_4_ssn AND dups.last_name = clients.last_name AND dups.session_type = clients.session_type)
GROUP BY clients.id
Any ideas would be greatly appreciated.任何想法将不胜感激。 Thanks for your time!
谢谢你的时间!
SELECT t1.*,
COALESECE(t2.count, 0) -- NULL dates should show a 0 count
FROM clients t1
LEFT JOIN
(
SELECT last_name,
last_4_ssn,
type,
SUM(CASE WHEN complete_date IS NOT NULL THEN 1 ELSE 0 END) AS `count`
FROM clients
GROUP BY last_name,
last_4_ssn,
type
) t2
ON t1.last_name = t2.last_name AND -- join on name, ssn, and type
t1.last_4_ssn = t2.last_4_ssn AND
t1.type = t2.type AND
t1.complete_date IS NOT NULL -- but only join the count if date not NULL
Demo here:演示在这里:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.