I have data like this
emp_id| name | log |
16646 | bella | 2023-01-30 07:28:54 |
9050 | mark | 2023-01-30 07:22:12 |
8612 | nandya | 2023-01-30 07:20:35 |
7965 | arya | 2023-01-30 06:21:39 |
7965 | arya | 2023-01-30 06:21:40 |
My query
SELECT h.emp_id, p.name, h.log
FROM presence.history h
JOIN employee p on p.emp_id = h.emp_id
and h.log >= '2023-01-30 05:00:00' and h.log <= '2023-01-30 07:45:00'
I want the duplicate data only show one data and for the 'log' field show the early data.
the result I expected
emp_id| name | log |
16646 | bella | 2023-01-30 07:28:54 |
9050 | mark | 2023-01-30 07:22:12 |
8612 | nandya | 2023-01-30 07:20:35 |
7965 | arya | 2023-01-30 06:21:39 |
SELECT emp_id, name, MIN(log) as log
FROM table_name
GROUP BY emp_id, name;
Related
Not sure how to explain this properly in the title, i might as well show the data.
I have a table (SQLFIDDLE):
+----+----------+------------+
| id | session | updated |
+----+----------+------------+
| a | c9c9c9c9 | 2018-22-03 |
| a | c9c9c9c9 | 2018-21-03 |
| a | s9s9s9s9 | 2018-20-03 |
| a | s9s9s9s9 | 2018-19-03 |
| b | s8s8s8s8 | 2018-05-02 |
| b | s8s8s8s8 | 2018-04-02 |
| c | s7s7s7s7 | 2018-03-01 |
| c | s7s7s7s7 | 2018-02-01 |
| d | s2s2s2s2 | 2018-01-01 |
+----+----------+------------+
Currently i'm using this query
SELECT id,
session,
Max(updated) AS updated_at
FROM reports
GROUP BY id,
session
ORDER BY updated_at DESC
LIMIT 4
This is the SQLFIDDLE: http://sqlfiddle.com/#!9/3c5272/1
Which is giving me this but i don't want row 2 because it's a duplicate ID
+----+----------+------------+
| id | session | updated |
+----+----------+------------+
| a | c9c9c9c9 | 2018-22-03 |
| a | s9s9s9s9 | 2018-20-03 |
| b | s8s8s8s8 | 2018-05-02 |
| c | s7s7s7s7 | 2018-02-01 |
+----+----------+------------+
I want a results like this
+----+----------+------------+
| id | session | updated |
+----+----------+------------+
| a | c9c9c9c9 | 2018-22-03 |
| b | s8s8s8s8 | 2018-05-02 |
| c | s7s7s7s7 | 2018-02-01 |
| d | s2s2s2s2 | 2018-01-01 |
+----+----------+------------+
I tried
SELECT a.id,
(SELECT session
FROM reports AS b
WHERE b.id = a.id
LIMIT 1),
Max(a.updated) AS updated_at
FROM reports AS a
GROUP BY a.id
ORDER BY updated_at DESC
LIMIT 4
But my MYSQL just keeps getting timeout with 100% CPU load, I have about 328745+ rows in that table currently.
Use "DISTINCT" when using SELECT to filter duplicate values. Visit this link to know more about DISTINCT.
You need to identify on how the data from session column will be selected, because you have id and session in the GROUP BY clause, and there are multiple sessions per id.
If this is not a concern, just remove session in the GROUP BY
SELECT
id,
session,
MAX(updated) AS updated_at
FROM reports
GROUP BY id
ORDER BY updated_at
DESC LIMIT 4
SQL FIDDLE
I have a table, let's say, like this one:
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| user_id | mortgage_id | value | classification | created_at |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 6c1e1f12-2e5d-488d-b02d-29fcffe783f2 | 1e76bcbb-70ee-4966-87fd-1d6024a04513 | 0 | initial | 2014-08-23 14:25:42 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 49dc3dab-d2d0-400b-b964-71e03339d475 | 59366911-f1a8-4a8c-b7ea-c3257d04478e | 1 | created | 2015-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 76ce889b-2f2c-435f-8754-7c5ec15cbfcb | b962e26b-1ba6-4547-8eb8-167989a0705e | 5 | created | 2016-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 5d9f1892-05c0-4b0a-b5d9-a501595fa351 | fb4be36e-e156-4c1b-bd40-422d30646f8e | 8 | created | 2016-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 49dc3dab-d2d0-400b-b964-71e03339d475 | 2cee0bc7-744f-4f51-a094-f5eb66ac482e | 2 | created | 2017-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
| 76ce889b-2f2c-435f-8754-7c5ec15cbfcb | b0d27c9e-907c-43df-abd2-5772785cb91c | 0 | created | 2017-08-23 14:26:11 |
|--------------------------------------+--------------------------------------+--------+----------------+---------------------|
I'm trying to fetch/get all the distinct/unique user_ids that don't have any records from a given moment in time and onwards.
For instance, if I choose that "time frame" to be: After 2017-01-01 00:00:00, the return would be:
|--------------------------------------+
| user_id |
|--------------------------------------+
| 6c1e1f12-2e5d-488d-b02d-29fcffe783f2 |
|--------------------------------------+
| 5d9f1892-05c0-4b0a-b5d9-a501595fa351 |
|--------------------------------------+
I have this query, but I think there should be a better way to do this:
SET #timestamp = '2017-01-01 00:00:00';
SELECT DISTINCT user_id
FROM mortgages
WHERE user_id NOT IN (SELECT DISTINCT user_id FROM mortgages WHERE created_at > #timestamp);
I would use group by:
select user_id
from mortgages
group by user_id
having max(created_at) <= #timestamp;
The result I want.
+--------+-------------+------------+--------+
| Tag | most_recent | Comment | Author |
+--------+-------------+------------+--------+
| TAG001 | 2015-07-23 | Something3 | AM |
| TAG002 | 2015-07-25 | Something5 | BN |
+--------+-------------+------------+--------+
The tables I have:
Status Table
+--------+-------------+------------+
| Tag | Status | DateStatus |
+--------+-------------+------------+
| TAG001 | Not Started | |
| TAG002 | Complete | 2015-07-23 |
+--------+-------------+------------+
Comments Table
+----+--------+-------------+------------+--------+
| ID | Tag | DateCreated | Comment | Author |
+----+--------+-------------+------------+--------+
| 1 | TAG001 | 2015-07-22 | Something1 | JS |
| 2 | TAG002 | 2015-07-23 | Something2 | JS |
| 3 | TAG001 | 2015-07-23 | Something3 | AM |
| 4 | TAG002 | 2015-07-23 | Something4 | AS |
| 5 | TAG002 | 2015-07-25 | Something5 | BN |
+----+--------+-------------+------------+--------+
I've tried 4 different queries, each getting progressively more complicated, but still not working.
The queries I have tried:
Query 1)
SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];
Result 1)
+--------+-------------+
| Tag | most_recent |
+--------+-------------+
| TAG001 | 2015-07-23 |
| TAG002 | 2015-07-25 |
+--------+-------------+
Just gives me the most recent date, but no values.
Query 2)
SELECT Comments.[Tag], Max(Comments.[DateCreated]) AS most\_recent
FROM Comments
GROUP BY Comments.[Tag];
Result 2)
+--------+-------------+------------+
| Tag | most_recent | Comment |
+--------+-------------+------------+
| TAG001 | 2015-07-22 | Something1 |
| TAG001 | 2015-07-23 | Something3 |
| TAG002 | 2015-07-23 | Something2 |
| TAG002 | 2015-07-23 | Something4 |
| TAG002 | 2015-07-25 | Something5 |
+--------+-------------+------------+
Now I see all the information I want, but I cannot filter for the most recent.
I tried DISTINCT, but it didn't work.
Query 3)
Modified from here: MYSQL - Join most recent matching record from one table to another
SELECT Status.\*,Comments.\*
FROM Status S
LEFT JOIN Comments C ON S.tag = C.tag
JOIN(SELECT x.tag, MAX(x.DateCreated) AS MaxCommentDate FROM Comments x
GROUP BY x.tag) y ON y.tag = x.tag AND y.MaxCommentDate = x.DateCreated
Result: Syntax error (missing operator) in query expression
Query 4)
Modified from here:
Left Join to most recent record
SELECT
Status.\*,Comments.\*
FROM Status S
LEFT JOIN
(
Comments C
INNER JOIN
(
SELECT
x.tag, MAX(x.DateCreated) AS MaxCommentDate
FROM
Comments x
GROUP BY
x.tag
)
y
ON y.tag = x.tag
AND y.MaxCommentDate = x.DateCreated
)
ON S.tag = C.tag;
Result: Syntax Error on JOIN
Not having much luck...thanks in advanced.
Thanks.
The following seems to work for me in Access 2010:
SELECT c.Tag, c.DateCreated AS most_recent, c.Comment, c.Author
FROM
(
SELECT Tag, MAX(DateCreated) AS MaxDate
FROM Comments
GROUP BY Tag
) AS md
INNER JOIN
Comments AS c
ON c.Tag = md.Tag AND c.DateCreated = md.MaxDate
I have a MySQL table with a million rows. The result of my query needs to contain 100 rows and has to be ordered by date.
I would now like to add a clause to the query that makes it possible to tell the database to "start the result with the row that comes after the row with the id '5'", for example. My result would therefore include the rows with the ids 4, 3, 2, 6, 8, ...
Are you, dear reader, the sought database wizard who could put me out of my misery? ;)
Query:
SELECT id, type, content, date
FROM my_table
WHERE type = 'FI'
ORDER BY date ASC
LIMIT 100;
Result:
| id | type | content | date |
|----|------|----------|------------|
| 7 | FI | ContentR | 2014-01-01 |
| 9 | FI | ContentS | 2014-01-13 |
| 1 | FI | ContentT | 2014-02-09 |
| 5 | FI | ContentU | 2014-03-27 |
| 4 | FI | ContentV | 2014-03-30 | ---|
| 3 | FI | ContentW | 2014-04-01 | |
| 2 | FI | ContentX | 2014-06-10 | |- The result I want
| 6 | FI | ContentY | 2014-09-03 | |
| 8 | FI | ContentZ | 2014-12-09 | |
... ---|
SELECT id, type, content, date
FROM my_table
WHERE type = 'FI'
AND date > (SELECT date FROM my_table WHERE id = 5)
ORDER BY date ASC
LIMIT 100;
I have two tables that look something like this:
TABLE_conversations:
+-----------------+----------+----------------+------------+---------------------+--------+
| CONVERSATION_ID | QUEUE_ID | CONTACT_NUMBER | CONTACT_ID | DATE_CREATED | STATUS |
+-----------------+----------+----------------+------------+---------------------+--------+
| 1 | 1 | 15551112222 | 9000001 | 2014-09-12 00:28:24 | ACTIVE |
| 2 | 1 | 15553334444 | 9000002 | 2014-09-12 00:32:08 | ACTIVE |
+-----------------+----------+----------------+------------+---------------------+--------+
TABLE_messages:
+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+
| MESSAGE_ID | CONVERSATION_ID | FROM_NUMBER | TO_NUMBER | DIRECTION | SENDER | TIMESTAMP | VIEWED | MESSAGE | STATUS |
+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+
| 1 | 1 | 15551112222 | 17021112222 | IN | 9000001 | 2014-09-12 00:30:11 | 1 | Hello! Is this working? | ACTIVE |
| 2 | 1 | 17021112222 | 15551112222 | OUT | 8000001 | 2014-09-12 00:31:05 | 1 | Good evening! Of course! | ACTIVE |
| 3 | 1 | 15551112222 | 17021112222 | IN | 9000001 | 2014-09-12 00:31:27 | 1 | Perfect. Thank you! | ACTIVE |
| 4 | 1 | 17021112222 | 15553334444 | OUT | 8000002 | 2014-09-12 00:32:52 | 1 | Ticket 11251 is ready for pickup. | ACTIVE |
+------------+-----------------+-------------+-------------+-----------+---------+---------------------+--------+-----------------------------------------------------------------------------------------------------------------+--------+
I'm trying to run a query to select the CONVERSATION_ID, CONTACT_NUMBER, CONTACT_ID and most recent TIMESTAMP and grouping by phone number:
SELECT TABLE_conversations.CONVERSATION_ID, TABLE_conversations.CONTACT_NUMBER,
TABLE_conversations.CONTACT_ID, MAX(TABLE_messages.TIMESTAMP)
FROM TABLE_conversations, TABLE_messages
WHERE TABLE_conversations.STATUS='ACTIVE'
AND TABLE_messages.STATUS='ACTIVE'
GROUP BY CONTACT_NUMBER
ORDER BY TABLE_messages.TIMESTAMP;
The output I'm getting is below:
+-----------------+----------------+------------+-------------------------------+
| CONVERSATION_ID | CONTACT_NUMBER | CONTACT_ID | MAX(TABLE_messages.TIMESTAMP) |
+-----------------+----------------+------------+-------------------------------+
| 1 | 15551112222 | 9000001 | 2014-09-12 00:32:52 |
| 2 | 15553334444 | 9000002 | 2014-09-12 00:32:52 |
+-----------------+----------------+------------+-------------------------------+
I'm getting the same TIMESTAMP for both. The result I want is 2014-09-12 00:31:27 for 15551112222 and 2014-09-12 00:32:52 for 15553334444.
Really appreciate any help!
You're missing join conditions between the tables, so you're getting a full cross-product. So every conversation is being joined with every message, not just the messages from that conversation.
SELECT TABLE_conversations.CONVERSATION_ID, TABLE_conversations.CONTACT_NUMBER,
TABLE_conversations.CONTACT_ID, MAX(TABLE_messages.TIMESTAMP)
FROM TABLE_conversations
JOIN TABLE_messages ON TABLE_conversations.conversation_id = TABLE_messages.conversation_id
WHERE TABLE_conversations.STATUS='ACTIVE'
AND TABLE_messages.STATUS='ACTIVE'
GROUP BY CONTACT_NUMBER
ORDER BY TABLE_messages.TIMESTAMP;
Your sql cross join all rows from two tables, so the max timestamp for any group is the same.
SELECT TABLE_conversations.CONVERSATION_ID,
TABLE_conversations.CONTACT_NUMBER,
TABLE_conversations.CONTACT_ID,
MAX(TABLE_messages.TIMESTAMP)
FROM TABLE_conversations
JOIN TABLE_messages
ON TABLE_conversations.CONVERSATION_ID = TABLE_messages.CONVERSATION_ID
WHERE TABLE_conversations.STATUS='ACTIVE'
AND TABLE_messages.STATUS='ACTIVE'
GROUP BY CONTACT_NUMBER
Suggest you to get rid of the group by and aggregation function to see what's different between full cross join and inner join. Such as:
SELECT TABLE_conversations.CONVERSATION_ID,
TABLE_conversations.CONTACT_NUMBER,
TABLE_conversations.CONTACT_ID,
TABLE_messages.TIMESTAMP
FROM TABLE_conversations
JOIN TABLE_messages
ON TABLE_conversations.CONVERSATION_ID = TABLE_messages.CONVERSATION_ID
--without on clause above, comes to the full cross join
WHERE TABLE_conversations.STATUS='ACTIVE'
AND TABLE_messages.STATUS='ACTIVE'
ORDER BY TABLE_messages.TIMESTAMP;