I have a photos table, with two columns in that table named "id" and "user_id". Obviously one user can have many photos. I'd like to run a query that can give me the photo count for each user.
Any help is appreciated.
select user_id, count(*) as photo_count from photos group by user_id
Use:
SELECT t.user_id,
COUNT(*) AS photo_count
FROM PHOTOS
GROUP BY t.user_id
use count as your aggregate function and groupby clause to give the count of specific attribute,
here is the sample:
SELECT user_id, count(*) 'Photos Count'
from photos
group by user_id
Related
I have an sql table called messages, where i receive all messages from various devices.
The structure is (start_time is a unique key):
explanation: first remove all duplicates from one session and then sum all quantities.
I will be very thankful for your help
Try this query. It should do it.
select t.product, sum(t.quantity) as sum_quantity
from
(
select distinct product, quantity
from
messages
) t
group by t.product
order by t.product asc
Try doing a group by with a distinct
Select Product, Sum(Distinct Quatity) As Qty
From messages
Group By Product
I am trying to generate a simple report that will display the number of customers owning number of distinct brands. The following query I wrote generates the desired numbers one at a time. I tried writing a loop and it takes forever. Is there an alternative?
SELECT COUNT(DISTINCT customer_id)
FROM
(
SELECT customer_id,COUNT(DISTINCT brand) AS no_of_customers
FROM table_A
WHERE brand_id != 10
GROUP BY customer_id
HAVING COUNT(DISTINCT brand) =1
ORDER BY customer_id) as t1;
What this does is to give me a count of customers with a total count of distinct brands =1. I change the count of brands to 2,3 and so on. Please let me know if there is a way to automate this.
Thanks a lot.
Use a second level of GROUP BY to get them all in one query, rather than looping.
SELECT no_of_brands, COUNT(*) no_of_customers
FROM (SELECT customer_id, COUNT(DISTINCT brand) no_of_brands
FROM Table_A
WHERE brand_id != 10
GROUP BY customer_id) x
GROUP BY no_of_brands
You also don't need DISTINCT in your outer query, since the inner query's grouping guarantees that the customer IDs will be distinct.
I have a query like this, to select the most recent time someone was contacted:
SELECT `user_id`, `last_contact`
FROM `emails_sent`
group by `user_id`
order by `last_contact` desc
The above code gives a table with the last contact time for each user. Now, I have another table with contacts to users, a table with columns user_id and last_contact, among others.
How can I make my select use both tables and select the last contact time for each user from the two tables?
Summarize the union of two summary queries, something like this.
SELECT user_id,
MAX(user_date) user_date
FROM
(
SELECT user_id,
MAX(last_contact) user_date
FROM emails_sent
GROUP BY user_id
UNION ALL
SELECT whatever_user_id_column user_id,
MAX(whatever_date_column) user_date
FROM whatever_table
GROUP BY user_id
)a
GROUP BY user_id
I have trying to identify and select all dublicated entries filled under an row in my mysql table.
I have tried using this query:
SELECT id, link, COUNT(*)
FROM linkuri
HAVING COUNT(*)>1
LIMIT 0 , 30
The problem is that is resulting me 0 results and I've checked manualy few pages and I've seen some dublicates entries.
What I want is to check and delete all dublicated entryes filled under the row link.
You are probably looking for
SELECT a.id, a.link, b.cnt
FROM linkuri
INNER JOIN
( SELECT link, COUNT(*) AS cnt FROM linkuri GROUP BY link HAVING COUNT(*) >1 )b
ON (b.link = a.link)
The problem with your query is you're not grouping by anything. To find duplicates you have to group them by a column(s) and then get the count from each group. Having statement is like a where clause on the group http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT id, link, COUNT(*)
FROM linkuri
GROUP BY link
HAVING COUNT(*)>1
LIMIT 0 , 30
I am trying to filter (or count) companies with multiple contact persons from a table containing a company_id and a person_id. Currently I just do this:
SELECT DISTINCT company_id,person_id FROM mytable GROUP BY company_id ORDER BY company_id
as well as
SELECT DISTINCT company_id FROM mytable
The first query returns a couple of rows more. Hence it is obvious that there are companies with multiple contact persons. From the different row count between the two queries I can even tell how many of them. Though I´d like to know how I can select exactly those companies that have more than one person_id assigned.
Thx in advance for any help!
How about this?
SELECT company_id, COUNT(DISTINCT person_id)
FROM mytable
GROUP BY company_id
HAVING COUNT(DISTINCT person_id) > 1
SELECT
company_id
FROM
mytable
GROUP BY
company_id
HAVING
COUNT(person_id) > 1
ORDER BY
company_id