Select Count Distinct the value to get unique data - sql

i'm very new in sql and dont have an idea for this even simple one.
table 1 bos_report_users contain user_id,dept,branch_code table 2 bos_report_access contain user_id,report_id
i would like join this table so the result should be something like this
dept | user_id|report_id|.
this is what i have done and result is not what i'm expected.
SELECT A.USER_ID,B.REPORT_ID
FROM(SELECT DISTINCT DEPT FROM BOS_M_USERS WHERE DEPT='FINANCE'),BOS_REPORT_ACCESS B ,BOS_M_USERS A
WHERE A.USER_ID='SLTAM'
kindly assist me everyone. thanks for helping.

The join is performed through the shared column user_id
SELECT
u.user_id, u.dept, u.branch_code
, a.report_id
FROM bos_report_users u
INNER JOIN bos_report_access a ON u.user_id = a.user_id
WHERE u.user_id = 'SLTAM'
ORDER BY u.dept, u.user_id
alias "u" = users
alias "a" = access

Related

Select name from SQL column

I am trying to select the names (along with the rest of the information on the table such as data, time, lesson) of each user that is inside a users table. The names I'm trying to select are the ones that are on the same row as a particular teacherID. However when I run this code I'm getting the same name for all records, when I should be getting two different ones.
SELECT FName, SName, bookedLessons.bookedHour,
bookedLessons.bookedDate, bookedLessons.lesson
FROM users JOIN bookedLessons
WHERE teacherID = 11
AND users.userID = (SELECT userID FROM bookedLessons WHERE teacherID = 11)
Your join needs an ON clause but not the subquery in the WHERE clause:
SELECT u.FName, u.SName, b.bookedHour, b.bookedDate, b.lesson
FROM bookedLessons b INNER JOIN users u
ON u.userID = b.userID
WHERE b.teacherID = 11

Remove duplicates from result in sql

i have following sql in java project:
select distinct * from drivers inner join licenses on drivers.user_id=licenses.issuer_id
inner join users on drivers.user_id=users.id
where (licenses.state='ISSUED' or drivers.status='WAITING')
and users.is_deleted=false
And result i database looks like this:
And i would like to get only one result instead of two duplicated results.
How can i do that?
Solution 1 - That's Because one of data has duplicate value write distinct keyword with only column you want like this
Select distinct id, distinct creation_date, distinct modification_date from
YourTable
Solution 2 - apply distinct only on ID and once you get id you can get all data using in query
select * from yourtable where id in (select distinct id from drivers inner join
licenses
on drivers.user_id=licenses.issuer_id
inner join users on drivers.user_id=users.id
where (licenses.state='ISSUED' or drivers.status='WAITING')
and users.is_deleted=false )
Enum fields name on select, using COALESCE for fields which value is null.
usually you dont query distinct with * (all columns), because it means if one column has the same value but the rest isn't, it will be treated as a different rows. so you have to distinct only the column you want to, then get the data
I suspect that you want left joins like this:
select *
from users u left join
drivers d
on d.user_id = u.id and d.status = 'WAITING' left join
licenses l
on d.user_id = l.issuer_id and l.state = 'ISSUED'
where u.is_deleted = false and
(d.user_id is not null or l.issuer_id is not null);

1052: Column 'user_id' in field list is ambiguous when

I have 3 tables which share a user id, now I want to get data from all 3 but I don't know where I'm doing it wrong in this query
SELECT user_id, first_name, image_id, description, gender
FROM users a
JOIN user_services b ON b.user_id = a.user_id
JOIN user_timeframe c ON c.user_id = a.user_id
Two key best practices:
Always qualify all column references.
Use table aliases that are meaningful rather than arbitrary letters.
So:
SELECT u.user_id, u.first_name, ?.image_id, ?.description,
u.gender
FROM users u JOIN
user_services us
ON us.user_id = u.user_id JOIN
user_timeframe ut
ON ut.user_id = u.user_id;
The ? is because it is not clear what table those columns come from. (The other columns are guesses so they might not be right either.)
The preferred way should be explicit aliasing. There is another way to fix this query by using JOIN ... USING syntax:
SELECT user_id, first_name, image_id, description, gender
FROM users a
JOIN user_services b USING(user_id)
JOIN user_timeframe c USING(user_id);
db<>fiddle demo

SQL Where on different table

SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
Teacher_id being the session id,
I want to see all the students that have the same mentor.
Right now if I run this I just see all of the students twice, maybe one of you knows why?
My db scheme
You are not specifying on which columns you want to do the join, so you're getting a cross reference where all records are joined to all records.
You should do something like (not sure about your column names):
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id

Left Outer Join with subqueries?

----------
User
----------
user_ID(pk)
UserEmail
----------
Project_Account
----------
actno
actname
projno
projname
ProjEmpID
ProjEmpMGRID
Where ProjEmpID,ProjEmpMGRID is the user_id and ProjEmpMGRID can be null.
I need to look up the useremail and display the table project_account. I need to query with actNo which has duplicate values.
My query goes like this:
select projno,projname,actno,actname,
(select u.user_email as project_manager from project_account c left outer join users u
on u.user_id = c.ProjEmpID where actno='some no')as project_manager,
(select u.user_email as program_manager from project_account c left outer join users u
on u.user_id = c.ProjEmpMGRID where actno='someno') as program_manager
from project_account where actno='someno'
The error message I get in Oracle:
ora-01427 single row subquery returns
more than one row
As my subquery returns more than one email id, I get this error. As I said, act no is not unique. I could understand the error, but I couldn't figure out the solution. I am doing a left outer join in a subquery because there might be nulls in prog manager id.
Any help would be appreciated.
The error you are getting is that one of your subqueries (either for project_manager or program_manager) is giving you back more than one ID based on your conditions. This kind of makes sense, since multiple project accounts could have the same "actno" since you haven't specified that as a Primarky Key (pk)
furhter, rather than using subqueries, just join directly to the user tables to find the IDs
select projno,projname,actno,actname,
project_user.user_email as project_manager,
program_user.user_email as program_manager
from project_account
left join User as project_user
on project_account.ProjEmpID = project_user.user_id
left join User as program_user
on project_account.ProjEmpMGRID = program_user.user_id
where actno='someno'
What about something like:
select c.projno, c.projname, c.actno, c.actname, u.user_email as project_manager, us.user_email as program_manager
from project_account c
left outer join users u
on u.user_id = c.ProjEmpID
left outer join users us
on us.user_id = c.ProjEmpMGRID
WHERE actno = 'someno'
This way you aren't running subqueries and returning multiple results and trying to store them as one value.
Why don't you simply use this?
select projno, projname, actno, actname, (select user_email from users where user_id = pa.projempid), (select user_email from users where user_id = pa.projempmgrid)
from project_account pa