Thereis a good answer to the main question here: MySQL: Count the distinct rows per day
I need it with the days with values also included but the query
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
WHERE totalCOunt < 1
GROUP BY DATE(timestamp)
gives an error ( #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE totalCOunt < 1
LIMIT 0, 25' at line 3 ). Where did I go wrong?
Sample data here: http://sqlfiddle.com/#!2/11aa6/146
It is throwing Unknown column 'totalCOunt' in 'where clause' error:
Please try the below query(replaced WHERE clause with HAVING clause):
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
GROUP BY DATE(timestamp)
HAVING totalCOunt<1
You have a few errors:
Date should be escaped, as it is a keyword in MySQL.
The syntax is wrong. You forgot AS.
So the corrected one is:
SELECT CAST(`timestamp` AS Date) AS `Date`, COUNT(DISTINCT(`ipNum`)) AS totalCOunt
FROM `tableName`
GROUP BY CAST(`timestamp` AS Date)
Fiddle: http://sqlfiddle.com/#!2/809838/8
You can't use an alias in the WHERE clause
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
WHERE totalCOunt < 1
GROUP BY DATE(timestamp)
Use HAVING instead:
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
GROUP BY DATE(timestamp)
HAVING totalCOunt =1
Related
How to do this subquery I started doing it with IN but I was investigating and changed it to INNER JOIN but it still makes me wrong.
SELECT DISTINCT
TIMESTAMPDIFF (YEAR, date_nac, CURDATE ()) AS age
FROM patient
WHERE date_nac
INNER JOIN (
select count(patient id) as Quantity
from patient
group by age
order by Quantity desc limit 6
);
Show me this error:
Error Code: 1235. This version of MariaDB doesn't yet support 'LIMIT &
IN/ALL/ANY/SOME subquery'
Assuming that your syntax for calculating age is correct, you can use something like this:
Select age, count(age)
From
(SELECT distinct patient_id, TIMESTAMPDIFF(YEAR, date_nac, CURDATE ()) AS age
FROM patient
) a
Group by age;
You can just use aggregation:
select
timestampdiff(year, date_nac, current_date) age,
count(distinct patient_id) no_patients
from patient
group by age
order by no_patients desc
If there are no duplicate patient_ids in the patient table (as it should be), you can use count(*) instead of the more expensive count(distinct patient_id).
I am speculating that you want the top 6 most populated ages only: if so, you can just add limit 6 t the end of the query.
Whats wrong with my query:
SELECT minDate, deviceType, COUNT(*)
FROM (SELECT visitorId, deviceType,
MIN(sessionDate) as minDate
FROM sessions
GROUP BY visitorId)
WHERE minDate > DATE_SUB(NOW(), INTERVAL 14 DAY)
GROUP BY minDate, deviceId
I've got this message:
Query Error: Error: ER_DERIVED_MUST_HAVE_ALIAS: Every derived table must have its own alias
The error message is clear enough. You must alias the derived table that is generated by your sub-select. So, give it an alias.
Another issue is that, in the subquery, non-aggregated column deviceType should be included in the GROUP BY clause. This change might, or might not produce the results that you do expect: if it doesn't, then you would need to provide sample data, expected results and an explanation of what you are trying to accomplish so one can help fixing the query.
SELECT minDate, deviceType, COUNT(*)
FROM (
SELECT visitorId, deviceType, MIN(sessionDate) as minDate
FROM sessions
GROUP BY visitorId, deviceType -- all non-aggregated columns in the GROUP BY clause
) t -- alias here
WHERE minDate > DATE_SUB(NOW(), INTERVAL 14 DAY)
GROUP BY minDate, deviceId
I'm tyring to use a where clause with a subquery inside a 'in'statement.
Thisis my query:
select *
from trading.historical_prices
where
date in (
(select date
from trading.historical_prices
group by date
order by date desc
limit 1)
union
(
select date
from trading.historical_prices
group by date
order by date desc
limit 7,1
)
)
limit 100
But I'm getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union
(
select date
from trading.historical_prices
group by date
orde' at line 13
The union query works fine when it's ran alone.
How can i fix this?
According to this answer, you don't parenthesis.
So your query would look like so :
SELECT *
FROM trading.historical_prices
WHERE date IN
(
SELECT date
FROM trading.historical_prices
GROUP BY date
ORDER BY date DESC limit 1
UNION
SELECT date
FROM trading.historical_prices
GROUP BY date
ORDER BY date DESC limit 7,
1) limit 100
I have the following problem:
I have this query:
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) as totalCount
FROM tableName
GROUP BY DATE(timestamp)
I get a result like this:
Date totalCount
1.1. 7
2.1. 19
I need just the sum of all totalCount values. Is this possible with MySql?
I googled a lot (Link1, Link2, Link3) but nothing really answers my question.
I created a fiddle to illustrate my case: http://sqlfiddle.com/#!2/e4fd9/22
The trick is to use a derived table like this:
SELECT SUM(amount)
FROM (
SELECT COUNT(DISTINCT ipNum) as amount, DATE(timestamp)
FROM tableName
GROUP BY DATE(timestamp)
) x
I have a working mysql query, but I can not get it work with postgres. This is the query (I already changed date format to to_char
SELECT country as grouper, date(users.created_at) as date,
to_char(users.created_at, '%Y-%m') as date_group,
count(id) as total_count
FROM "users"
WHERE (users.created_at >= '2011-12-01')
AND (users.created_at <= '2014-02-11')
GROUP BY grouper, date_group
ORDER BY date ASC
I am getting the error:
PG::Error: ERROR: column "users.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT country as grouper, date(users.created_at) as date, t...
Thank for your help.
SELECT country as grouper, date(MIN(users.created_at)) as date,
to_char(MIN(users.created_at), '%Y-%m') as date_group,
count(id) as total_count
FROM "users"
HAVING (users.created_at >= '2011-12-01')
AND (users.created_at <= '2014-02-11')
GROUP BY grouper, date_group
ORDER BY date ASC
MySQL is not very strict. In standard conform SQL all column values have to use an aggrate function (SUM, COUNT, MAX, MIN) on non-grouping fields - when using GROUP BY.
Honestly said, I am not entirely sure about data_group in the GROUP BY; can it be dropped?
Also note that I have switched WHERE with a HAVING.
You should use every selected column in GROUP BY section.
SELECT country as grouper, to_char(created_at, '%Y-%u') as date_group, count(id) as total_count
FROM "users"
WHERE created_at >= '2013-10-01'
AND created_at <= '2014-02-11'
GROUP BY grouper, date_group
ORDER BY date_group ASC