Hi I am not perfect in mysql queries i tried this code to get previous date record count
code snippet
SELECT id, date, COUNT(IF(date<= date-INTERVAL 1 DAY, id, NULL))
FROM table_name
GROUP BY date
this query gives me prev day value is 0.
help me out to get previous day count of id
this is what i need
date count
-------------------
2014-01-01 0
2014-01-02 13
2014-01-03 55
I suspect you rather look for something like this, to count id's from yesterday:
select
date(dt),
count(id)
from
table_name
where
date(dt) < date(now())
group by
date(dt)
Related
My table is like this:
root_tstamp
userId
2022-01-26T00:13:24.725+00:00
d2212
2022-01-26T00:13:24.669+00:00
ad323
2022-01-26T00:13:24.629+00:00
adfae
2022-01-26T00:13:24.573+00:00
adfa3
2022-01-26T00:13:24.552+00:00
adfef
...
...
2021-01-26T00:12:24.725+00:00
d2212
2021-01-26T00:15:24.669+00:00
daddfe
2021-01-26T00:14:24.629+00:00
adfda
2021-01-26T00:12:24.573+00:00
466eff
2021-01-26T00:12:24.552+00:00
adfafe
I want to get the number of users in the current year and in previous year like below using SQL.
Date Users previous_year
2022-01-01 10 5
2022-01-02 20 15
The code is written as follows.
select CAST(root_tstamp as DATE) as Date,
count(DISTINCT userid) as users,
count(Distinct case when CAST(root_tstamp as DATE) = dateadd(MONTH,-12,CAST(root_tstamp as DATE)) then userid end) as previous_year
FROM table1
But it returns 0 for previous_year values.
How can I fix that?
Possible solution for SQL Server:
WITH cte AS ( SELECT 2022 [year]
UNION ALL
SELECT 2021 )
SELECT cte.[year],
COUNT(DISTINCT test.userId) current_users_amount,
COUNT(DISTINCT CASE WHEN YEAR(test.root_tstamp) < cte.[year]
THEN test.userId
END) previous_users_amount
FROM test
JOIN cte ON YEAR(test.root_tstamp) <= cte.[year]
GROUP BY cte.[year]
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=88b78aad9acd965bdbac4c85a0b81927
This query (for MySql) returns unique number of userids where the root_timestamp is in the current year, by day, and the number of unique userids for the same day last year. If there is no record for a day in the current year nothing will be displayed for that day. If there are rows for the current year, but no rows for the same day last year, then NULL will be shown for that lastyear column.
SELECT cast(ty.root_tstamp as date) as Dte,
COUNT(DISTINCT ty.userId) as users_this_day,
count(distinct lysd.userid) as users_sameday_lastyear
FROM test ty
left join
test lysd
on cast(lysd.root_tstamp as date)=date_add(cast(ty.root_tstamp as date), interval -1 year)
WHERE YEAR(ty.root_tstamp) = year(current_date())
GROUP BY Dte
If you wish to show output rows for calendar days even if there are no rows in current year and/or last year, then you also need a calendar table to be introduced (let's hope that it is not what you need)
I have requirement to get count of distinct department with total employees in period of month but unfortunately query is not working and throwing error
My table
Department_id emloyee_id date_time
1 1 2020-02-01
1 2 2020-02-04
3 7 2020-02-06
1 4 2020-02-07
expected output
total department=2
total employee of all department=4
But all should work based on last one record , I am getting sql syntax error
Query:
SELECT COUNT(DISTINCT department_id) x, COUNT(*) y
FROM department
WHERE date_time>=DATE_FORMAT(NOW() ,'%Y-%m-01')
AND date_time<DATE(NOW()+INTERVAL 1 DAY and status='1'
You can combine them within only one query :
SELECT COUNT(DISTINCT Department_id), COUNT(DISTINCT employee_id)
FROM department
WHERE date_time >= NOW() - INTERVAL 1 MONTH
AND status = '1';
counting both distinctly.
Update : If you mean to stay within the current month, then also
AND date_time>=DATE_FORMAT(NOW() ,'%Y-%m-01')
might be added to this query as in your original one.
It seems you should use month instead of day and are missing a bracket after month
SELECT COUNT(DISTINCT department_id) AS departments,
COUNT(*) AS employees
FROM department
WHERE date_time>=DATE_FORMAT(NOW() ,'%Y-%m-01')
AND date_time < DATE(NOW()+INTERVAL 1 MONTH)
AND status = '1';
Currently, I have a table with 3 columns with id, clicks and dates. Every id has an entry with clicks for each day of a month with the format like '20180420' in the date column, let's say for 20th April 2018.
SELECT
id,
month(date),
COUNT(id)
FROM mydb
WHERE (date >= 20180101 AND date < 20180424) clicks > 0
GROUP BY id, MONTH(date);
I run this query to get if I have more than one clicks each month for this id and I want to do something like that to get the count of how many months every id has more than one clicks, but I don't get the correct result.
Am I missing something basic?
P.S: I am using MySQL 5.6
I guess the date is stored as a varchar, if so then convert string into date.Try this:
SELECT MONTH(STR_TO_DATE(A.date,'%y%m%d')) MONTH, COUNT(*) NUMBER_OF_CLICKS
FROM mydb A
WHERE STR_TO_DATE(A.date,'%y%m%d') BETWEEN STR_TO_DATE('20180101','%y%m%d') AND CURRENT_DATE
GROUP BY 1
ORDER BY 1 DESC;
Lets say i have a table in a MySQL db with the following schema.
+-----------+--------+
| timestamp | string |
+-----------+--------+
And in this table are many entries per day for a month.
Now i want to count the entries between 6am and 10am per day for every day in the table and group them by this timeframe for each day.
The response should be something like this
+------+------------------------------+
| date | count in the given timeframe |
+------+------------------------------+
My Problem is how to select a recurring time frame. I know how to count the entries and how to group them.
I had success to do that on an intervall. But the intervall is not the same for every day.
SELECT count(timestamp) as count,
ROUND(UNIX_TIMESTAMP(timestamp)/(60 * 60 * ", hours, ")) AS timekey
FROM event_data,
GROUP BY timekey
Assuming that you are working on MySQL, you can use the following query:
SELECT
date(timestamp1) AS entrydate
, COUNT(*) FROM mytable
WHERE
EXTRACT(HOUR FROM TIME(timestamp1)) BETWEEN 6 AND 10
GROUP BY
entrydate
Where
timestamp1 --> timestamp in your example
entrydate --> date in your desired output column
mytable --> the table which holds the data
And, please provide some sample entries next time and try to be a bit specific.
psuedo code will be like
select timestamp ,
sum(case when hours of timestamp between 6am and 10am then 1 else 0 end) timeframe_count
from yourtable
group by timestamp
Hi i am trying to get the count of records per day which i can do, but i also want the date to be show, for example,
Result
Date | Count
26/01/2015 20
25/01/2015 | 413
Here is an example of my data.
I would think this would work. Replace 'yourTable' with your table name
SELECT Date, COUNT(*) FROM yourTable GROUP BY Date;
Get the total count and group them by date.
SELECT `date`, COUNT(*) as Total
FROM `table`
GROUP BY `date`
ORDER BY `date`;