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;
Related
I am trying to return a list of brands that do not have a specific product ID.
Using the following SQL:
SELECT Brands
FROM AllCustomers
WHERE ProductID NOT IN (SELECT ProductID WHERE ProductID ='10235')
Is there a more effective way to do this as it keeps timing out?
Given your subquery has the explicit ProductID in the where clause and the subquery is only returning the ProductID you could simplify your query to be
SELECT Brands FROM AllCustomers WHERE ProductID <> '10235';
If you already have product ID, then why are you adding sub query ?
Simply write down query like:
SELECT `Brands` FROM `AllCustomers` WHERE `ProductID`!='10235'
Assuming you will actually use the same syntax for other queries, you didn't specify a table in your sub-query.
Eg, if we are getting the filtered records of ProductID from the same table, it should be:
SELECT Brands FROM AllCustomers WHERE ProductID NOT IN (SELECT ProductID FROM AllCustomers WHERE ProductID ='10235')
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 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 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
I would like to group products by their associated quoteId and get only the unique products from a table like below:
On top of doing this I would like to add a column that shows the count of how many of that product was in the associated quote similar to the following:
This is beyond my MYSQL skills and I was wondering if I could get a hand figuring out what would I have to do for a query to achieve this?
You can use the aggregation function count() and group by productid, quoteid
select productid, quoteid, count(*)
from my_table
group by productid, quoteid
Take a look at following ...
SELECT
productid, quoteid, COUNT(*) AS count
FROM
product
GROUP BY
productid, quoteid
HAVING
COUNT(*) > 1