mysql group by but only group if second row is the same - mysql

im wondering what the smartest way is to group my mysql results... I have the following table structure:
- id
- userId
- status (values from 1-100)
Lets say with the following content:
1 | 1 | 10
2 | 1 | 10
3 | 1 | 15
4 | 2 | 15
5 | 3 | 10
Now I want to group all results by user but only for each status. So the results im looking for should be:
1 | 1 | 10
3 | 1 | 15
4 | 2 | 15
5 | 3 | 10
Hope you understand want im looking for...
Best
Tassilo

If you need the id, then a GROUPing query is needed; this will produce the results you shown:
SELECT MIN(id), userId, status
FROM your_table
GROUP BY userId, status
;
If you don't need the id, then GROUPing is not the best tool, use DISTINCT instead; like so:
SELECT DISTINCT userId, status
FROM your_table
;

The topic of this question say "Group only if next row is the same" in that case I would do something like this:
create table USER_(id integer, UserId integer, status integer);
insert into USER_ values(1,1,10);
insert into USER_ values(2,1,10);
insert into USER_ values(3,1,115);
insert into USER_ values(4,2,115);
insert into USER_ values(5,3,10);
insert into USER_ values(6,1,10);
select min(a.id)as id, a.userId, a.status ,count(*) from USER_ a join USER_ b
on a.userid = b.userid and a.id = b.id-1 group by a.userId,a.status;
id | userid | status | count
-----+--------+--------+-------
1 | 1 | 10 | 2
If I look at the explanation for the question here then, I would do something like this:
select min(a.id) as id, a.userId, a.status from USER_ a
group by a.userId,a.status order by a.userid,status;
id | userid | status
----+--------+--------
1 | 1 | 10
3 | 1 | 15
4 | 2 | 15
5 | 3 | 10
Please correct if I have a wrong understanding of the question

Related

Select all rows with multiple occurrences - on same day

I have a single MySQL table with the name 'checkins' and 4 columns.
id | userIDFK | checkin_datetime | shopId
------------------------------------------------
1 | 1 | 2018-01-18 09:44:00 | 3
2 | 2 | 2018-01-18 10:32:00 | 3
3 | 3 | 2018-01-18 11:19:00 | 3
4 | 1 | 2018-01-18 17:57:00 | 3
5 | 1 | 2018-01-18 16:31:00 | 1
6 | 1 | 2018-01-19 08:31:00 | 3
Basically I want to find rows where users have checked-in more than once (>=2) on the same day and the same shop. So for instance if a user checks-in as in rows with ids 1 and 4 (same user, same day, same shop), the query should return a hit with the the entire rows (id, userIDFK, checkin_datetime, shopId). Hope this makes sense.
I already tried using
SELECT id, userIDFK, checkin_datetime, shopId
FROM (
SELECT * FROM 'checkins' WHERE COUNT(userIDFK)>=2 AND COUNT(shopId)>=2
)
The same day part I have no clew how to do it, and I know this query is way off, but this is the best I could.
You can try grouping by userId checkin_date and shopID
SELECT userIDFK, checkin_datetime, shopId,COUNT(SHOPiD)
FROM checkins
GROUP BY userIDFK, DATE(checkin_datetime), shopId
HAVING COUNT(SHOPID)>1
EDIT
You can include a subquery to get all lines:
select b.id,b.userIDFK, b.checkin_datetime, b.shopId
from checkins b
where (SELECT COUNT(SHOPiD)
FROM checkins a
where a.userIDFK=b.userIDFK and date(a.checkin_datetime)=date(b.checkin_datetime) and a.shopId=b.a.shopId
GROUP BY userIDFK, DATE(checkin_datetime), shopId)>1
GROUPBY can be used to get the multiple occurrences.
SELECT id, userIDFK, checkin_datetime, shopId
FROM checkins
GROUP BY userIDFK, DATE(checkin_datetime), shopId
HAVING count(id) > 1;
Hope it helps!
EDIT:
Using inner join you can achieve it. Here is the query:
SELECT c1.* FROM checkins c1 INNER JOIN checkins c2
ON c1.userIDFK = c2.userIDFK
AND date(c1.checkin_datetime) = date(c2.checkin_datetime)
AND c1.shopId = c2.shopId
AND c1.id != c2.id
Cheers!!

