Reputation: 1194
I need to find duplicates from a column in a table based on user_id how much they did a transaction based on borrow_id
So the table looks like:
borrow_progress
+--------+-----------+------------+------------+
|user_id | borrow_id | state | remark |
+--------+-----------+------------+------------+
| 170 | 236 | 10 | waiting |
| 170 | 236 | 22 | proceed |
| 170 | 236 | 26 | success |
| 170 | 236 | 30 | sent |
| 172 | 237 | 10 | waiting |
| 172 | 237 | 22 | proceed |
| 172 | 237 | 90 | fail |
| 170 | 238 | 10 | waiting |
| 170 | 238 | 22 | proceed |
| 170 | 238 | 90 | fail |
| 173 | 239 | 10 | waiting |
| 173 | 239 | 22 | proceed |
| 173 | 239 | 26 | success |
| 170 | 240 | 10 | waiting |
| ... | ... | ... | ... |
+--------+-----------+------------+------------+
My expected outputs are:
1.
+--------+------------------+
|user_id | count(borrow_id) |
+--------+------------------+
| 170 | 3 |
| 172 | 1 |
| 173 | 1 |
+--------+------------------+
2.
+--------+------------------+
|user_id | borrow_id |
+--------+------------------+
| 170 | 236 |
| 170 | 238 |
| 170 | 240 |
| 172 | 237 |
| 173 | 239 |
+--------+------------------+
Upvotes: 1
Views: 42
Reputation: 4061
I gather these are the two queries
select user_id, count(distinct borrow_id )
from borrow_progress
group by user_id
select user_id, borrow_id
from borrow_progress
group by user_id, borrow_id
Upvotes: 0
Reputation: 522762
You can make use of DISTINCT
here, for both queries:
SELECT
user_id,
COUNT(DISTINCT borrow_id) AS cnt
FROM borrow_progress
GROUP BY
user_id
ORDER BY
user_id;
And:
SELECT DISTINCT
user_id,
borrow_id
FROM borrow_progress
ORDER BY
user_id,
borrow_id;
Upvotes: 1