How to get the first result of rows with duplicate dates?
Hi,
I have a table that contains the following fields:
- An ID
- An event
- A date-time for each event
It looks like this:
ID
| Event
| Event Date
|
1122334
| Event 1
| 2016-10-01 10:00:00
|
1122334
| Event 2
| 2016-10-01 10:00:00
|
1122335
| Event 1
| 2018-06-23 11:43:22
|
1122336
| Event 2
| 2019-02-02 08:04:13
|
1122336 | Event 1 | 2019-02-03 06:07:17
|
1122336 | Event 2 | 2019-02-06 13:37:44
|
My goal is to find the first event for a given ID, based on the date so that I have a list of unique IDs and their corresponding events and dates like so:
ID
| Event
| Event Date
|
1122334
| Event 1
| 2016-10-01 10:00:00
|
1122335
| Event 2
| 2018-06-23 11:43:22
|
1122336
| Event 2
| 2019-02-02 08:04:13
|
The following query gets me close, but due to the fact that there are duplicate values in the date-time column (to the second), I still get duplicate entries:
- SELECT
- "Table_1"."Event"
- FROM (SELECT
- "Table_1"."ID" as "ID",
- MIN("Table_1"."Event Date") AS "Event Date"
- FROM "Table_1"
- GROUP BY "ID"
- ) AS "First Action"
- LEFT JOIN "Table_1" ON "Table_1"."ID" = "First Action"."ID"
- AND "Table_1"."Event Date" = "First Action"."Event Date"
This results in:
ID
| Event
| Event Date
|
1122334
| Event 1
| 2016-10-01 10:00:00
|
1122334
| Event 2
| 2016-10-01 10:00:00
|
1122335
| Event 1
| 2018-06-23 11:43:22
|
1122336
| Event 2
| 2019-02-02 08:04:13
|
For record 1122334 there are still two entries because the date is duplicated. My logical solution would be to select only the first row, but I am not sure how to do this.
I can't change the data in the underlying table, and I tried various implementations of DISTINCT and COUNT(*) that didn't yield good results.
I would appreciate any assistance in getting the correct SQL query to get the results.
Sticky Posts
Announcing Zoho Analytics 6.0 Beta!
We are delighted to open up the next major version, Zoho Analytics 6.0 Beta! The new version comes packed with a wide range of functionalities for all persona, namely business users, data analysts, data engineers, and data scientists. Zoho Analytics team
What's New in Zoho Analytics - August 2024
Hello Users! We are back with the latest updates and enhancements made to Zoho Analytics. Keep reading to learn more about them. Connect to the data hosted in the cloud without allow-listing the IP addresses Utilize Zoho Databridge to connect to the data
We are coming to your city! Zoho Analytics Community Meetup
Hello, business leaders and data enthusiasts! We are delighted to announce that registrations are now open for the ZUG meetups, and we can't wait for you to be a part of them. Our in-house analytics experts are geared up to lead discussions on constructing
Zoho Analytics: 2021 Look Back
As we start a new year in 2022, here's some of our top moments from 2021. Zoho Analytics in 2021
[Customer Talk] PREMO Group's Analyst Interview at Zoho Day 2022
Premo Group, a 50 year old Spanish Manufacturing Company, has been our long-standing customer with #ZohoAnalytics. They've been using our platform for their end-to-end, unified business analytics solution. Hear more from Claudio Cabeza, Director at PREMO