MySQL - max( ) row values of subquery - mysql

I have a table in which the rainfall is recorded every 15 minutes, so the total rainfall for a day is the sum of 'rain' in 95 records. The following query correctly returns a list of dates and the rainfall for that day.
select thedate as tdate,sum(rain) as dailyrain
from weatherdata
group by year(thedate), month(thedate), day(thedate)
(thedate stored as datetime, rain stored as integer )
I now want to make this a sub query and return the date on which there was maximum rainfall.
My query below correctly returns the maximum rain amount, but not the correct date linked with that rainfall.
select maxdate,max(dailyrain) from (select thedate as maxdate,sum(rain) as dailyrain
from weatherdata
group by year(thedate), month(thedate), day(thedate)) as maxrain
I think I probably have to use a JOIN. I know many similar questions have been answered here but I can't quite get the syntax right to make this work. Any help would be much appreciated.

SELECT DATE(thedate) as tdate, sum(rain) as dailyrain
FROM weatherdata
GROUP BY DATE(thedate)
ORDER BY sum(rain) DESC, DATE(thedate) DESC
LIMIT 1 ;
or:
SELECT DATE(thedate) as tdate, sum(rain) as dailyrain
FROM weatherdata
GROUP BY DATE(thedate)
ORDER BY dailyrain DESC, tdate DESC
LIMIT 1 ;

To get all dates having the maximal rainfall:
SELECT DATE(thedate), SUM(rain)
FROM weatherdata
GROUP BY DATE(thedate)
HAVING SUM(rain) = (
SELECT SUM(rain)
FROM weatherdata
GROUP BY DATE(thedate)
ORDER BY SUM(rain) DESC
LIMIT 1
)

please try this
select tdate,dailyrain from
(select date(thedate) as tdate,sum(rain) as dailyrain
from weatherdata
group by date(thedate))r
WHERE dailyrain =
(select max(dailyrain) as maxrain from
(select date(thedate) as tdate,sum(rain) as dailyrain
from weatherdata
group by date(thedate)
)m
)
sqlFiddle

Related

Mysql query - GROUP BY and show the highest COUNT(*)

I'm doing a small project and I'm trying to get the restaurant with the most votes each week to be displayed once.
This is my query:
SELECT votedRestaurant,
week,
COUNT(*)
FROM mylunch.votes
GROUP BY votedRestaurant,
week
ORDER BY week DESC;
This gets me the following result:
I would only like to have the one with the highest COUNT(*) displayed per week.
Thanks for any help.
you can try use LIMIT and ordering by count, for example
SELECT votedRestaurant,
week,
COUNT(*) AS tcount
FROM mylunch.votes
GROUP BY votedRestaurant,
week
ORDER BY tcount DESC
LIMIT 1;
Also, you can use subquery, so says the documentation.
Using Mysql 8 you can make use of window functions
WITH
cte AS (SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC, votedRestaurant)
SELECT *
FROM (
SELECT *,
row_number() over (PARTITION BY WEEK ORDER BY total DESC) AS rn
FROM
cte
) t
WHERE rn = 1
Demo
Another way would be by using string fuctions
SELECT t.week, SUBSTRING_INDEX(GROUP_CONCAT(t.votedRestaurant ORDER BY t.total DESC),',',1) votedRestaurant, MAX(t.total)
FROM(
SELECT votedRestaurant, WEEK, COUNT(*) total
FROM votes
GROUP BY votedRestaurant,WEEK
ORDER BY WEEK DESC
) t
GROUP BY t.week
ORDER BY t.week DESC
Demo

Select last n record within group by year month

I have a table where I need to get the last 12 records which is grouped by year() month() ASC. I try to use the query below but the result is not as expected.
SELECT * FROM
(
SELECT
id,
tanggal,
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY id ASC
the query result is as below
My expected result is sort by bulan column order by year(), month() as follows
Bulan jumlah
Mar-2018 26600000
Oct-2017 1000000
Sept-2017 4500000
and so on....
EXTRACT(YEAR FROM bulan) as year
SELECT EXTRACT(YEAR FROM tanggal) as year , EXTRACT(MONTH FROM tanggal) as month, id FROM table_name group by year order by month
you can get year same like you can get month after that put group by and order i hope it will help you
This works for my situation
SELECT * FROM
(
SELECT
id,
tanggal,
month(tanggal),
year(tanggal),
date_format(tanggal,'%b-%Y') as bulan,
sum(sisa_pokok) as jumlah
FROM transaksi_detail
GROUP BY date_format(tanggal,'%b-%Y')
ORDER BY id DESC LIMIT 12
) sub
ORDER BY year(tanggal), month(tanggal) ASC

Calculate Sum grouping by day

SELECT *,
DATE(date) AS post_day
FROM notes
WHERE MONTH(date) = '08'
AND userid = '2'
AND YEAR(date) = '2016'
ORDER BY post_day DESC,
timestamp ASC
In this query I'm grouping my posts by day.
What I'm struggling with is calculating the total word count of all notes for each day. There is a word count column which contains the word count for each post. Is it possible to calculate this sum in the same query or does it need to be made separately?
By table columns:
NoteID UserID Date Note WordCount
SELECT date, SUM(wordCount) AS monthWordCount
FROM Notes
WHERE userID = 2
AND MONTH(date) = '08'
AND YEAR(date) = '2016'
GROUP BY userID, date
Check out this demo using the above code.
If you want to return the notes as well you can do a subquery like below:
SELECT noteID, userID, date, note, wordCount,
(SELECT SUM(wordCount)
FROM Notes
WHERE userID = a.userID
AND date = a.date
GROUP BY userID) AS dayTotalWordCount
FROM Notes a
WHERE a.userID = 102
AND MONTH(date) = '08'
AND YEAR(date) = '2016'
Here's a demo using the above code.
First, don't use select * with a group by query. select * just doesn't make sense with aggregation . . . you need to apply aggregation functions.
I assume that you want something like this:
SELECT DATE(date) as post_day, SUM(WordCount)
FROM notes
WHERE MONTH(date)= '08' AND userid = '2' AND YEAR(date)= '2016'
GROUP BY DATE(date)
ORDER BY post_day DESC, timestamp ASC

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

Most common week days query

Have this query:
SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY hr
ORDER BY cnt DESC
DATE is a DATETIME field and the above query shows me the most common hours in the date field.
I'm trying to improve it but not sure how to do it, I want to break it down by week days and within each week day to most common hours.
Try this query -
SELECT
DAYOFWEEK(DATE) AS wd,
HOUR(DATE) AS hr,
COUNT(*) AS cnt
FROM
users
GROUP BY
wd, hr
ORDER BY
cnt DESC
SELECT
DATE_FORMAT(DATE, '%a') AS wd, -- or DATE_FORMAT(DATE, '%W')
HOUR(DATE) AS hr,
COUNT(*) AS cnt
FROM
users
GROUP BY
wd, hr
ORDER BY
cnt DESC
Let me know if below works:
SELECT dayname(DATE) as week_day,HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY week_day, hr ORDER BY cnt DESC