L
L
Lulzsec2015-12-12 21:37:52
PHP
Lulzsec, 2015-12-12 21:37:52

Counting the same values ​​for some period?

There is a working request

SELECT text,count(*) as 'A' 
FROM all_queries
group by text
having text in ('Привет','Хэллоу','Здравствуйте')
ORDER BY A desc

Everything is OK until the moment when I add a selection of unique values ​​​​by periods (here for a month):
SELECT text,dates,count(*) as 'A' 
FROM all_queries
group by text
having dates > LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND dates < DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 1 DAY)
AND text in ('Привет','Хэллоу','Здравствуйте')
ORDER BY A desc

The request works, but does not produce any values. For example, if you change the first (by the date it entered the database) row from "Hello" in the database, where the date is 12/12/2015 to the date 03/12/2014, then the selection for the current month will not contain the value "Hi", even if it is in the database (for the current month).
How to make it so that for a certain period the number of unique values ​​\u200b\u200bis displayed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Vitrenko, 2015-12-12
@Lulzsec

If there is a GROUP BY in the query, then only the names of those attributes that are specified in the GROUP BY or aggregate functions can be specified after SELECT.

SELECT text, count(*) as 'A' 
FROM all_queries
WHERE dates > LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND dates < DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 1 DAY)
group by text
HAVING text in ('Привет','Хэллоу','Здравствуйте')
ORDER BY A desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question