по возможности самый краткий (три блатных аккорда) быстрого освоения
SELECT*
FROM
(
SELECT org_name, docPublishDate, inn, kpp, contactEMail contactPerson, contactPhone, period_end, period_start,
DENSE_RANK() OVER(ORDER BY docPublishDate DESC) AS rank
FROM table1, table2
WHERE table2.customer_fullname LIKE table1.org_name AND table2.period_end LIKE '%2021%' LIMIT 100
) AS t1
WHERE rank=1
SELECT *,
( (SELECT MAX(amount) FROM books) - amount ) AS orderAmount
FROM books
DELETE FROM guild_users
WHERE id
NOT IN
(
SELECT id
FROM
(
SELECT id,
ROW_NUMBER(PARTITION BY user_id, user_money, user_description, user_xp, lastMessage, user_warns, user_warn_reasons, user_private_role, user_exception, Time, strTime, startTime, endTime, user_timelvl, user_timexp ORDER BY user_lvl DESC) AS rn
FROM guild_users
) AS t1
WHERE rn = 1
)
CREATE TABLE profil...только в первой части дампа, а в остальных 11 удалить, оставить только insert'ы
SELECT *
FROM table
WHERE text LIKE '%555%'
AND NOT (
text LIKE '%5550%'
OR text LIKE '%5551%'
OR text LIKE '%5552%'
OR text LIKE '%5553%'
OR text LIKE '%5554%'
OR text LIKE '%5555%'
OR text LIKE '%5557%'
OR text LIKE '%5557%'
OR text LIKE '%5558%'
OR text LIKE '%5559%'
)
-- Пишем телефоны в пустые значения
UPDATE table AS t1
SET t1.Phone=t2.Phone
FROM table AS t1
INNER JOIN table AS t2
ON t1.FIO=t2.FIO
AND t1.phone IS NULL
AND t2.phone IS NOT NULL
-- Удаляем лишнее
DELETE
FROM table
WHERE Id NOT IN
(
SELECT Id
FROM
(
SELECT MAX(Id) AS id, FIO, Phone
FROM table
GROUP BY FIO,
Phone
) AS t1
)
SELECT user.name,
t1.cnt AS status1,
t2.cnt AS status2,
t3.cnt AS status3
FROM user
LEFT JOIN
(
SELECT user_id,
COUNT(status) AS cnt
FROM zayavki
WHERE status=1
GROUP BY user_id
) AS t1
ON user.id = t1.user_id
LEFT JOIN
(
SELECT user_id,
COUNT(status) AS cnt
FROM zayavki
WHERE status=2
GROUP BY user_id
) AS t2
ON user.id = t2.user_id
LEFT JOIN
(
SELECT user_id,
COUNT(status) AS cnt
FROM zayavki
WHERE status=3
GROUP BY user_id
) AS t3
ON user.id = t3.user_id
SELECT t1.slug, COALESCE(t2.lang, t3.lang, 'language not found') AS lang
FROM
(
SELECT DISTINCT slug
FROM tbl
) AS t1
LEFT JOIN tbl AS t2
ON t1.slug=t2.slug
AND t2.lang = 'ru'
LEFT JOIN tbl AS t3
ON t1.slug=t3.slug
AND t3.lang = 'en'