I have a table that has user_id and purchase_id. I want to filter the table so that only users with more than 2 purchases (i.e. there are more than 2 rows for that user in the table). I used count and group by, but it does not work in a way I want.
create view myview as
select user_Id, purchase_id, count(*) as count from mytable group by user_Id;
select user_id, purchase_id from myview where count >2;
But it gives me only users (only one user_id) that has more than 2 purchase and it does not give me all of their purchases. For example if the table looks like this:
user_id purchase_id
1 1212
1 1312
2 1232
1 1321
3 1545
3 4234
My query gives me this :
1 1212
3 1545
But I want this:
1 1212
1 1312
1 1321
3 1545
3 4234
change your last sql like this
select mt.user_id, mt.purchase_id
from myview mv
inner join mytable mt
on mt.user_id=mv.user_id where mv.count >5;
SELECT
*
FROM
mytable mt,
(SELECT user_id, count(*) AS purchase_count FROM mytable GROUP BY user_id) ct
WHERE
mt.user_id = ct.user_id AND ct.purchase_count > 5;
SELECT *
FROM MYTABLE
WHERE USER_ID IN (SELECT USER_ID
FROM MYTABLE
GROUP BY USER_ID
HAVING COUNT(*)>=2)
I tested in my netezza,it works. hopefully, it's also working in mysql
Try GROUP BY with HAVING comment.
SELECT user_Id, purchase_id
FROM mytable
GROUP BY user_Id
HAVING count( * ) >5
As far as I can tell you want to list the user id's and purchase id's of all users that have over 5 purchases.
In order to do this you could do a join on two queries.
For example:
SELECT tblLeft.user_id,
tblLeft.purchase_id
FROM myview tblLeft
JOIN (SELECT user_id,
Count(*) AS purchases
FROM myview
GROUP BY user_id
HAVING purchases > 1) tblRight
ON tblLeft.user_id = tblRight.user_id
The tblRight is essentially a table containing the user_id's of all users with over 5 purchases.
We then do a select (tblLeft) and join it on the tbl right, ensuring only customers with over 5 purchases remain.
Related
I have 2 Tables..
User 1
user_id mobile_no
1 1111
2 2222
User 2
user_id mobile_no
1 3333
2 2222
I Want to first UNION These tables, then group by and then want to count total records
I am using this query but it's not working
SELECT COUNT(Q2.total) AS Overall
FROM (
SELECT COUNT(Q.user_id) AS total
FROM (
SELECT * FROM user1
UNION ALL
SELECT * FROM user2
) Q
GROUP BY Q.mobile_no
) Q2
if i user Inner Query e-g:
SELECT COUNT(Q.user_id) AS total
FROM (
SELECT * FROM user1
UNION ALL
SELECT * FROM user2
) Q
GROUP BY Q.mobile_no
I get these results, actually i want to again count these result...
total
2
1
1
i expect this result
Overall
3
This is weird. No one seems to have realised it's as simple as:
select count(*) overall
from (select mobile_no from user1 union select mobile_no from user2)
The difference between union and union all is that union removes duplicates.
Assuming that you are looking for the distinct number of mobile numbers:
select count(distinct mobile_no) as Overall
from (
select user_id, mobile_no
from user1
union all
select user_id, mobile_no
from user2
) a
select count (distinct mobile_no) from
(select user_id, mobile_no from user1 u1
UNION ALL
select user_id, mobile_no from user2 u2
) X
group by X.mobile_no
Rather use UNION and not UNION ALL
SQL UNION Operator
The UNION operator selects only distinct values by default. To allow
duplicate values, use the ALL keyword with UNION.
SELECT COUNT(mobile_no) Overall
FROM (
SELECT
mobile_no
FROM User1
UNION
SELECT
mobile_no
FROM User2
) Q
EDIT:
As #Bohemian correctly stated, no need for the distinct.
Try this:
SELECT COUNT(*) FROM
( (SELECT * FROM user1) UNION
(SELECT user_id as u1,mobile_no as m1
FROM user2) ) as a1 GROUP BY a1 .1
I'm trying to return a count of return users which happens when there is a duplicate 'user_id and action_type'.
So if you refer below, I would like my output to be = 2, since user_id (5) has 2 similar action_types (234) and user_id (6) also has 2 similar action_types (585).
How do I structure my query to reflect this?
Table t1
User_Id Action_Type
--------- ------------
5 234
5 846
5 234
6 585
6 585
7 465
SELECT COUNT(DISTINCT User_Id) FROM (
SELECT User_Id
FROM t1
GROUP BY User_Id, Action_Type
HAVING COUNT(*) > 1
) t
SELECT COUNT(User_ID) DuplicateRecordsUsers
FROM
(SELECT User_ID, Action_Type, COUNT(User_ID) Records
FROM Table
GROUP BY User_ID, Action_Type
HAVING COUNT(User_ID) > 1
)
SELECT COUNT(User_Id) FROM (
SELECT User_Id
FROM t1
GROUP BY User_Id, Action_Type
HAVING COUNT(*) > 1
) t
DISTINCT not required just count the ids returned
I have table with, folowing structure.
tbl
id name
1 AAA
2 BBB
3 BBB
4 BBB
5 AAA
6 CCC
select count(name) c from tbl
group by name having c >1
The query returning this result:
AAA(2) duplicate
BBB(3) duplicate
CCC(1) not duplicate
The names who are duplicates as AAA and BBB. The final result, who I want is count of this duplicate records.
Result should be like this:
Total duplicate products (2)
The approach is to have a nested query that has one line per duplicate, and an outer query returning just the count of the results of the inner query.
SELECT count(*) AS duplicate_count
FROM (
SELECT name FROM tbl
GROUP BY name HAVING COUNT(name) > 1
) AS t
Use IF statement to get your desired output:
SELECT name, COUNT(*) AS times, IF (COUNT(*)>1,"duplicated", "not duplicated") AS duplicated FROM <MY_TABLE> GROUP BY name
Output:
AAA 2 duplicated
BBB 3 duplicated
CCC 1 not duplicated
For List:
SELECT COUNT(`name`) AS adet, name
FROM `tbl` WHERE `status`=1 GROUP BY `name`
ORDER BY `adet` DESC
For Total Count:
SELECT COUNT(*) AS Total
FROM (SELECT COUNT(name) AS cou FROM tbl GROUP BY name HAVING cou>1 ) AS virtual_tbl
// Total: 5
why not just wrap this in a sub-query:
SELECT Count(*) TotalDups
FROM
(
select Name, Count(*)
from yourTable
group by name
having Count(*) > 1
) x
See SQL Fiddle with Demo
The accepted answer counts the number of rows that have duplicates, not the amount of duplicates. If you want to count the actual number of duplicates, use this:
SELECT COALESCE(SUM(rows) - count(1), 0) as dupes FROM(
SELECT COUNT(1) as rows
FROM `yourtable`
GROUP BY `name`
HAVING rows > 1
) x
What this does is total the duplicates in the group by, but then subtracts the amount of records that have duplicates. The reason is the group by total is not all duplicates, one record of each of those groupings is the unique row.
Fiddle: http://sqlfiddle.com/#!2/29639a/3
SQL code is:
SELECT VERSION_ID, PROJECT_ID, VERSION_NO, COUNT(VERSION_NO) AS dup_cnt
FROM MOVEMENTS
GROUP BY VERSION_NO
HAVING (dup_cnt > 1 && PROJECT_ID = 11660)
I'm using this query for my own table in PHP, but it only gives me one result whereas I'd like to the amount of duplicate per username, is that possible?
SELECT count(*) AS duplicate_count
FROM (
SELECT username FROM login_history
GROUP BY username HAVING COUNT(time) > 1
) AS t;
Consider the following MySQL table:
user_id
-------
1
1
2
3
3
3
3
I would like an output of:
user_id cnt
-------------
1 2
2 1
3 4
I though this would make that happen, but it doesn't:
SELECT user_id, COUNT(1) FROM table GROUP BY user_id
What am I doing wrong?
SELECT user_id, COUNT(*) FROM table GROUP BY user_id;
btw, there is also the curiosity:
SELECT user_id, SUM(1) FROM table GROUP BY user_id
Both will give you the output you want.
Try this:
SELECT user_id, COUNT(user_id) as cnt FROM table GROUP BY user_id
SELECT user_id, count(user_id) as total FROM temp group by user_id;
The query produced this.
user_id total
-------------
1 2
2 1
3 4
Table 1 : (Company)
ID Name
1 A
2 B
3 C
Each company (pk = ID) can have one or more employees.
Table 2 : (Employee) (CompanyID referencing ID)
CompanyID EmpID Name
1 1 Joe
1 2 Doe
1 3 Boe
2 4 Lou
3 5 Su
3 6 Ram
Query :
select CompanyID, count(*) from Employee group by CompanyID having count(*) > 1; # Lists companies and their counts.
CompanyID count(*)
1 3
3 2
For this query, I want just one result with the count of distinct CompanyIDs. So, '2' in this case [Companies A and C].
In short, I am looking for number of companies with 2 or more employees.
Is there anyway to get the result without a temp table or a join? I am using MySQL.
Yes:
select count(*) from
(select CompanyID from Employee group by CompanyID having count(*) > 1) v
or for ranges:
select count(*) from
(select CompanyID from Employee group by CompanyID
having count(*) >= 5 and count(*) < 10) v
Yes, it's possible with subqueries:
SELECT COUNT(*)
FROM
( SELECT 1
FROM Employee
GROUP BY CompanyID
HAVING COUNT(*) > 1
) AS grp
or:
SELECT COUNT(DISTINCT CompanyID)
FROM Employee AS e
WHERE EXISTS
( SELECT *
FROM Employee AS e2
WHERE e2.CompanyID = e.CompanyId
AND e2.EmpID <> e.EmpID
)
or perhaps if COUNT(*) is slow, you can use MIN() and MAX():
SELECT COUNT(*)
FROM
( SELECT 1
FROM Employee
GROUP BY CompanyID
HAVING MAX(EmpID) > MIN(EmpId)
) AS grp