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
Related
I have a single table.
This table has 2 fields, product IDs and Store IDs. The same product ID can exist with many different Store IDs.
I need to find the products (if any) that are common across all the stores.
I'm having difficulty constructing the correct query, any advice?
You can check distinct store ids count with product id. If distinct Store ids count equal to total stores that will be the product ids you want.
SELECT productID, count(DISTINCT StoreID) as stroes FROM [Table name] GROUP BY productID
HAVING COUNT(DISTINCT StoreID) = (SELECT COUNT(DISTINCT StoreID) FROM [Table name] );
I'm sure you'll get many better answers, but it sounds like you are wanting the reverse of the distinct clause, not sure if this will work though:
SELECT NOT DISTINCT [Product_ID]
FROM TABLENAMEHERE
You could sue count(distinct productID)
select productID
from my_table
group by productID
having count(distinct productID) = (
select count(distinct store)
from my_table )
I have two tables with identical schema. I want to get a count of all the people with a given surname in both tables, and have found I can do it like this:
SELECT surname, count(*) AS cnt
FROM
(
SELECT surname
FROM people.NorthKorea
UNION ALL
SELECT surname
FROM peopleGlobal.NorthKorea
) AS t
GROUP BY surname
ORDER BY cnt DESC
This is fine for small tables, but I have tables with up to 250 million rows, so was wondering if there may be a more efficient way of doing this? Such as INSERTING the result of the COUNT from one table into a table, and then updating / inserting (REPLACE?) the result of the COUNT on the second table.
N.B. I actually want to store the result of the COUNT on both tables in another table.
An index on the surname column should help a lot. I would try with this query, if there are a lot more rows than surnames I expect it to run faster:
SELECT surname, SUM(cnt)
FROM
(
SELECT surname, COUNT(*) as cnt
FROM people.NorthKorea
GROUP BY surname
UNION ALL
SELECT surname, COUNT(*) as cnt
FROM peopleGlobal.NorthKorea
GROUP BY surname
)
GROUP BY surname
ORDER BY cnt DESC
right now I'm trying to return the biggest COUNT(DISTINCT column)-number from a mysql table.
It's hard to describe, so I'll give you an example:
My table has the following columns: s_id, k_id, p_id.
Now I want to count the different s with the condition that every entry has the same p_id, too. I need this to prepare a HTML-Table (so i know how many Columns this table will have).
Data Example:
This is what I got, so far:
SELECT COUNT(DISTINCT k_id) AS a FROM `table`
the problem with this is, that there may be 4 different k_ids but 3 of them are related to p_id = 1 and the last one is releated to p_id = 2.
a returns 4 instead of 3.
Thanks for support!
I think you want this:
select p_id, count(distinct s_id) as cnt
from table
group by p_id
order by cnt desc
limit 1;
Please consider this:
select max(count(distinct(k_id))) from table
group by p_id
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 relationship table such that it has
employeeID | storeID
What would be the query to find out which employees worked at more than one store?
SELECT employeeID WHERE ???
And possibly also list each different stores just once per employee...
Use group by and having, as in:
select employeeID, count(*) from table group by employeeID having count(distinct storeID) > 1
This will give you the employees working at more than one store. Use this as a subquery to list the stores for each such employee.
you can try -
select distinct employeeID,StoreID from table1
where storeID in
(
select storeID from table1 group by storeID having count(distinct employeeID) >1
)
cor storing count and showing store ID also in one query you can use following query..
select a.employeeID,a.storeID,b.cnt
from table1 a,
(select employeeID,count(*) cnt
from table1
group by employeeID
having count(distinct storeID) >1) b
where a.employeID=b.employeeid