I have a single table and I need to count the number of unique viewers for each month.
SELECT month, location, COUNT(DISTINCT id) AS unique_viewers
FROM mytable
GROUP BY month, location
I need the results to only display the month, location, and the number of unique views based on the acct ids.
for example
Month ID Location
JAN 123 CA
JAN 456 CA
JAN 123 CA
I am having trouble counting based on the ID being distinct. I need my result to dislplay 2 unique views for Jan
I have a single table and I need to count the number of unique viewers for each month.
"for each month" translates to GROUP BY month in SQL.
"count the number of unique viewers" translates to COUNT(DISTINCT viewer) (or whatever that column is named).
The query:
SELECT month, COUNT(DISTINCT viewer) AS unique_viweers
FROM mytable
GROUP BY month
ORDER BY month;
I need the results to only display the month, location, and the number of unique views based on the acct ids.
The location? Which location? There can be multiple locations in a month. So this cannot be solved. You could display a list of locations instead using GROUP_CONCAT.
SELECT
month,
COUNT(DISTINCT viewer) AS unique_viweers,
GROUP_CONCAT(DISTINCT location ORDER BY location) as locations
FROM mytable
GROUP BY month
ORDER BY month;
You have altered your request, contradicting "I need to count the number of unique viewers for each month". It seems you rather want to count the number of unique viewers for each month and location.
Use COUNT(DISTINCT acct_id):
SELECT month, location, COUNT(DISTINCT acct_id) AS unique_viewers
FROM mytable
GROUP BY month, location
ORDER BY month, location;
Related
Suppose, I have a table :
classroom_id(int): id of the classroom
user_id (int): id of the user
login_date(date): date of login
login_attempt_id(int): unique id of login
I am trying to find the frequency of logins in a week, so if a student logins 3 days in a week (in any order) the output would be 3.
So, the output of frequency would be in the range of 0-7, meaning, the person has logged in 0-7 days out of the 7 days of the week.
At the same time, a student can log in to the system multiple times on the same day. I am looking to get unique instances across the week only.
The data is for a month and the output should contain:
user_id(int): id of the user
week: week numbers.
frequency: frequency of login in that week number
I have written a query but I am stuck:
with cte as(
select distinct classroom_id,
user_id,
date_format(login_date,'w') as week,
login_date,
login_attempt_id
from table)
Select classroom_id,
user_id,
week,
count(login_date) as frequency from cte
group by 1,2,3
I am not sure how to go about this problem. Any help would be appreciated.
you dpn't nee a CTE for that
COUNT supports DISTINCT
select classroom_id,
user_id,
date_format(login_date,'w') as week,
COUNT(DISTINCT login_date) as frequency
from table1
GROUP BY classroom_id,
user_id,
date_format(login_date,'w')
My database table contains value in this way
IMAGE FOR TABLE DATA
I want to track down same email which has been used more than one times for a particular month and year.
In the above scenario, email that has been repeated multiple times was sandeshphuya#gmail.com, jes#gmail.com and ramu#gmail.com for different months. I want to track down customer repetition following their email for each month and year.
The query that I am using right now is
SELECT DATE_FORMAT(booked_on, '%Y-%m') as monthYear, email,
COUNT(*) AS 'Count'
FROM 'tablename'
GROUP BY email,DATE_FORMAT(booked_on, '%Y-%m') HAVING COUNT(*) > 1 ORDER BY `booked_on`;
GROUP BY email was used as it generates the repeated email and GROUP BY DATE_FORMAT(booked_on, '%Y'-%m') was used to track down total email repeated for each month/year.
This prints out data as
IMAGE FOR SELECT QUERY
How can I track down total repeated email following month and year? The expected result is
RESULT EXPECTED
You can use your query as a subquery for a new group by:
select sub.monthYear,count(*)
from
(SELECT DATE_FORMAT(booked_on, '%Y-%m') as monthYear,
email,
COUNT(*) AS 'Count'
FROM 'tablename'
GROUP BY email,DATE_FORMAT(booked_on, '%Y-%m')
HAVING COUNT(*) > 1
ORDER BY `booked_on`) as sub
GROUP BY sub.monthYear
So what i'm trying to do here, is that i am trying to count the number of repeat users (users who made more than one order) in a period of time, let it be month day or year, the case here is months
i'm currently running mysql mariadb and i'm pretty much a beginner in mysql, i've tried multiple subqueries but all have failed till now
This is what i have tried so far ..
This returns all the number of users with no ordering count condition
Since people are asking for sample data, here is what the data is looking like at the moment:
Order_Creation_Date - User_ID - Order_ID
2019-01-01 123 1
2019-01-01 123 2
2019-01-01 231 3
2019-01-01 231 4
This is the query i am using to get the result but it keeps on returning total number of users within the month
select month(o.created_at)month,
year(o.created_at)year,
count(distinct o.user_uuid) from orders o
group by month(o.created_at)
having count(*)>1
and this returns the number of users as 1 ..
select month(o.created_at)month,
year(o.created_at)year,
(select count(distinct ord.user_uuid) from orders ord
where ord.user_uuid = o.user_uuid
group by ord.user_uuid
having count(*)>1) from orders o
group by month(o.created_at)
Expected result will be from the sample data above
Month Count of repeat users
1 2
If you want the number of users that make more than one purchase in January, then do two levels of aggregations: one by user and month and the other by month:
select yyyy, mm, sum( num_orders > 1) as num_repeat_users
from (select year(o.created) as yyyy, month(o.created) as mm,
o.user_uuid, count(*) as num_orders
from orders o
group by yyyy, mm, o.user_uuid
) o
group by yyyy, mm;
I think you should try something like this which will return USer_ID list Month and Year wise who ordered more that once for the period-
SELECT
[user_uuid],
MONTH(o.created_at) month,
YEAR(o.created_at) year,
COUNT(o.user_uuid)
FROM orders o
GROUP BY
MONTH(o.created_at),YEAR(o.created_at)
HAVING COUNT(*) > 1;
For more, if you are looking for the count that how many users placed more that one order, you can just place the above query as a sub query and make a count on column 'user_uuid'
In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.
I have three queries;
Q1. Returns all customer records (id, name, year amount) where the customer has spent money with us.
Q2. Returns all customers records (id,name, year of regis, number) where the customer has a telephone number.
Q3. Returns all customer records (id,name, year, amount) where the customer has a purchased a specific product.
I want a new query that will return a list of all customers ids, names if they appear in any of the 3 queries, along with the latest year. How do I do that?
First, add all the results of the 3 queries together using UNION, then group the records by id and select the first name (or any name really, because they must be all the same) and the maximum year.
Try this:
SELECT id, FIRST(name), MAX(year)
FROM (
SELECT id, name, year FROM q1
UNION
SELECT id, name, year FROM q2
UNION
SELECT id, name, year FROM q3) AS q
GROUP BY id