My Table Details image with test data
I have come up with below code:
select tableName,userDets,regDate
from (
(SELECT 'Table1' AS tableName, t1.username as userDets, t1.created_date as regDate FROM table1 t1 group by username )
union all
(SELECT 'Table2' AS tableName, t2.username as userDets, t2.created_date as regDate FROM table2 t2 group by username )
union all
(SELECT 'Table3' AS tableName, t3.username as userDets, t3.created_date as regDate FROM table3 t3 group by username )
) AS allTableCombo
group by allTableCombo.userDets
If in this code i use order by date at last and then make it subquery for another select with group by username , I can obtain required output. But I dont want to use sbuquery.
Please if anyone could provide me with an alternative.. I tried with self join but was not success on using it....
Related
I have a complex mysql query where one of the Select fields is Min(value). Since all the 'values' are unique, is there also a way to get found min value's row id along?
In other words if we simplify the query to this question, it is like this:
SELECT t1.name, MIN(t2.value) AS minval
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY id_user
How can i now know which t2.id was chosen for lowest t2.value for particular user? Thank you!
Use ROW_NUMBER() to find the first value of each id_user
You can replace * with the fields you need
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY t2.id_user ORDER BY t2.value) as rnk
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
) as X
WHERE X.rnk = 1
Maybe this simple, dont know how complex your statement is:
SELECT name,value,id
FROM(
SELECT t1.name,t2.value,t2.id
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY t2.id,id_user
ORDER BY t1.name,t2.id asc) as test
GROUP BY name;
I have a large table with just 2 column. One is the primary id column and other is a data column.
I need to select only the records that is not-duplicated in the table. I tried the below query but it takes much time and not sure if it really work.
select * from (select column_name
from table_name
group by column_name
having count(*) = 1) x;
What do you think?
I am also open to other tries if it will do the job faster.
You can left join the same table or use subquery to check for duplicates.
This should be easier for SQL server as it would not count all duplicates.
Something like this:
SELECT
t1.column_name
FROM
table_name AS t1
WHERE
NOT EXISTS (
SELECT
*
FROM
table_name AS t2
WHERE
t2.column_name = t1.column_name
AND t2.id != t1.id
)
OR
SELECT
t1.column_name
FROM
table_name AS t1
LEFT JOIN table_name t2 ON (
t2.column_name = t1.column_name
t2.id != t1.id
)
WHERE
t2.column.name IS NULL
I have a table in MySQL which I store registrations info.
I need to select all rows but only the latest according to email field and ID field
My table:
and I need
select *
from yourTablename
where (id,email) in
(select max(id),email from yourTablename group by email);
Another approach with Inner Join:(As suggested by Tim)
SELECT t1.*
FROM t t1
INNER JOIN
(
SELECT email, MAX(id) AS max_id
FROM t t2
GROUP BY email
) t2
ON t1.email = t2.email AND
t1.id = t2.max_id;
DEMO
Thanks for the replies.
Using
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT email, MAX(name) AS max_name
FROM yourTable
GROUP BY email
) t2
ON t1.email = t2.email AND
t1.name = t2.max_name;
Worked fine.
Thanks
I have a location table in my database which contains location data of all the users of my system.
The table design is something like
id| user_id| longitude| latitude| created_at|
Now I have a array of user ids and I want to write a sql query to select the latest location of all these users. Can you please help me with this sql query ?
In the user_id in (......) at the end of the query you sould insert your array of user ..
select * from my_table
where (user_id , created_at) in (select user_id, max(created_at)
from my_table
group by user_id)
and user_id in ('user1','user2',... ) ;
SELECT
t1.ID,
t1.user_id,
t1.longitude,
t1.latitude,
t1.created_at
FROM
YourTable t1
INNER JOIN
(SELECT MAX(id), user_id FROM YourTable GROUP BY user_id) t2 on t2.user_id = t1.user_id
INNER JOIN
yourArrayTable ON
yourArrayTable.user_id = t1.user_id
If I have a table with two columns, name and timestamp, and a bunch of rows that will have shared names. How do I select the most recent row of each set of rows that shares the same name?
Thanks!
SELECT name, MAX(timestamp) FROM Table1 GROUP BY name
EDIT: Based on the comment, please try the following:
SELECT name, timestamp, col3, col4
FROM Table1 t1
WHERE timestamp = (SELECT MAX(t2.timestamp)
FROM Table1 t2
WHERE t1.name = t2.name);
Added by Mchl
Version with no dependent subquery (should perform better)
SELECT
t1.name, t1.timestamp, t1.col3, t1.col4
FROM
Table1 AS t1
CROSS JOIN (
SELECT
name, MAX(timestamp) AS timestamp
FROM
Table1
GROUP BY
name
) AS sq
USING (name,timestamp)
Then you need a subquery:
SELECT columns
FROM Table1 t1
WHERE row_id = (SELECT row_id
FROM table1 t2
WHERE t1.name = t2.name
ORDER BY timestamp DESC
LIMIT 1)
GROUP BY name
Edited, forgot the group by name