OrderBy and sort with 2 tables with doctrine, SQL - sql

I have 2 tables:
User:
id
lastname
role_id
1
UserA
2
2
UserB
1
3
UserD
1
4
UserC
2
Role:
id
name
1
ROLE_B
2
ROLE_A
I would like to order and sort the users with ROLE_B first and alphabeticaly then the rest alphabeticaly, the result should be like this:
id
lastname
role_id
2
UserB
1
3
UserD
1
1
UserA
2
4
UserC
2
I tried with
$qb = $this->createQueryBuilder('user')
->addSelect('roles')
->addSelect('(CASE WHEN roles.name = :firstRole THEN 1 ELSE 0 END) AS HIDDEN orderByRole')
->leftJoin('user.roles', 'roles')
->setParameters(
[
'firstRole' => EnumRole::ROLE_B
]
)
->addOrderBy('orderByRole', 'ASC')
->addOrderBy('user.lastname', 'ASC')
;
But it always orders alphabeticaly by lastname only
If someone know how to do that with doctrine or in SQL, thanks

Related

Query to fetch user names according to a condition from another table (SQL)

I have two tables:
user
id
full_name
is_admin
is_active
1
Alan
0
1
2
Carl
0
1
3
Any
0
1
4
Jane
0
1
5
Marry
0
1
6
Pedri
0
1
7
admin
1
1
8
Mota
0
0
approver
id
subordinate_id
leader_id
main_leader_id
is_active
1
1
2
3
0
2
4
5
6
1
3
1
2
4
0
(subordinate_id, leader_id and main_leader_id are foreign keys that correspond to the id column of the user table)
I would like to perform a query that brings all user names that are not admin (user table is_admin=0) and that are active (user table is_active=1), and that if they have the id in the subordinate_id column in the approver table that only brings the name of that user that has the is_active of the approver table = 0.
That is, I would like to bring users that if they have any record as subordinate_id that only bring me those that are not active in the approver table.
I tried to get the data in the following way:
SELECT
full_name
FROM user AS U
LEFT JOIN approver AS A
ON U.id = A.subordinate_id
WHERE
A.id is null
AND
U.is_admin = 0
AND
U.is_active = 1
But with this query i only get the user name that not has a register in the approver table,
and in my case i want to get the user that have a register in the approver table as subordinate_id, but not if the register have the column 'is_active' equal to 1.
In my final result I could get something like this:
Alan
carl
any
marry
Pedri
In order to make this working, you should split the conditions in the WHERE clause into:
"user" conditions: is_admin = 0 AND is_active = 1
"approver" conditions: is not a subordinate OR is_active = 0
These two groups of conditions have to be set in AND.
SELECT DISTINCT user_.id, user_.full_name
FROM user_
LEFT JOIN approver
ON user_.id = approver.subordinate_id
WHERE (user_.is_admin = 0 AND user_.is_active = 1)
AND (approver.id IS NULL OR approver.is_active = 0)
Check the demo here.
Note: the DISTINCT keyword is necessary because the JOIN operation is made between two tables having cardinality 1:n.

How to get unique id's data among the same score

The records in the table likes:
user score label
----------------------------------------------
userA 2 apple
userA 2 banana
userB 5 cat
userB 5 dog
For different rows, if the user and score are same, it just need to get the unique user and score, and any label is fine.
The expected query result from above is example can be any one as below:
user score label
----------------------------------------------
userA 2 apple
userB 5 cat
or
user score label
----------------------------------------------
userA 2 banana
userB 5 cat
or
user score label
----------------------------------------------
userA 2 apple
userB 5 dog
or
user score label
----------------------------------------------
userA 2 banana
userB 5 dog
if any label is fine, I think the easiest way is to use an aggregation function like min or max --
SELECT user, score, max(label) as label
FROM table
GROUP BY user, score
you can use window function:
select * from
(
select * , row_number() over (partition by user,score order by label) rn
from tablename
) t
where t.rn = 1;

Laravel count and return 0

Table A:
id name
1 Apple
2 Orange
Table B:
id table_a_id
1 1
2 1
3 1
How can i return like? :
Name count
Apple 3
Orange 0
I only got Apple =3 from my join sql, how can i count the orange with 0 result?
Here is my sql:
A::select('A.name', DB::raw('COUNT(B.table_a_id) AS count'))
->leftJoin('B', 'a.id', '=', 'B.table_a_id')
->groupBy('A.name','B.table_a_id')
->get();
$data = DB::table('A')
->join('B','A.id','=','B.table_a_id')
->select('name',DB::raw('COUNT(table_a_id)')->groupBy('A.id')->get();

Count and Grouping 2 column in a table

This is my table and data
id | owner | buyer
1 1 3
2 2 2
3 1 2
I want the result to be like this
user | totals
2 3
1 2
3 1
User field means owner and buyer.
Hope you all are understand.
Thanks ~
You can do this using union all and group by:
select user, count(*)
from ((select owner as user from t
) union all
(select buyer from t
)
) ob
group by user
order by user;

Twitter style following-follower table in SQL-2

Addition to this Twitter style following-follower table in SQL
In the upper example I was able to return the query for "if userB is following the other users who follows him"
I need a simpler thing now. I tried to modify the original code without any luck. I simply need "if userB is following or not any of the users , regardless if they follow him. Currently it returns null for people who dont follow that particular user.
This is the sample table for users
id user_id
1 userA
2 userB
3 userC
4 userD
5 userE
This is the sample table for follows
id user_id follower_id
1 userA userB
2 userD userB
I like to return a result that includes this follower/following state. For example for userB (he follows A and D but not C and E:
id followedBy areWeFollowing
1 userA 1
2 userB 0
3 userC 0
4 userD 1
5 userE 0
Thanks for your help!
arda
you can even take a count also from second table for this situation -
select id,user_id, ( select count(*)
from follows
where follows.user_id = users.user_id
and follows.follower_id = 'userB') as areWeFollowing
from users