I have 3 mysql tables:
members_table(registrationDate memberId ageBand scoreBand)
clicks_table(clickDate memberId isApplication isApproval revenue ageBand scoreBand)
activeUsers_table(activityMonth memberId ageBand scoreBand platform)
I'm trying to find the way to get the :
percentage of active users every month for each monthly registration cohort broken out by age.
I can't fine the right manner for find results, can you help please?
Thanks!
I fear your question is difficult to understand, so I'll just offer a hint about how you might proceed.
If you want to write aggregate queries to summarize by month, you can use the LAST_DAY() function. Here's an example:
SELECT COUNT(*) members, LAST_DAY(registrationDate) registrationMonth
FROM members_table
GROUP BY LAST_DAY(registrationDate)
You could write several queries like this summarizing different metrics by month. Then, treating the queries like tables, you could join them together.
Related
I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;
I have a table of tripadvisor data. There are columns (restaurant, rank, score, user_name,review_stars,review_date,user_reviews....) and other columns that are not useful for my question...
I am trying to return each restaurant with how many 3-star reviews they have and list them using the rank column from high to low.
I know i can use count, i was thinking of count if ( review_stars=3) and then order by rank to return it... i am stuck and any help would be appreciated. Thank you.
What you are wanting to accomplish is counting how many 3 star reviews each restaurant has ..
You really are almost there -- Your original question really does contains all the answers, just written out in long-hand. Think about what you are asking:
"I need to SELECT the restaurants that have 3 star reviews and count how many there are per restaurant" -- The basic syntax you are missing is GROUP BY -- Which groups all of your results per restaurant to a single row.
SELECT restaurant, count(*) as 3_star_count from table where review_stars = '3' GROUP BY restaurant
This is a basic example. But from what you are asking .. This syntax should get you the number of 3 star reviews for each restaurant.
I would recommend that you look into SQL clauses and what they mean as #Alexis stated in an earlier comment. The WHERE clause and the GROUP BY clause (especially this one) are what you want to understand here.
this is a screenshot of the database I am talking about
So suppose I have a database full of people with their ID numbers along with a year in which they made an entry plus their favorite show in that year. The years are always in the range 2014-2018, but not every one has an entry for each year. How can I count the total number of people who have consistently had the same show as their favorite show over all the years they have been recorded for.
I tried doing a nested selected but I kept getting error. I have checked other SQL related questions here talk about calculate 'change over the years' but none of those answers are compatible with my database and the solution wasn't transferable.
I think you need something like this:
See my SQLFiddle
select id, favorite_show, count(id) as total from people
group by id, favorite_show
having count(id) > 1
Hmmm . . . this gets the people who have only one show:
select count(*)
from (select person
from t
group by person
having min(show) = max(show)
) p;
You can count the number of different favorite shows someone has, and if that's 1 then they've had the same favorite every time.
SELECT COUNT(*)
FROM (SELECT 1
FROM yourTable
GROUP BY person_id
HAVING COUNT(DISTINCT favorite_show) = 1) AS x
How to get Number of Employees Joined or Resigned by Quarterly in SQL
I have a Table called Mst_Employee
fields are
Emp_No, Emp_JoiningDate, Emp_ResignedDate, Emp_Status
Edit: Every half a year, not quarterly.
The easiest way to do analysis like this would be to use the DATEPART function in T-SQL. Assuming all you want is to know a specific quantity for all quarters in all years on your table, an example code would be:
SELECT
DATEPART(YEAR,Emp_JoiningDate) as [Year],
DATEPART(QUARTER,Emp_JoiningDate) as [Quarter],
COUNT(1) as [Emp Count]
FROM Mst_Employee
GROUP BY DATEPART(YEAR,Emp_JoiningDate),DATEPART(QUARTER,Emp_JoiningDate)
ORDER BY 1,2
This will show all the numbers of employee's joined in the quarter. The query can easily be modified to also show resigned employee's in that quarter, or you could use a separate query to show this data.
Just an additional comment, as you're in the Employee Table, you don't need to directly state "Emp_" under all of your attributes.
Given the following diagram:
Right now I have queries to find out how much each member has donated and also listing those donations in a list for each member. Now I want to query the donations table to get the results divided up by each organization.
Something like:
Can someone please help me with this SQL?
Assuming that you're using MySQL:
SELECT
MemberId,
OrganizationId,
SUM(Amount) AS `Amount Donated to Organization`,
COUNT(Amount) AS `Number of Donations`
FROM
Donations
GROUP BY
MemberId,
OrganizationId
;