MySQL query to count occurrences on days of the year - mysql

I have a (large) table like this:
id venue city date time
1 Waldorf Hotel London 2020-01-01 07:00
2 Waldorf Hotel London 2020-01-02 07:00
3 Heathrow London 2020-01-02 14:00
4 Lennon Airport Liverpool 2020-01-02 16:00
5 Port of Liverpool Liverpool 2020-01-02 19:30
6 Port of Liverpool Liverpool 2020-01-03 07:00
7 Port of Liverpool Liverpool 2020-01-04 07:00
8 Port of Liverpool Liverpool 2020-01-05 07:00
9 Port of Liverpool Liverpool 2020-01-06 07:00
10 Manchester Airport Manchester 2020-01-06 12:40
11 Heathrow London 2020-01-06 14:40
So this person has been in Liverpool on 5 different days, in London on 3 different days, and in Manchester on 1 day only.
I am looking for a query that gets me this number of days that a certain city has been visited, resulting in this list:
Liverpool 5
London 3
Manchester 1
But now I'm stuck at this query that counts the occurrences per day and I can't get it right:
SELECT city, COUNT(city) AS value FROM visits WHERE YEAR(dt) = 2020 GROUP BY city, DATE(dt)

One way to achieve this is to select all the distinct city/date combinations in a subquery and then count the occurrence of each city:
SELECT city, COUNT(city) AS value
FROM (
SELECT DISTINCT city, DATE(dt)
FROM visits
WHERE YEAR(dt) = 2020
) v
GROUP BY city
Output:
city value
Liverpool 5
London 3
Manchester 1
Demo on SQLFiddle

Related

counting occurrences in an SSRS table

Working on SSRS. Have the following data set.
ID City Country
1 London England
2 Glasgow Scotland
3 Aberdeen Scotland
4 Swansea Wales
5 London England
6 Glasgow Scotland
7 Glasgow Scotland
8 Manchester England
Would like to have a table, grouped by the city and showing how many occurrences there have been for each city, see below?
City Country Total
Glasgow Scotland 3
London England 2
Manchester England 1
Swansea Wales 1
Aberdeen Scotland 1
How would i go about doing this please?
After creating a group of City use the following expression:
=COUNT(Fields!City.Value,"City")
Let me know if this helps.
It seems from your question that you don't want the detail in the report, only the summary data. If you do want detail, Alejandro Zuleta's answer will work. If you don't need the detail, you can do the grouping in your dataset:
select city, country, count(*) as total
from <yourtable>
group by city, country;

Grouping by and ordering by an aggregate column

I'm creating a SSRS report and have the following data set.
ID City Country
------------------------------
1 London England
2 Glasgow Scotland
3 Aberdeen Scotland
4 Swansea Wales
5 London England
6 Glasgow Scotland
7 Glasgow Scotland
8 Manchester England
I have a COUNT on the 'City' and grouped by City and Country. This is how I would like it to appear
City Country Total
--------------------------------------------
Glasgow Scotland 3
London England 2
Swansea Wales 1
Aberdeen Scotland 1
Manchester England 1
However, this is how it's appearing
City Country Total
--------------------------------------------
Swansea Wales 1
Glasgow Scotland 3
3
3
Aberdeen Scotland 1
Manchester England 1
London England 2
2
So I need to group by the aggregate total column and also order by that column but not sure how to do it. All help appreciated, thanks.
Have tried Sanjays answer below, unless I'm doing it wrong I'm getting this error.
here
we need to create city and country as group and delete detail section from row groups as like below
so table look like after above changes
and set Count(Fields!ID.Value,"CITY") sd sorting expression for first group city1 with order Z to A
There are multiple ways you could go about this. One option would be to handle the grouping in the query and simply display the results in SSRS. So your query would look something like this:
SELECT ID, City, Country, COUNT(*) as Total
FROM MyTable
GROUP BY ID, City, Country
Now in SSRS you can sort by the Total column since it's not being aggregated in the report.

Count occurrences in Access query

I am looking to see if its possible to run a count in an Access Query.
I have a list of data under TeamName
TeamName
----------
Liverpool
Liverpool
Liverpool
Liverpool
Liverpool
Manchester
Manchester
Newcastle
Newcastle
Stoke
Stoke
Stoke
I would you like a formula in Access that counts the number of occurrences in chronological order, so like below
TeamName Count
-------- -----
Liverpool 1
Liverpool 2
Liverpool 3
Liverpool 4
Liverpool 5
Manchester 1
Manchester 2
Newcastle 1
Newcastle 2
Stoke 1
Stoke 2
Stoke 3
I've added a screenshot to see if that helps
Since you don't have any column that can be used to order the team names with the same name you have to first add such a column:
ALTER Table YourTable ADD TeamID AUTOINCREMENT(1,1);
now your table has a numeric id that you can use in a query to rank items with the same name like so:
select
teamname,
(select count(*)
from yourtable as t2
where t1.teamid > t2.teamid
and t1.teamname = t2.teamname
) + 1 as rank
from yourtable t1;

