SQL Snippets to Remember
February 8, 2021
note-to-self
Row Number Over:
SELECT * FROM (
SELECT
ROW_NUMBER() OVER(
PARTITION BY JSON_VALUE(jsonData, ‘$.path’)
ORDER BY created_at
) rowNum,
JSON_VALUE(jsonData, ‘$.path’) as FP,
*
FROM <table> (nolock)
WHERE JSON_VALUE(jsonData, ‘$.path’) IN (
SELECT JSON_VALUE(jsonData, ‘$.path’)
FROM <table> (nolock)
WHERE intake_id <> 0
GROUP BY JSON_VALUE(jsonData, ‘$.path’)
HAVING count(*) > 1
)
) a WHERE rowNum = 1
;
Find Duplicates:
SELECT id, COUNT(*)
FROM <table>
GROUP BY id
HAVING COUNT(*) > 1;
To determine if there is a duplicate:
SELECT
(select count(1) from <table>) as C1,
(select count(distinct <columns>) from <table>) as C2;