Grouping two columns in mysql - mysql

I have a query?
I have a table as
sender_user_id receiver_user_id
2 3
3 2
2 7
2 8
7 3
10 6
2 3
and i want to group the columns by sender_user_id and receiver_user_id also
(sender id=2 and receiver_id=3) and (sender id=3 and receiver_id=2) should be grouped as one column with max timestamp

Is this what you are looking for?
SELECT CONCAT(sender_user_id,' ', receiver_user_id ) as newcolumn, max(datestamp)
from temptable
group by CONCAT(sender_user_id,' ', receiver_user_id ) ;
Really crude, quick, SQLFiddle Solution

Try the GROUP BY as a subquery and do the CONCAT afterwards
SELECT
CONCAT(sender_user_id,' ',receiver_user_id) sender_receiver,
max_datestamp
FROM
(
SELECT sender_user_id,receiver_user_id, max(datestamp) max_datestamp
FROM temptable GROUP BY sender_user_id,receiver_user_id
) A;
Make sure temptable is properly indexed
ALTER TABLE temptable ADD INDEX sender_receiver_ndx (sender_user_id,receiver_user_id);

Try this:
select max(timestamp) from yourTable group by sender_user_id, receiver_user_id

Not sure if you can do this with a GROUP BY combining the two columns. You could try a UNION
SELECT user1,user2,MAX(timestamp) FROM
(SELECT
sender_user_id AS user1,
receiver_user_id AS user2,
timestamp
FROM table
) AS table1
UNION DISTINCT
(SELECT
receiver_user_id AS user1,
sender_user_id AS user2,
timestamp
FROM table) AS table2
GROUP BY user1,user2

try
SELECT *, MAX(timestamp) FROM <my_table> GROUP BY sender_user_id, receiver_user_id

Not clear what you mean. Do you want something like
group by sender_id, reciever_id
order by timestamp desc

Related

Mysql Join query with count table records with same date

Hello i am having two different table with same field created_date (datetime)
now i want records which counts daywise records with joining table i have done for individual counting as below query :
SELECT DATE(created_date), COUNT(*) FROM table1 GROUP BY DAY(created_date)
SELECT DATE(created_date), COUNT(*) FROM table2 GROUP BY DAY(created_date)
and i am getting results for individuals something like this:
RESULT I NEED :
DATE(created_date) count(table1) count(table2)
2016-12-01 10 3
2016-12-02 1 0
2016-12-05 1 0
2016-11-29 1 0
2016-11-30 4 1
Now i just want to join these result WITH INDIVIDUAL VIEW COUNT ACCORDING TO TABLE can anyone please help me out with this profile....
First take a UNION between your two tables, then use conditional aggregation to determine the counts for each of the two tables. Note that I introduce a field called table_name to keep track of data from each of the two tables.
SELECT t.created_date,
SUM(CASE WHEN t.table_name = 'one' THEN 1 ELSE 0 END) AS count_table_one,
SUM(CASE WHEN t.table_name = 'two' THEN 1 ELSE 0 END) AS count_table_two
FROM
(
SELECT DATE(created_date) AS created_date, 'one' AS table_name
FROM table1
UNION ALL
SELECT DATE(created_date), 'two'
FROM table2
) t
GROUP BY t.created_date
I used DATE consistently everywhere to make the query correct.
Try This:
SELECT created_date, sum(countTable1) countTable1,
sum(countTable2) countTable2
FROM (
SELECT DATE(created_date) created_date, COUNT(*) countTable1, NULL countTable2
FROM table1 GROUP BY DAY(created_date)
UNION ALL
SELECT DATE(created_date) created_date, NULL, COUNT(*) countTable2
FROM table2 GROUP BY DAY(created_date)) t GROUP BY t.created_date
You have a problem in your queries, you are grouping by DAY(date) and showing 'date' so the result will be first date with day(date), yet repeating it to avoid misunderstanding :)
select IFNULL(A.cd, B.cd), A.cnt, B.cnt from
(SELECT DAY(created_date) d, DATE(created_date) cd, COUNT(*) cnt
FROM table1 GROUP BY DAY(created_date)) as A
LEFT JOIN
(SELECT DAY(created_date) d, DATE(created_date) cd , COUNT(*) cnt
FROM table2 GROUP BY DAY(created_date)) B ON B.d = A.d
Its not too hard just use union if no need to allow duplicate row else use union all for all(means allow duplicate as well).
SELECT DATE(created_date), COUNT(*) FROM table1 GROUP BY DAY(created_date)
UNION
SELECT DATE(created_date), COUNT(*) FROM table2 GROUP BY DAY(created_date)
SELECT T.create_date,ISNULL(T.count,0)AS Counttable1,ISNULL(X.count,0)AS Counttable2 FROM(SELECT DATE(created_date) AS create_date,COUNT(*) as count FROM table1 GROUP BY DAY(created_date)) AS T LEFTJOIN(SELECT DATE(created_date) AS create_date, COUNT(*) as count FROM table2 GROUP BY DAY(created_date))AS X ON T.create_date=X.create_date
You actually need a SQL UNION. JOIN natuarually eliminate counts becuase the maytch fields. I.e. if you had 2016-12-01 in both table1 andtable2 then a JOIN on created_date would give you a count of 1 instead of a count of 2.
SELECT DATE(total.created_date), COUNT(*)
FROM (
SELECT created_date FROM table1
UNION ALL
SELECT created_date FROM table2) as total
GROUP BY total.created_date
HERE you simply union the two tables since they have a matching column name. Then you get back every date from both tables. That is in the inner query. The outer query then does the counting.
Hope that makes sense.

SQL count without group by

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.

count number of rows using where

m new in mysql
here is my table
now i want to count "count_id" where count of 'questionID' greater than 2
Try this :
SELECT COUNT(count_id) FROM myTable WHERE questionID > 2
select count(Count_ID),QuestionID,SurveyId from table
where QuestionID>2
group by QuestionID,SurveyID
select count(count_id) from yourtable where questionID > 2
If you want to count unique ID:
select count(DISTINCT count_id) from table_name where questionID > 2
SELECT COUNT(count_id) FROM table_name WHERE questionID > 2
Group by Count_ID and count their distinct questions. Stay with those that have more than two. Then count how many IDs you got.
select count(*)
from
(
select count_id
from mytable
group by count_id
having count(distinct questionid) > 2
) x;
EDIT: If count_id + questionid happen to be unique for the table, you can replace count(distinct questionid) with count(*).
You can also try the statement below:
select count(count_id) CountOfID,count_id from mytable
where questionID > 2 group by count_id;

UNION All, Group BY and then get overall COUNT

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

SQL count of return users

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