Ignore duplicates on GROUP BY - mysql

I have a table containing a shop_id, name and promotion_code. I want to get the amount of records for every shop_id, but there are some duplicate records where a promotion_code is used multiple times. I want to ignore these.
I have tried:
SELECT shop_id, count(*) as total_entries FROM tbl GROUP BY shop_id
This gives me the correct structure, but it does count all the duplicate rows. So I thought I'd try grouping them first on promotion_code, and then on shop_id:
SELECT shop_id, count(*) as total_entries FROM tbl GROUP BY promotion_code, shop_id
But this gives me all zero's as result.

SELECT shop_id, count(distinct promotion_code) as total_entries FROM tbl GROUP BY shop_id

try :
select shop_id, count(distinct promotion_code)
from tbl
group by shop_id

SELECT shop_id, count(*) as total_entries FROM tbl GROUP BY promotion_code, shop_id
Instead of using (*) you could use DISTINCT function so that you will be sure that all data you try to group are all unique values.
You could do something like this:
SELECT shop_id, COUNT(DISTINCT promotion_code) as total_entries FROM tbl GROUP BY shop_id

Related

MySQL INSERT into with COUNT() and GROUP BY error `Invalid use of group function`

I am trying to do the following query and get an error that it is an invalid use of group by function. Any ideas?
insert into table_a(id, date, product)
select id, date, count(product)
from table_b
group by id, date
on duplicate key update product = count(product)
I don't kow what you try to accomplish, sanatizing query helps only marginaly, but you can't use count( as you notced but you can make a corect SELECT that gives you the desired result.
insert into table_a(id, date, product)
select id, date, count(product)
from table_b
group by id, date
on duplicate key update product =
(SELECT COUNT(product) FROM table_b WHERE id = VALUES(id) GROUP BY id, date LIMIt 1) t1
This is of course only an example how you can do something leike you want, but of course you have to adept to your needs

How to select * from table when group by?

select
OrderNo,
Sum(QtyIn) as QuantityIn,
Sum(QtyOut) as QuantityOut
from
tbl_Assign
group by
OrderNo
I want to select * from table also group by from table. How to do it?
To group by on all columns with a sum you cannot use *, you have to list all of the columns out and every column that isn't a function like Sum must be included in the group by.
So if you have other fields in your database such as OrderName, OrderedBy you can perform a group by like this:
Select
OrderNo,
OrderName,
OrderBy,
Sum(QtyIn) as QuantityIn,
Sum(QtyOut) as QuantityOut
From
tbl_Assign
Group By
OrderNo, OrderName, OrderBy
The following will create one row for every row in the tbl_Assign.
Each row will also show the summary information for the order.
This might not be what you need, but it's useful to understand it anyway.
SELECT T1.*, T2.*
FROM
( select * FROM tbl_Assign ) AS T1
LEFT JOIN ( select
OrderNo,
Sum(QtyIn) as QuantityIn,
Sum(QtyOut) as QuantityOut
from
tbl_Assign
group by
OrderNo
) AS T2
ON T1.OrderNo = T2.OrderNo
Harvey

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

Mysql - getting distinct records

I have the following MySql table (user_actions) with the following columns:
id, user_id, created_at, action_type
I want a sql query that will get the latest actions that the user performed with no duplication of the actions.
for example:
user_id 1 has 3 records that has the action_type "follow"
and 2 records that has the action_type "unfollow"
in this case i want the query to return two records, one with action_type "follow" and one with "unfollow"
any thoughts?
You can use SQL's group by clause for this:
select user_id, action_type, max(created_at)
from user_actions
group by user_id, action_type
try this:
select ua.*
from user_actions ua
join (select max(id) as max_id,user_id,action_type from user_action group by user_id,user_action) ua_max
on ua.id=ua_max.max_id and ua.user_id=ua_max.user_id and ua.action_type=ua_max.action_type
Try this query:
select * from user_actions where action_type, created_at in
(select action_type, max(created_at) from user_actions group by action_type);

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;