How can I identify if a certain row is the last by using an ID in MySQL

Sorry to confuse you about my title. I am building an auction system and I am having a difficulty in getting the user's winning item.
Example I have a table like this:
the columns are:
id, product_id, user_id, status, is_winner, info, bidding_price, bidding_date
here's my sql fiddle:
http://sqlfiddle.com/#!9/7097d/1
I want to get every user's item that they already win. So I need to identify if they are the last who bid in that item.
I need to filter it using a user_id.
If I do a query like this:
SELECT MAX(product_id) AS product_id FROM auction_product_bidding
WHERE user_id = 3;
it will get only the product_id that is 12 and the product_id of 9 did not get. Product ID 9 is also that last bid of the user_id 3.
Can you help me? I hope you got my point. Thanks. Sorry if my question a little bit confusing.
According to your question, seems 11 is also what you want, try this query:
SELECT apd.product_id
FROM auction_product_bidding apd
JOIN (
SELECT MAX(bidding_date) AS bidding_date, product_id
FROM auction_product_bidding
GROUP BY product_id
) t
ON apd.product_id = t.product_id
AND apd.bidding_date = t.bidding_date
WHERE apd.user_id = 3;
Check Demo Here
select id,product_id,user_id,status,is_winner,info,bidding_price,bidding_date,rank
from
( SELECT apb.*,
greatest(#rank:=if(product_id=#prodGrp,#rank+1,1),-1) as rank,
#prodGrp:=product_id as dummy
FROM auction_product_bidding apb
cross join (select #prodGrp:=-1,#rank:=0) xParams
order by product_id,bidding_date DESC
) xDerived
where user_id=3 and rank=1;
That user won 9,11,12
+----+------------+---------+--------+-----------+------+---------------+---------------------+------+
| id | product_id | user_id | status | is_winner | info | bidding_price | bidding_date | rank |
+----+------------+---------+--------+-----------+------+---------------+---------------------+------+
| 60 | 9 | 3 | | 0 | | 75000.00 | 2016-08-02 16:31:23 | 1 |
| 59 | 11 | 3 | | 0 | | 15000.00 | 2016-08-02 12:04:16 | 1 |
| 68 | 12 | 3 | | 0 | | 18000.00 | 2016-08-10 09:20:01 | 1 |
+----+------------+---------+--------+-----------+------+---------------+---------------------+------+
SELECT product_id FROM auction_product_bidding where bidding_price= any
(select max(bidding_price) from auction_product_bidding group by product_id)
and user_id='3';
select * from
(select product_id,user_id,max(bidding_price) from
(select * from auction_product_bidding order by bidding_price desc) a
group by product_id) b
where user_id=3;
Answer:
product_id user_id max(bidding_price)
9 3 75000
11 3 15000
12 3 18000
An idea could be to sort the table desc by date and select every distinct row by product_id and customer_id. Something like
SELECT DISTINCT prod_id, user_id FROM (
SELECT * FROM auction_product_bidding ORDER BY date DESC
)
You want everything that bids last in 3, is it right ?

getting maximum number of occurrences along with count in group by

I have a table which looks like this
question_id response
1 yes
1 yes
2 agree
1 no
3 disagree
2 agree
2 disagree
I want my query to return
question_id max_response max_response_count total_responses
1 yes 2 3
2 agree 2 3
3 disagree 1 1
The query
SELECT question_id,
max(response),
count(max(response)),
count(*)
FROM response
GROUP BY question_id
gives Invalid use of group function error.
Which query should I place to get the above output.
Try this
SELECT T.question_id,Max(MR) max_response,MAX(CR)max_response_count,(Select Count(question_id) from response where question_id = T.question_id)total_responses
FROM
(SELECT question_id,max(response) MR,count(response) CR
FROM response group by question_id,response
)T
GROUP BY T.question_id
Fiddle Demo
OP:
+--------------------------------------------------------------------+
|QUESTION_ID | MAX_RESPONSE | MAX_RESPONSE_COUNT |TOTAL_RESPONSES |
+--------------------------------------------------------------------+
| 1 | yes | 2 | 3 |
| 2 | disagree | 2 | 3 |
| 3 | disagree | 1 | 1 |
+--------------------------------------------------------------------+
SELECT count(*) AS total_responses ,
question_id ,
count(max(response)) AS max_response_count
FROM response
GROUP BY question_id ,
response
I have updated my query by looking your sqlfiddle data. please try now:
create table tableA(question_id int,response varchar(50));
insert into tableA values(1,'yes'),(1,'yes'),(2,'agree'),(1,'no'),(3,'disagree'),(2,'agree'),(2,'disagree');
SELECT question_id,
max(response) AS max_response,
(select count(response) from tableA b where a.response= b.response and a.question_id= b.question_id ) as max_response_count,
count(*) AS count_response
FROM tableA a
GROUP BY question_id
I tried and get the result as expected..

How to return multiple rows when compared to multiple values?

So my current table looks like this :
| UserID | QuestionID | GameID |
| 1 | 30 | 2 |
| 1 | 30 | 3 |
| 2 | 30 | 4 |
| 3 | 30 | 2 |
| 4 | 30 | 3 |
How would I return all the rows where the same UserID has a value for GameID 1, 2, 3 and 4? Basically trying to return all the users who have played Game 1, 2, 3 and 4
Assuming no duplicates of game id..
Select userID
from table
where gameID in (1,2,3,4)
group by UserID
having count(GameID) = 4
If I get the answer correct, it might be:
SELECT p.* FROM poll AS p WHERE p.UserID = <your_user_id> AND p.GameID IN (1,2,3,4)
or you are seeking for:
SELECT p.*, GROUP_CONCAT(p.GameID) FROM poll AS p WHERE p.UserID = <user_id> GROUP BY p.GameID
Assuming the tables are normalized and joined properly, simply use a WHERE clause:
WHERE colname = value1 OR colname = value2 ...
or use the In operator
WHERE colname = In (value,value...)
http://beginner-sql-tutorial.com/sql-logical-operators.htm
SELECT
UserID
FROM (
SELECT
UserID,
GROUP_CONCAT(GameID ORDER BY GameID) as Games
FROM some_table
GROUP BY UserID
) AS baseview
WHERE Games="1,2,3,4"
Please understand, this is a performance killer - it creates the GROUP_CONCAT for all users.

How to get total occurrence of a value within its own query?

Let's say we have this query
SELECT * FROM table
And this result from it.
id | user_id
------------
1 | 1
------------
2 | 1
------------
3 | 2
------------
4 | 1
How could I get the count of how often a user_id appears as another field (without some major SQL query)
id | user_id | count
--------------------
1 | 1 | 3
--------------------
2 | 1 | 3
--------------------
3 | 2 | 1
--------------------
4 | 1 | 3
We have this value currently in code, but we are implementing sorting to this table and I would like to be able to sort in the SQL query.
BTW if this is not possible without some major trick, we are just going to skip sorting on that field.
You'll just want to add a subquery on the end, I believe:
SELECT
t.id,
t.user_id,
(SELECT COUNT(*) FROM table WHERE user_id = t.user_id) AS `count`
FROM table t;
SELECT o.id, o.user_id, (
SELECT COUNT(id)
FROM table i
WHERE i.user_id = o.user_id
GROUP BY i.user_id
) AS `count`
FROM table o
I suspect this query as not being a performance monster but it should work.