Select entries appearing in all results from another query - mysql

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 )

Related

Count Distinct on multiple values within same column in SQL Aggregation

Objective:
I wanted to show the number of distinct IDs for any combination selected.
In the below example, I have data at a granular level: ID level data.
I wanted to show the number of distinct IDs for each combination.
For this, I use count distinct which will give me '1' for the below combinations.
But let's say if I wanted to find the number of IDs who made both E-commerce and Face to face transactions, in that case, if I just use this data, I would be showing the sum of E-comm and Face to face and the result would be '2' instead of '1'.
And this is not limited to Ecom/Face to face. I wanted to apply the same logic for all columns.
Please let me know if you have any other alternative approach to address this issue.
First aggregate in your table to get the distinct ids for each TranType:
SELECT TranType, COUNT(DISTINCT id) counter_distinct
FROM tablename
GROUP BY TranType
and then join to the table:
SELECT t.*, g.counter_distinct
FROM tablename t
INNER JOIN (
SELECT TranType, COUNT(DISTINCT id) counter_distinct
FROM tablename
GROUP BY TranType
) g ON g.TranType = t.TranType
Or use a correlated subquery:
SELECT t1.*,
(SELECT COUNT(DISTINCT t2.id) FROM tablename t2 WHERE t2.TranType = t1.TranType) counter_distinct
FROM tablename t1
But let's say if I wanted to find the number of IDs who made both E-commerce and Face to face transactions, in
You can get the list of ids using:
select id
from t
where tran_type in ('Ecomm', 'Face to face')
group by id
having count(distinct tran_type) = 2;
You can get the count using a subquery:
select count(*)
from (select id
from t
where tran_type in ('Ecomm', 'Face to face')
group by id
having count(distinct tran_type) = 2
) i;

Alternative for a loop

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.

SQL Query - How to find out how which employees worked at more than one store

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

Count multiple appearances in DISTINCT statement with two parameters?

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

Using DISTINCT and COUNT together in a MySQL Query

Is something like this possible:
SELECT DISTINCT COUNT(productId) WHERE keyword='$keyword'
What I want is to get the number of unique product Ids which are associated with a keyword. The same product may be associated twice with a keyword, or more, but i would like only 1 time to be counted per product ID
use
SELECT COUNT(DISTINCT productId) from table_name WHERE keyword='$keyword'
I would do something like this:
Select count(*), productid
from products
where keyword = '$keyword'
group by productid
that will give you a list like
count(*) productid
----------------------
5 12345
3 93884
9 93493
This allows you to see how many of each distinct productid ID is associated with the keyword.
You were close :-)
select count(distinct productId) from table_name where keyword='$keyword'
FYI, this is probably faster,
SELECT count(1) FROM (SELECT distinct productId WHERE keyword = '$keyword') temp
than this,
SELECT COUNT(DISTINCT productId) WHERE keyword='$keyword'
What the hell of all this work anthers
it's too simple
if you want a list of how much productId in each keyword here it's the code
SELECT count(productId), keyword FROM `Table_name` GROUP BY keyword;
SELECTING DISTINCT PRODUCT AND DISPLAY COUNT PER PRODUCT
for another answer about this type of question, this is my way of getting the count of product based on their product name using distinct. Here a sample:
List of Product (SQLFiddle):
select * FROM Product
Product Name Count Using Distinct (SQLFiddle):
SELECT DISTINCT(Product_Name),
(SELECT COUNT(Product_Name)
from Product WHERE Product_Name = Prod.Product_Name)
as `Product_Count`
from Product as Prod
Optimized version without Distinct:
SELECT Product_Name, COUNT(Product_Name) AS `Product_Count`
FROM Product
GROUP BY Product_Name
Isn't it better with a group by?
Something like:
SELECT COUNT(*) FROM t1 GROUP BY keywork;