So my table has:
column1, column2, column3 & datetime
SELECT * FROM mytable WHERE column1 = :column1
CONCAT column1 & column2 (column1NRcolumn2) to get a string to search for duplicates.
SELECT 1 column1_column2 WHERE datetime is highest
What i want is to combine two cells to create a new temponary column. (column1NRcolumn2)
Then i want to look for duplicates and then select the latest column1NRcolum2 WHERE datetime is highest/latest
How can i manage this?
You could use a subquery for concatenated columns having count > 1 and join to your table for max datetime
select t.my_concat, max(m.datetime)
from mytable m
inner join (
SELECT concat(column1,column2) my_concat
FROM mytable
group by concat(column1,column2)
having count(*)>1
) t
group by t.my_concat
and if you need thre related mytable columns you need a join on the max datetime
select m2.*
from mytable m2
inner join
(
select t.my_concat, max(m.datetime) max_datetime
from mytable m
inner join (
SELECT concat(column1,column2) my_concat
FROM mytable
group by concat(column1,column2)
having count(*)>1
) t
group by t.my_concat
) t2 on t2.my_concat = m2. concat(column1,column2)
and t.max_datetime = m2.datetime
Related
Consider the following table:
As shown in image, I want to return all the data from only first distinct id. How can I achieve that in MySQL ?
You can filter with a subquery. Assuming that by first you mean the row with the earlier start_time, that would be:
select t.*
from mytable t
where t.start_time = (
select min(t1.start_time) from mytable t1 where t1.call_unique_id = t.call_unique_id
)
from your_table t1
join
(
select min(call_unique_id) as id
from your_table
group by start_time
) t2 on t1.id = t2.id
group by should also do the job. so try
select * from your_table group by call_unique_id
I want to make a query where I want to get all the values from a table1 prior to the max date from the date column.
I have table1 with column names as id,ord_date.
Further, I want to join it to another table2 with column as id, name
SELECT s.title,s.title_id,t.ord_date
FROM titles s
INNER JOIN (
SELECT title_id
,max(ord_date) AS ord_date
FROM sales
where ord_date < max(ord_date)
GROUP BY title_id
) t ON s.title_id = t.title_id
I want solution query showing title from table2 and dates from table1
Query.
SELECT s.title,s.title_id,t.ord_date
FROM titles s
INNER JOIN (
SELECT x.title_id, max(x.ord_date) AS ord_date
FROM sales x
where x.ord_date < (
select max(m.ord_date) from sales m where m.title_id = x.title_id
)
GROUP BY title_id
) t ON s.title_id = t.title_id
how can i make something like this work?
INSERT INTO age.page(domain,title_count,youtube_count,ipaddress,updated)
SELECT * FROM
(
SELECT domain,
COUNT(domain) AS titlecount,
(SELECT COUNT(*) FROM table2 WHERE title = table1.title) AS YoutubeCount, ipaddress
NOW() AS timeNow
FROM table1
GROUP BY domain
ORDER BY title DESC
) a;
I want to use a subquery to get a count of a different table but use the same field from the main query.
the reason i want to do this is so i dont have to run two queries instead its only one.
You can do this COUNT in a subquery and then JOIN it with the first table:
INSERT INTO age.page(domain, title_count, youtube_count, ipaddress, updated)
SELECT * FROM
(
SELECT
domain,
COUNT(domain) AS titlecount,
t2.titlecount AS YoutubeCount,
ipaddress,
NOW() AS timeNow
FROM table1
INNER JOIN
(
SELECT title, COUNT(*) Titlecount
FROM table2
GROUP BY title
) AS t2 ON t2.title = table1.title
GROUP BY domain
ORDER BY table1.title DESC
) a;
I have this query:
SELECT t1.* FROM table t1
JOIN
(
SELECT MIN(orig_id) min_orig_id, MAX(orig_id) max_orig_id
FROM table
GROUP BY left(datetime,4), mid(datetime,5,2),
mid(datetime,7,2), mid(datetime,10,2), mid(datetime,13,2)
) t2
ON t1.orig_id = t2.min_orig_id OR t1.orig_id = t2.max_orig_id;
It groups records by first & last record in each minute.
Now I want to group by first & last record in each 3 minutes.
Any idea?
Thanks
This might be what you're looking for
SELECT t1.* FROM table t1
JOIN
(
SELECT MIN(orig_id) min_orig_id, MAX(orig_id) max_orig_id
FROM table
GROUP BY concat(left(datetime,4), mid(datetime,5,2),
mid(datetime,7,2), mid(datetime,10,2),(cast mid(datetime,13,2) as integer)/3)
) t2
ON t1.orig_id = t2.min_orig_id OR t1.orig_id = t2.max_orig_id;
If datetime is a timestamp/datetime data type and not a string
SELECT t1.* FROM table t1
JOIN
(
SELECT MIN(orig_id) min_orig_id, MAX(orig_id) max_orig_id
FROM table
GROUP BY UNIX_TIMESTAMP(datetime)/3
) t2
ON t1.orig_id = t2.min_orig_id OR t1.orig_id = t2.max_orig_id;
is much cleaner
I have table with id, item_id, value (int), run (datetime) and i need select value diff betwen last two run per *item_id*.
SELECT item_id, ABS(value1 - value2) AS diff
FROM ( SELECT h.item_id, h.value AS value1, h2.value AS value2
FROM ( SELECT id, item_id, value
FROM table_name
GROUP BY item_id
ORDER BY run DESC) AS h
INNER JOIN ( SELECT id, item_id, value
FROM table_name
ORDER BY run DESC) AS h2
ON h.item_id = h2.item_id AND h.id != h2.id
GROUP BY item_id) AS h3
I believe this should do the trick for you. Just replace table_name to correct name.
Explanation:
Basicly I join the table with itself in a run DESC order, JOIN them based on item_id but also on id. Then I GROUP BY them again to remove potential 3rd and so on cases. Lastly I calculate the difference between them through ABS(value1 - value2).
SELECT t2.id, t2.item_id, (t2.value- t1.value) valueDiff, t2.run
FROM ( table_name AS t1
INNER JOIN
table_name AS t2
ON t1.run = (SELECT MAX(run) FROM table_name where run < t2.run)
and t1.item_id = t2.item_id)
This is assuming you want the diff between a record and the record with the previous run