So here is the structure and query: http://www.sqlfiddle.com/#!9/de7ec8/4
And this is the result:
+-------------+--------+
| id_requests | status |
+-------------+--------+
| 1 | 0 |
| 1 | 0 |
| 1 | 1 |
| 2 | 1 |
+-------------+--------+
4 rows in set
and im looking for this result:
+-------------+--------+
| id_requests | status |
+-------------+--------+
| 1 | 0 |
| 1 | 1 |
| 2 | 1 |
+-------------+--------+
3 rows in set
I want to group by status but for each id_requests, any help will be appreciated.
Seems you are looking for selecting distinct rows for this use distinct clause
SELECT distinct a.id_requests, b.status
FROM `admin_availability_requests` a
Related
I have a table with this structure
+----------+----------+
| user_id | tema_id |
+----------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 2 |
| 6 | 3 |
| 7 | 1 |
+----------+----------+
What I want to get in only one query is the total of different tema_id by tema_id. I have this query but it returns the different tema_id but the count column to one instead of the total of that tema_id.
SELECT tema_id, COUNT(DISTINCT(tema_id)) as total
FROM push_subscriptions
GROUP BY tema_id
Return this:
+----------+----------+
| tema_id | total |
+----------+----------+
| 1 | 1 | -> must be 3
| 2 | 1 | -> must be 2
| 3 | 1 | -> must be 2
+----------+----------+
Thank you
A simple count(*) should do the trick:
select tema_id, count(*) as total
from push_subscriptions
group by tema_id;
Fiddle
I have 2 tables, follow and following
follow
+---------+----------------+
| user_id | follow_user_id |
+---------+----------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 100 | 10 |
+---------+----------------+
following
+---------+-------------------+
| user_id | following_user_id |
+---------+-------------------+
| 1 | 2 |
| 3 | 4 |
| 4 | 6 |
| 200 | 500 |
+---------+-------------------+
I want to concat 2 tables without duplicate.
Here is the result that I want.
+---------+----------------+-----------+
| user_id | target_user_id | category |
+---------+----------------+-----------+
| 1 | 2 | follow |
| 2 | 3 | follow |
| 3 | 4 | follow |
| 4 | 6 | following |
| 100 | 10 | follow |
| 200 | 500 | following |
+---------+----------------+-----------+
Condition 1 - Remove duplicated row
Condition 2 - Have to add category column with each table's name
Condition 3 - If category is duplicated, it can be follow or following. it doesn't matter.
Condition 4 - follow_user_id as target_user_id and following_user_id as target_user_id
In this case, should I have to use join? or union?
Any suggestion, very appreciate.
Thanks!
Just use union and group by, the SQL as below:
select
user_id,target_user_id,min(tag) as category
from
(
select user_id,follow_user_id as target_user_id, 'follow' as tag from follow
union
select user_id,following_user_id as target_user_id, 'following' as tag from following
) tmp
group by
user_id,target_user_id
order by
user_id,target_user_id;
+---------+----------------+-----------+
| user_id | target_user_id | category |
+---------+----------------+-----------+
| 1 | 2 | follow |
| 2 | 3 | follow |
| 3 | 4 | follow |
| 4 | 6 | following |
| 100 | 10 | follow |
| 200 | 500 | following |
+---------+----------------+-----------+
6 rows in set (0.00 sec)
I have a complicated ordering issue in my query.
Raw, Unordered Data:
+------+--------+-----------+
| id | job_id | action_id |
+------+--------+-----------+
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 3 | 1 | 1 |
| 4 | 2 | 3 |
| 5 | 4 | 1 |
| 6 | 1 | 2 |
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| 9 | 4 | 2 |
+------+--------+-----------+
Required Ordering:
+------+--------+-----------+
| id | job_id | action_id |
+------+--------+-----------+
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| | | | * blank lines added for clarity,
| 5 | 4 | 1 | not desired in actual data
| 9 | 4 | 2 |
| | | |
| 3 | 1 | 1 |
| 6 | 1 | 2 |
| | | |
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 4 | 2 | 3 |
+------+--------+-----------+
The theory behind this ordering:
the largest id is the most recently added entry
the most recent id with action_id of 1
followed by the entries with ascending action_ids that have the same job_id
then the next most recent action_id of 1
ad infinitum
EDIT: I'm not able to add columns to the table in order to aid in sorting, as I've seen in some other solutions to ordering questions.
Any help is greatly appreciated!
My best shot is this:
SELECT * FROM tbl
ORDER BY FIND_IN_SET(job_id,
(SELECT GROUP_CONCAT(job_id ORDER BY ID DESC)
FROM tbl WHERE action_id = 1));
I didn't find a way to do it easily, What do you think of the following code :
select c.id, c.job_id, c.action_id
from (select a.id, a.job_id, a.action_id, min(b.id) as related_id
from myTable a
inner join myTable b
on a.job_id=b.job_id
group by a.job_id) c
group by c.id
order by c.related_id desc, c.action_id
+------+---------+
| id | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
| 3 | 3 |
| 3 | 4 |
+------+---------+
i want to select count id where have a same value, so the result be, id 1 have 4 same value, id 2 have 2 same value, id 3 have 3 same value .
+------+
| id |
+------+
| 4 |
| 2 |
| 3 |
+------+
thanks for help, master.
SELECT id, COUNT(object) FROM tablename GROUP BY id
SELECT COUNT( * ) FROM `test` GROUP BY id
Take for example this table (let's call it BIN_TABLE):
+------+------+
| A | B |
+------+------+
| 0 | 0 |
| 0 | 1 |
| 1 | 1 |
| 1 | 0 |
+------+------+
I want to roll it up, so I do:
SELECT A, B, COUNT(*)
FROM BIN_TABLE
GROUP BY A, B WITH ROLLUP;
And I get:
+------+------+----------+
| A | B | COUNT(*) |
+------+------+----------+
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | NULL | 2 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| 1 | NULL | 2 |
| NULL | NULL | 4 |
+------+------+----------+
This is an example of how WITH ROLLUP uses the order of the fields I put in the GROUP BY clause.
I would like to also have the following lines in the result:
| NULL | 1 | 2 |
| NULL | 0 | 2 |
Which would mean that I have all of the rolled-up permutations.
Is this possible to do without resorting to this:
SELECT A, B, COUNT(*)
FROM BIN_TABLE
GROUP BY A, B WITH ROLLUP
UNION
SELECT NULL, B, COUNT(*)
FROM BIN_TABLE
GROUP BY B
(I use MySQL 5.6, if it matters)
No, I believe UNION is the only way. You could however use UNION ALL (rather than the implicit UNION DISTINCT) to save from needlessly searching for duplicates.
According the function of roll-up modifier, there are already all rolled-up permutations in your results.
Please look at these results.
+------+------+----------+
| A | B | COUNT(*) |
+------+------+----------+
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | NULL | 2 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| 1 | NULL | 2 |
| NULL | NULL | 4 |
+------+------+----------+
Here, 3rd record and 6th record are the rolled-up permutations.
So we can get the result by a following sql.
SELECT * from
(select a,b,count(*) from
bin_table group by a,b with rollup) total
order by total.b desc,a;
+------+------+----------+
| a | b | count(*) |
+------+------+----------+
| 0 | 1 | 1 |
| 1 | 1 | 1 |
| 0 | 0 | 1 |
| 1 | 0 | 1 |
| NULL | NULL | 4 |
| 0 | NULL | 2 |
| 1 | NULL | 2 |
+------+------+----------+
7 rows in set (0.01 sec)
Here, 6th record and 7th record are the rolled-up permutations.
And I think that it will make the performance better.
Thanks,