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
Related
I am doing the data cleaning in a BigQuery. I managed to count each of the variables. However there are some redundant info with different variables, so I need to merge the number and save the overall total in a single row.
This is my work:
SELECT
day,
COUNT(*) as Total,
FROM
table
where day<> 'null'
GROUP BY day
-- HAVING COUNT(*) >= 10?
ORDER BY COUNT(*) DESC;
The result is
What should I add so the Monday and Mndy is added in a single row? Thanks
SELECT
CASE WHEN day in ('Monday', 'Mndy') THEN 'Monday' ELSE day END day,
COUNT(*) as Total,
FROM
table
where day<> 'null'
GROUP BY 1
-- HAVING COUNT(*) >= 10?
ORDER BY COUNT(*) DESC;
There is a task: develop a fragment of the Web site that provides work with one table.
Attributes of the table:
Day of the week,
Time of the beginning of the lesson,
Subject name,
Number of the audience,
Full name of the teacher.
We need to make a query: determine the day of the week with the largest number of entries, if there are more than one maximum (ie, they are the same), then output them all. I did the query as follows:
SELECT COUNT (*) cnt, day
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1;
But if there are several identical maxima, then only one is displayed. How to write a query which returns them all?
You can use your query as a subquery in the HAVING clause, e.g.:
SELECT day, count(*) as cnt
FROM schedule
GROUP BY day
HAVING count(*) = (
SELECT count(*) as cnt
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1
)
ORDER BY day
I am trying to perform the following query on my database :-
SELECT
source, Month as t1,
GROUP_CONCAT(SELECT SUM(amount) FROM `reports` GROUP BY Month) as amount
FROM `reports`
GROUP BY source
To get the source, month and the concatenated string of the sum of the money that is obtained by the distinct source in 1 month. But I get a syntax error.
I'm not exactly sure what you need, hopefully it is one of these two:
SELECT source, Month, SUM(amount) as sum
FROM reports
GROUP BY source, Month
The above, but grouped by source with the sums listed in one field:
SELECT source, GROUP_CONCAT(sum) as sums
FROM (
SELECT source, Month, SUM(amount) as sum
FROM reports
GROUP BY source, Month
) as t
GROUP BY source
I am trying to calculate active monthly users based on signup and cancel dates. Several users have NULL cancel dates (since still active). This query has a bunch of users as just null action_year and action_month.
SELECT
T.action_year,
T.action_month,
COUNT(USerID) active_users
FROM
(
SELECT DISTINCT UserID, YEAR(SignupDate) action_year, MONTH(SignupDate) action_month FROM Stat
UNION
SELECT DISTINCT UserID, YEAR(CancelDate) action_year, MONTH(CancelDate) action_date FROM Stat
) T
GROUP BY
T.action_year,
T.action_month
ORDER BY
T.action_year ASC,
T.action_month ASC
Presumably active users are those where the month is somehow between the signup and cancel dates. This is tricky to define. Is it active on any date of the month? Active on the last day? Active on the first day?
I will assume the first. Active on any day during the month.
The idea is that the number of actives in a given month are all people who have signed up previously and not yet stopped. Given this observation, the calculation proceeds as follows:
Get a list of all years and months. The following query assumes that signups occur every month to simplify this part.
Use a correlated subquery to get the number of actives. This will do comparisons to the "yyyymm" form of the date.
Be sure to remember that CancelDate can be NULL.
The resulting query is:
select ym.the_year, ym.the_month,
(select count(*)
from stat s
where date_format(SignupDate, '%Y-%m') <= ym.yyyymm and
(CancelDate is null or date_format(CancelDate, '%Y-%m') >= ym.yyyymm)
) as NumActives
from (select distinct year(SignupDate) as the_year, month(SignupDate) as the_month,
date_format(SignupDate, '%Y-%m') as yyyymm
from stat
) ym
I am querying a mysql table and want results group by date, and one column name is type. There are two value for the type call and email. I want to find count for call and email for each day.
Find the SQL Fiddle here
I am trying with this query. Which only gets me total counts:
SELECT Date(date) date,
COUNT(type) total,
COUNT(type='email') emails,
COUNT(type='call') calls
from leads
where user_id = 1
GROUP BY Date(date)
Use SUM() instead. The type='email' in the function returns either 0 (false) or 1 (true).
SELECT Date(date) date,
COUNT(type) total,
SUM(type='email') emails,
SUM(type='call') calls
from leads
where user_id = 1
GROUP BY Date(date)