How to count in a SQL list - mysql

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;

Related

SQL query help( Yes,I have already tried nested select)

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

Mysql distinct date by month

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.

count number of repeating entries

I am fairly new to Databases and I am just beginning to understand the DML/queries, I have two tables, one named customer this contain customer data and one named requested_games, this contains games requested by the customers, I would like to write a query that will return the customers that have requested more than two games, so far when I run the query, I don't get the desired result, not sure if I'm doing it right.
Can anyone assist with this thanks,
Below is a snippet of the query
select customers.customer_name, wants_list.requested_game, wants_list.wantslists_id,count(wants_list.customers_ID)
from customers, wants_list
where customers.customers_ID = wants_list.customers_id
and wants_list.wantslists_id = wants_list.wantslists_id
and wants_list.requested_game > '2';
just include a HAVING clause
GROUP BY customers_ID
HAVING COUNT(*) > 2
depending on how you have your data setup you may need to do
HAVING COUNT(wants_list.requested_game) > 2
This is how I like to describe how a query works maybe itll help you visualize how the query executes :)
SELECT is making an order at a restaurant....
FROM is the menu you want to order from....
JOIN is what sections of the menu you want to include
WHERE is any customization you want to make to your order (aka no mushrooms)....
GROUP BY (and anything after) is after the order has been completed and is at your table...
GROUP BY tells your server to bring your types of food together in groups
ORDER BY is saying what dishes you want first (aka i want my entree then dessert then appetizer ).
HAVING can be used to pick out any mushrooms that were accidentally left on the plate....
etc..
I would like to write a query that will return the customers that
have requested more than two games
For this to happen you need to do the following
First you need to use GROUP BY to group the games based on customers (customers_id)
Then you need to use HAVING clause to get customers who requested more than two games
Then make this a SUBQUERY if you need more information on the customer like name
Finally you use a JOIN between customers and the sub query (temp) to display more information on the customer
Like the following query
SELECT customers.customer_id, customers.customer_name, game_count
FROM (SELECT customer_id, count(wantslists_id) AS game_count
FROM wants_list
GROUP BY customer_id
HAVING count(requested_game) > '2') temp
JOIN customers ON customers.customer_id = temp.customer_id

MySQL GROUP BY and COUNT

I have a small problem regarding a count after grouping some elements from a mysql table,
I have an orders table .. in which each order has several rows grouped by a code (named as codcomanda) ... I have to do a query which counts the number of orders per customer and lists only the name and number of orders.
This is what i came up (this might be dumb ... i'm not a pro programmer)
SELECT a.nume, a.tel, (
SELECT COUNT(*) AS `count`
FROM (
SELECT id AS `lwtemp`
FROM lw_comenzi_confirmate AS yt
WHERE status=1 AND yt.tel LIKE **a.tel**
GROUP BY yt.codcomanda
) AS b
) AS numar_comenzi
FROM lw_comenzi_confirmate AS a
WHERE status=1
GROUP BY tel;
nume = NAME
tel = PHONE (which is the distinct identifier for clients since there's no login system)
The problem with the above query is that I don't know how to match the a.tel with the one on which the first select is on. If I replace it with a number that is in the db it works....
Can anyone help me one how to refer to that var?
or maybe another solution on how to get this done?
If any more info is needed I`ll provide asap.
Please, correct me if I'm wrong in my understanding of your schema:
lw_comenzi_confirmate contains nume and tel of the customer;
lw_comenzi_confirmate contains order details (same table);
one order can have several entries in the lw_comenzi_confirmate table, order is distinguished by codcomanda field.
First, I highly recommend reading about Normalisation and fixing your database design.
The following should do the job for you:
SELECT nume, tel, count(DISTINCT codcomanda) AS cnt
FROM lw_comenzi_confirmate
WHERE status = 1
GROUP BY nume, tel
ORDER BY nume, tel;
You can test this query on SQL Fiddle.

help with SQL queries

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
;