r/SQL • • Apr 24 '26

MySQL Removing duplicate records from CASE buckets

I have the following code that is technically bucketing my data correctly, but it's not doing what I intended.

The query is counting the UserId__c every time it falls into a bucket, but I want it to only capture the FIRST bucket it falls into.

SELECT COUNT( DISTINCT UserId__c),
  CASE
WHEN DATEDIFF('day', LoginTime__c, NOW()) BETWEEN 0 AND 7 THEN '0 - 7 Days'
WHEN DATEDIFF('day', LoginTime__c, NOW()) BETWEEN 8 AND 14 THEN '08 - 14 Days'
WHEN DATEDIFF('day', LoginTime__c, NOW()) BETWEEN 15 AND 30 THEN '15 - 30 Days'
WHEN DATEDIFF('day', LoginTime__c, NOW()) > 30 THEN '31+ Days'
  END AS Bucket
FROM LoginHistory__dlm l
INNER JOIN User_Temp__dlm u
ON l.UserId__c = u.user_ID__c
GROUP BY Bucket
ORDER BY Bucket asc

I'm getting the following results:

Bucket Count of Rows
0 - 7 Days 1,229
08 - 14 Days 1,337
15 - 30 Days 1,246
31+ Days 1,889

When I remove the buckets, the true count of DISTINCT UserId__c is 1,912 - this total is correct.

How do I stop the query from counting every instance of UserId__c?

This is in Salesforce CRMA, so it's technically Data 360 SQL (if that matters).

4 Upvotes

12 comments sorted by

11

u/Lanky_Debate_5267 Apr 24 '26

You’re grouping on the bucket before you’ve reduced each user down to a single row, so the same UserId__c can land in multiple buckets depending on different LoginTime__c values.

2

u/Intrepid_Shake_1438 Apr 24 '26

Makes sense, thanks for pointing that out!

5

u/thargoallmysecrets Apr 24 '26

Yup.  Need to select only the most recent login time only via a partition or rank otherwise past logins will count as multiples

4

u/Imaginary__Bar Apr 24 '26

With Temp_Table as (
Select
UserID__c,
Min(LoginTime__c) as MinLoginTime
From XXX
Group By UserID__c)

Select
Case
When MinLoginTime... etc...,
UserId__c
From Temp_Table

1

u/Intrepid_Shake_1438 Apr 24 '26

I'll give this a try, thanks!

-1

u/NoYouAreTheFBI Apr 25 '26

An excellent answer:

Why because it follows the - Golden rule of query optimisation.

  • Never use CASE to “extract data subsets” & Never use CTEs for simple branching logic

This pattern has a few names:

  • Two-phase / Pre aggregation
  • ETL Staging
  • Dimension Reduction

Big-O of your pattern:

  • O(n) (most common case with hash aggregation)
  • O(n log n) (if sort-based aggregation is used)

17

u/SootSpriteHut Apr 24 '26

I would use a cte or temp table that takes the min login_time and then do your buckets based on that

3

u/Icy_Clench Apr 24 '26

You just need the max login time per user. (Other people are saying min but this is incorrect since min is their first login, not most recent.)

WITH last_login AS (SELECT bucket(MAX(login_time)) AS bucket FROM logins GROUP BY user_id)

SELECT bucket, COUNT(*) FROM last_login GROUP BY bucket

Also a good practice is to turn your buckets into a table you can join on instead of writing out several repetitive case statements. You would JOIN time_buckets tb ON DATEDIFF(…) BETWEEN tb.low AND tb.high. Now you can reuse the logic contained in the table.

1

u/GRRRRRRRRRRRRRG Apr 24 '26

First you need to deside what data you want. You want the user id appear once in your count but you don't say what it should be. Should it be the first log in date or last log in or something else....  you need to reduce your log in history data so it has one row per user id and then do your buckets....

1

u/aivanelabs Apr 25 '26

Try using a subquery to get each user's earliest login bucket first, then count.

WITH UserFirstBucket AS ( SELECT UserId__c, FIRST_VALUE(CASE WHEN DATEDIFF('day', LoginTime__c, NOW()) BETWEEN 0 AND 7 THEN '0 - 7 Days' WHEN DATEDIFF('day', LoginTime__c, NOW()) BETWEEN 8 AND 30 THEN '8 - 30 Days' END) OVER (PARTITION BY UserId__c ORDER BY LoginTime__c) AS FirstBucket FROM LoginHistory__dlm l INNER JOIN User_Temp__dlm u ON l.UserId__c = u.user_ID__c ) SELECT FirstBucket AS Bucket, COUNT(DISTINCT UserId__c) AS Count_of_Rows FROM UserFirstBucket GROUP BY FirstBucket ORDER BY FirstBucket ASC

This way each user is only counted in their first login bucket, no duplicates.

-6

u/MrEs Apr 24 '26

Just ask Ai bro