Selecting most recent MySQL rows by MAX(time) WHERE time <= x - mysql

I am selecting the most recent entries of a MySQL table with:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
ORDER BY most_recent DESC
My problem is, if I want to limit the maximum time with:
WHERE time <= nnn
The query does not work anymore. Is there a solution for with without a subquery?
Thanks in advance!

you can do it with subquery :
select t.userID, max(t.time)
from
(
select userID, time
from tableName
where time <= nnn
) t
group by t.userID
or simply :
select userID, max(time)
from tableName
where time <= nnn
group by userID

Use the HAVING clause like so:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
HAVING MAX(time) <= nnn
ORDER BY most_recent DESC

Related

MySQL Use where for count of a group

I've wrote this query:
SELECT COUNT(ProductID) AS ProNumber, userID
FROM orderLog WHERE time >= CURDATE()
GROUP BY userID ORDER BY ProNumber DESC
And i want to do that:
SELECT COUNT(ProductID) AS ProNumber, userID
FROM orderLog WHERE time >= CURDATE() AND ProNumber > 1000
GROUP BY userID ORDER BY ProNumber DESC
But it doesn't work.
why? how can i fix this query?
Because your alias ProNumber is not yet known by MySQL when the WHERE clause is executed. You should not use a column alias in the WHERE clause
If you want to keep only the the records with COUNT(*) > 1000, you should use the HAVING clause that filters the records after a GROUP BY:
SELECT COUNT(ProductID) AS ProNumber, userID
FROM orderLog
WHERE time >= CURDATE()
GROUP BY userID
HAVING COUNT(ProductID) > 1000
ORDER BY ProNumber DESC
If that's not what you want, add explanations because it is not clear
let's try this :-)
SELECT COUNT(ProductID) AS ProNumber, userID
FROM orderLog WHERE time >= CURDATE() AND ProductID> 1000
GROUP BY userID ORDER BY ProductID DESC

Reverse order for GROUP BY in MySQL

I need to select first value for every hour from my db. But I don't know how to reverse order on GROUP BY statement.
How can i rewrite my query (now it selects last value in hour)?
SELECT HOUR(`time`) as hour, mytable.*
FROM mytable
WHERE DATE(`time`) ="2015-09-12" GROUP BY HOUR(`time`) ORDER BY `time` ASC;
This query gave me expected result:
SELECT HOUR(`time`) as hour, sortedTable.* FROM
(SELECT electrolysis.* FROM electrolysis
WHERE DATE(`time`)='2015-09-12' ORDER BY `time`) as sortedTable
GROUP BY HOUR(`time`);
You can just select the MIN HOUR in sub query , try using the query:
SELECT * from mytable WHERE `time` IN (
SELECT MIN(HOUR(`time`)) as `hour`
FROM mytable
WHERE DATE(`time`) ="2015-09-12"
GROUP BY HOUR(`time`) ) ORDER BY `time` ASC;
You can do something like this:-
SELECT sub0.min_time,
mytable.*
FROM mytable
INNER JOIN
(
SELECT MIN(`time`) AS min_time
FROM mytable
GROUP BY HOUR(`time`)
) sub0
ON mytable.`time` = sub0.min_time
WHERE DATE(`time`) ="2015-09-12"
ORDER BY `time` ASC
This is using a sub query to get the smallest time in each hour. This is then joined back against your main table on this min time to get the record that has this time.
Note that there is a potential problem here if there are multiple records that share the same time as the smallest one for an hour. There are ways around this, but that will depend on your data (eg, if you have a unique id field which is always ascending with time then you could select the min id for each hour and join based on that)
You can use below query, which is more optimized just make sure that time field should be indexed.
SELECT HOUR(m.time), m.*
FROM mytable AS m
JOIN
(
SELECT MIN(`time`) AS tm
FROM mytable
WHERE `time` >= '2015-09-12 00:00:00' AND `time` <= '2015-09-12 23:59:59'
GROUP BY HOUR(`time`)
) AS a ON m.time=a.tm
GROUP BY HOUR(m.time)
ORDER BY m.time;

Mysql complex query, group by with limit

I've the following table
store_visits: (store_id, city_id, date, visits, ...)
I want to select the maximum 5 stores ordered by visits.
SELECT X.*
FROM (
SELECT
store_id, SUM(visits) as sum_visits FROM store_visits
WHERE
(date <= '2014-06-28' AND date >= '2014-06-27')
AND
store_visits.city_id = 2
GROUP BY
store_id
ORDER BY
sum_visits desc
) X
LIMIT 5
I was wondering if there's a way to enhance the query to eleminate the temporary table and filesort.
Try this:
SELECT store_id, SUM(visits) AS sum_visits
FROM store_visits sv
WHERE sv.date <= '2014-06-28' AND sv.date >= '2014-06-27' AND sv.city_id = 2
GROUP BY store_id
ORDER BY sum_visits DESC LIMIT 5

how to select with group by order by and desc?

i want to select the data in a table such that it should group it by userid except one value in that column and order by date time and desc.
The problem i am getting is the grouped items are not ordering by date and time and in desc manner.
I mean the grouped item is showing earlier row.
How can i do that.
This is what i have done.
SELECT * FROM `tbljobs`
GROUP BY user_id
UNION ALL
SELECT * FROM tbljobs
WHERE user_id = '1'
ORDER BY date_time DESC
LIMIT 20"
where '1' is should not be grouped.
Your ORDER BY is only executed on the second statement. You have to use braces to order the whole results:
(SELECT *
FROM `tbljobs`
GROUP BY user_id)
UNION ALL
(SELECT *
FROM tbljobs
WHERE user_id = '1')
ORDER BY date_time DESC
Thank you friends for your suggestions. Finally I created the solution by myself after a lot of effort.
(
SELECT *
FROM (
SELECT *
FROM tbljobs
ORDER BY date_time desc
) AS A
WHERE user_id <> '1' group by user_id
)
UNION ALL
(
SELECT *
FROM tbljobs
WHERE user_id=1
)
ORDER BY date_time DESC

Most common hour query?

I have this table:
ID(INT) DATE(DATETIME)
Under the DATE column there are a lot of different dates, and I want to figure out the most common hour between all the rows of the table, regardless of the day.
How can I do that with a MySQL query?
SELECT HOUR(date) AS hr, COUNT(*) AS cnt
FROM yourtable
GROUP BY hr
ORDER BY cnt DESC
LIMIT 1
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
Try this -
SELECT HOUR(`DATE`) AS `hour`, COUNT(*)
FROM `table`
GROUP BY `hour`
You could do a query like:
SELECT COUNT(daterow) AS occurrences FROM table GROUP BY daterow ORDER BY occurrences DESC LIMIT 1;
SELECT COUNT( id ) , HOUR( date )
FROM test
GROUP BY HOUR( date )
ORDER BY COUNT( id ) DESC
LIMIT 1