MySQL self join to get past averages

I am trying to find an average of past records in the database based on a specific time frame (between 9 and 3 months ago) if there is no value recorded for a recent sale. the reason for this is recent sales on our website sometimes do not immediately collect commissions so i am needing to go back to historic records to find out what a commission rate estimate might be.
Commission rate is calculated as:
total_commission / gross_sales
It is only necessary to find out what an estimate would be if a recent sale has no "total_commission" recorded
here is what i have tried so far but i think this is wrong:
SELECT
cs.*
,SUM(cs2.gross_sales)
,SUM(cs2.total_commission)
FROM
(SELECT
sale_id
, date
, customer_code
, customer_country
, gross_sales
, total_commission
FROM customer_sale cs ) cs
LEFT JOIN customer_sale cs2
ON cs2.customer_code = cs.customer_code
AND cs2.customer_country = cs.customer_country
AND cs2.date > cs.date - interval 9 month
AND cs2.date < cs.date - interval 3 month
GROUP BY cs.sale_id
so that data would be structured as follows:
sale_id date customer_code customer_country gross_sales total_commission
1 2013-12-01 cust1 united states 10000 1500
2 2013-12-01 cust2 france 20000 3000
3 2013-12-01 cust3 united states 15000 2250
4 2013-12-01 cust4 france 14000 2100
5 2013-12-01 cust5 united states 13000 1950
6 2013-12-01 cust6 france 12000 1800
7 2014-04-02 cust1 united states 10000
8 2014-04-02 cust2 france 20000
9 2014-04-02 cust3 united states 15000
10 2014-04-02 cust4 france 14000
11 2014-04-02 cust5 united states 13000
12 2014-04-02 cust6 france 12000
so I would need to output results from the query similar to this: (based on sales between 9 and 3 months ago from the same customer_code in the same customer_country)
sale_id date customer_code customer_country gross_sales total_commission gross_sales_past total_commission_past
1 2013-12-01 cust1 united states 10000 1500
2 2013-12-01 cust2 france 20000 3000
3 2013-12-01 cust3 united states 15000 2250
4 2013-12-01 cust4 france 14000 2100
5 2013-12-01 cust5 united states 13000 1950
6 2013-12-01 cust6 france 12000 1800
7 2014-04-02 cust1 united states 10000 10000 1500
8 2014-04-02 cust2 france 20000 20000 3000
9 2014-04-02 cust3 united states 15000 15000 2250
10 2014-04-02 cust4 france 14000 14000 2100
11 2014-04-02 cust5 united states 13000 13000 1950
12 2014-04-02 cust6 france 12000 12000 1800
Your query looks mostly right, but I think your outer query needs to be GROUP BY cs.sale_id (assuming that sale_id is unique in the customer_sale table, and assuming that the date column is datatype DATE, DATETIME, or TIMESTAMP).
And I think you want to include a join predicate so that you match only match "past" rows to those rows where you don't have a total commission, e.g.
AND cs.total_commission IS NULL
And I don't think you really need an inline view.
Here's what I came up with:
SELECT cs.sale_id
, cs.date
, cs.customer_code
, cs.customer_country
, cs.gross_sales
, cs.total_commission
, SUM(ps.gross_sales) AS gross_sales_past
, SUM(ps.total_commission) AS total_commission_past
FROM customer_sale cs
LEFT
JOIN customer_sale ps
ON ps.customer_code = cs.customer_code
AND ps.customer_country = cs.customer_country
AND ps.date > cs.date - INTERVAL 9 MONTH
AND ps.date < cs.date - INTERVAL 3 MONTH
AND cs.total_commission IS NULL
GROUP
BY cs.sale_id
Appropriate indexes will likely improve performance of the query. Likely, the EXPLAIN output will show "Using temporary; Using filesort", and that can be expensive for large sets.
MySQL will likely be able to make use of a covering index for the JOIN:
... ON customer_sale (customer_code,customer_country,date,gross_sales,total_commission).

MySQL query for grouping data

can anyone please guide me with writing MySQL query for following scenario.
The data in table is like this,
Table Name: user
user_id country city age
----------------------------------------------
1 India Mumbai 22
2 India Mumbai 22
3 India Delhi 22
4 India Delhi 23
5 India Chennai 23
6 China Beijing 20
7 China Beijing 20
8 China Shanghai 20
9 USA New York 30
10 USA New York 30
11 USA New York 30
12 USA Los Angeles 31
13 USA Los Angeles 31
14 USA Los Angeles 40
I want result to be like this which is basically sum of all users in particular country's city having same age.
country city age age_count
----------------------------------------------
India Mumbai 22 2
India Delhi 22 1
India Delhi 23 1
India Chennai 23 1
China Beijing 20 2
China Shanghai 20 1
USA New York 30 3
USA Los Angeles 31 2
USA Los Angeles 40 1
Try this :;
SELECT country,
city,
age,
count(user_id) AS age_count
FROM user
GROUP BY country,
city,
round(age)
select country, city, age, count(*) age_count
from user
group by country, city, age