I Have a table called
CountryVisits
Visitor Date
4 20/12/12 14:27:7
5 20/12/12 15:45:11
6 20/12/12 16:36:51
12 21/12/12 11:17:2
145 21/12/12 12:37:57
445 21/12/12 13:35:15
435 21/12/12 14:34:35
I want to have a count of all visitors per Date not time :
So i expect :
VisitorCnt Date
3 20/12/12
4 21/12/12
I tried :
SELECT COUNT(DATE(Date)), DATE(Date) from CountryVisits GROUP BY Date ;
But i don't have the desired results.
When you write GROUP BY Date it's grouping by the Date column in the table, which includes times. If you want to group by just the day, you need to assign an alias and group by that:
SELECT COUNT(*), DATE(Date) AS VisitedOn
FROM CountryVisits
GROUP BY VisitedOn
Related
I have the following sample data:
Date
amount
type
grouping_value
2021-03-01
10
1
2021-03-04
15
3
2021-03-16
32
4
2021-03-16
1
2
a
2021-03-16
4
3
a
2021-03-16
48
4
a
and my expected result is:
Date
amount
type
grouping_value
2021-03-01
10
1
2021-03-04
15
3
2021-03-16
32
4
2021-03-16
53
9
a
with the following query:
SELECT date, SUM(amount), type, grouping_value
FROM table
GROUP BY date, grouping_value
The query is still untested but should work for all requirements but one.
As you can see from the results, the query should group by date and grouping_value. So the first three rows of my input data will remain as they are, the other three will be grouped in one movement. What I want to do is: when some rows are grouped show 9 as type instead of one of the types that are grouped. If a row is not grouped then show the original type.
The condition to group is that grouping_value is not blank.
How can I tell mysql to replace if it is grouping only?
Use COUNT(*) to tell if the row is in a group.
SELECT date, SUM(amount), IF(COUNT(*) > 1, 9, MAX(type)) AS type, grouping_value
FROM table
GROUP BY date, grouping_value
I have two tables
Table_1 : Routes_Day_plan
Date Status_Id
------------------------
2019-06-09 1
2019-06-10 2
2019-06-09 2
2019-06-11 3
2019-06-14 4
2019-06-14 6
2019-06-15 8
Table_2 : Codes
id code
-------
1 Leave
2 Half_leave
3 Holiday
4 Work
5 Full_Hours
Now my task is to count week wise from table 1 where code (from second table) = Leave,Half_leave,work and than also show the sum , and where date not found show 0 , i write this query it's return data but not empty dates can someone please help ,
My Query:
select COUNT(*) as available, DATE(date)
from Table_1
where status_id in (
select id from codes
where code in ('Leave','Half_leave','work'))
AND DATE(date) >= DATE('2019-06-09') AND DATE(date) <= DATE('2019-06-16')
group by date
UNION ALL
SELECT COUNT(date), 'SUM' date
FROM Table_1
where status_id in (
select id from codes
where code in ('Leave','Half_leave','work'))
AND DATE(date) >= DATE('2019-06-09') AND DATE(date) <= DATE('2019-06-16')
Result Something Like ,
available Dates
------------------------
5 2019-06-09
2 2019-06-10
3 2019-06-11
3 2019-06-12
2 2019-06-14
2 2019-06-15
17 SUM
I want like this
available Dates
------------------------
5 2019-06-09
2 2019-06-10
3 2019-06-11
3 2019-06-12
0 2019-06-13
2 2019-06-14
2 2019-06-15
17 SUM
Your best bet here would be to have a Date Dimension/Lookup table which contains pre-populated dates for the entire year. By joining your record table to this lookup, you essentially allocate your data to each date that actually exist (ex. 2019-06-13) and if your data is not found in the lookup, you will find a null in that field.
The Count function will count a null as a 0. Just make sure you group on the date field from your lookup table and not from your record table.
Make a table, a date dimension that contains all the dates value, from beginning to end. Like this:
Set EndDate = '2099-01-01';
Set RunDate = '1900-01-01';
WHILE RunDate <= EndDate DO
insert into dim_date
(`DATE`)
select
RunDate as DATE
;
Set RunDate = ADDDATE(RunDate,1);
END WHILE;
Create temporary table with dim_date left join Routes_Day_plan and set Status as 0 maybe for record that dont match. Use this temporary table then instead of Routes_Day_plan in your queries.
Hi I'm trying to count order records with users who made more than one order in a month like 2018-01-01 to 2018-02-01.
Order table
id user_id date
1 12 2017-01-02 <- first order(no count)
2 23 2018-01-03 <- second order(count)
3 12 2018-01-04 <- second order(count)
4 12 2018-01-08 <- third order(count)
5 23 2017-11-02 <- first order(no count)
6 11 2018-01-01 <- first order(no count)
....
User table
id
11
12
23
....
Output
date count(*)
2018-01-01 3
I think I need to get order records first and find order records again with certain user_id. But I'm stuck
Is there a way to accomplish this task?
Thanks
How about this:
select count(*) from
(select user_id, count(*)
from `Order`
where date >= '2018-01-01' and date < '2018-02-01'
group by user_id
having count(*) > 1) users_w_multiple_orders;
The having command is how you filter results from an aggregation like sum. After you have that, you can count the results from that query.
I have a table, which looks like the following:
Table users:
id points date
1 100 2014-07-01
2 500 2014-07-02
3 200 2014-07-01
4 100 2014-07-03
5 100 2014-07-01
6 400 2014-07-02
7 800 2014-07-02
8 200 2014-07-02
Now, how is it possible to select each unique date and count the sum of the points on those days?
So I's need a result something like this:
points date
400 2014-07-01
1900 2014-07-02
100 2014-07-03
SELECT SUM(`points`) AS points, `date`
FROM users
GROUP BY `date`
Try this:
SELECT SUM(points) points, date
FROM users
GROUP BY date
ORDER BY date ASC
This group-and-aggregate operation is a standard query pattern and can be solved following these steps1.
Use a GROUP BY date to aggregate the dates into groups, by date.
Select SUM(points) to tally the points in each aggregated [date] group.
Select the date column, which represents each group, to include it in the results.
Finally, apply ORDER BY date to ensure the results are ordered.
1 It is good etiquette - and results in better answers/discussion - when discussing attempted solutions (ie queries) in questions, and why they didn't work [correctly].
I have a cron script that writes the total number of active users to a table every day. I'm trying to now generate a simple report that would show the "high water mark" for each month. Because some accounts expire during the month it's possible the highest number may NOT be at the end of the month.
Here's a sample of my table structure
tblUserLog
-----------
record_id INT(11) // PRIMARY KEY
run_date DATE // DATE RUN
ttl_count INT(11) // TOTAL FOR DAY
Sample data:
record_id run_date ttl_count
1 2013-06-01 500
2 2013-06-10 510
3 2013-06-20 520
4 2013-06-30 515
5 2013-07-01 525
6 2013-07-10 530
7 2013-07-20 540
8 2013-07-31 550
9 2013-08-01 560
What I would like returned is:
record_id run_date ttl_count
3 2013-06-20 520
8 2013-07-31 550
9 2013-08-01 560
I've tried two queries that are close...
// This will give me the total for the first of the month
SELECT s.record_id, s.run_date, s.ttl_count
FROM tblStatsIndividual s
JOIN (
SELECT record_id
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
HAVING MAX(ttl_count)
) s2
ON s2.record_id = s.record_id
ORDER BY run_date DESC
This returns the total for the first of each month, along with the record_id and correct date for the total.
Tried this...
SELECT record_id,max(run_date), max(ttl)
FROM (
SELECT record_id,run_date, max(ttl_count) AS ttl
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
) a
GROUP BY DATE_FORMAT(run_date, '%Y %m')
ORDER BY run_date DESC
This one appears to get the correct "high water mark" but it's not returning the record_id, or the run_date for the row that IS the high water mark.
How do you get the record_id and the run_date for the highest total?
Something like
Select detail.Record_ID, detail.Run_Date, detail.ttl_Count
From tblStatsIndividual detail
Inner Join
(Select Year(run_date) as Year, Month(Run_date) as Month, Max(ttl_count) as ttl
From tblStatsIndividual
Group By Year(run_date), Month(Run_date)) maximums
On maximums.Year = Year(detail.Run_date) and maximums.Month = Month(detail.Run_date)
and maximums.ttl = detail.ttl_count
Should do it. NB based on your requirement if you had two records in the same month with the same (and highest in the month) ttl_count, they would both be returned.
Based on the help from #Tony Hopkinson, This query gets me the info. The one caveat is it shows the ID and date for the first occurrence of the MAX total, so if the total is the same three days in a row on a month, the first day's ID is returned. For my purpose, the last ID would be more ideal, but I can live with this:
SELECT s.Record_ID, s.Run_Date, s.ttl_Count
FROM tblStatsIndividual s
INNER JOIN (
SELECT YEAR(run_date) AS yr, MONTH(run_date) AS mon, MAX(ttl_count) AS ttl
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
) maximums
ON maximums.yr = YEAR(s.run_date)
AND maximums.mon = MONTH(s.run_date)
AND maximums.ttl = s.ttl_Count
GROUP BY ttl_count
ORDER BY run_date DESC