Похоже (после всех исправлений) вам нужна
SELECT t1.ID,
t1.`Date`,
t1. city,
t1.code,
t1.uid,
CASE WHEN SUM((t1.city = t2.city) * (t1.code = t2.code)) - 1
THEN 'Yes'
ELSE 'No' END `Match`,
SUM((t1.city = t2.city) * (t1.code = t2.code) * (t1.uid = t2.uid)) - 1 same_uid,
SUM((t1.city = t2.city) * (t1.code = t2.code) * (t1.uid != t2.uid)) different_uid,
SUM(t1.uid = t2.uid) uid_count
FROM cities t1
CROSS JOIN cities t2
WHERE t1.`Date` >= '2020-01-01' AND t1.`Date` < '2020-03-01'
GROUP BY t1.ID, t1.`Date`, t1. city, t1.code, t1.uid
ORDER BY t1.ID
скрипка
PS. Multilpyings в SUM()
s можно заменить на AND
.
что, если у меня есть эта информация в двух разных таблицах
SELECT t11.ID,
t11.`Date`,
t21.city,
t21.code,
t11.uid,
CASE WHEN SUM((t21.city = t22.city) * (t21.code = t22.code)) - 1
THEN 'Yes'
ELSE 'No' END `Match`,
SUM((t21.city = t22.city) * (t21.code = t22.code) * (t11.uid = t12.uid)) - 1 same_uid,
SUM((t21.city = t22.city) * (t21.code = t22.code) * (t11.uid != t12.uid)) different_uid,
SUM(t11.uid = t12.uid) uid_count
FROM /* cities t1 */
(Table_1 t11 NATURAL JOIN Table_2 t21)
CROSS JOIN /* cities t2 */
(Table_1 t12 NATURAL JOIN Table_2 t22)
WHERE t11.`Date` >= '2020-01-01' AND t11.`Date` < '2020-03-01'
GROUP BY t11.ID, t11.`Date`, t21.city, t21.code, t11.uid
ORDER BY t11.ID
скрипка