I ahve a problem with an sql case, I want to check a column value, and if that returns true then I want another column value to change, but in a 'case' you can only check one column and change only the value of that, how do I make it work?
select f.[film name],f.city,p.[participant name],
case rt.[type name]
when 'director' then 'director' else null
end 'role type'
from Films f
join FilmParticipants fp on fp.filmID=f.filmID
join Participants p on p.participantID=fp.participantID
join RoleType rt on rt.roleID=fp.roleID
where f.city in(
select f.city
from Films f
group by f.city
having COUNT(*)>1)
order by f.city
it gives this table:
film name | city | participate name | role type
Shrek | London | John | null
Shrek | London | John | null
Dragon | London | Rick | null
Drago | London | Morty | Director
now I want that whenever there is null in the 'role type column' that the 'participate name' column will be null as well.
You could use case when same way for role type and show partecipant.name
select f.[film name],f.city,
case rt.rt.[type name]
when 'director' then p.[participant name] else null end 'partecipant',
case rt.[type name]
when 'director' then 'director' else null
end 'role type'
from Films f
join FilmParticipants fp on fp.filmID=f.filmID
join Participants p on p.participantID=fp.participantID
join RoleType rt on rt.roleID=fp.roleID
where f.city in(
select f.city
from Films f
group by f.city
having COUNT(*)>1)
order by f.city
for remove the null
select [film name], city, max(partecipant), max([role type])
from (
select f.[film name],f.city,
case rt.rt.[type name]
when 'director' then p.[participant name] else null end 'partecipant',
case rt.[type name]
when 'director' then 'director' else null
end 'role type'
from Films f
join FilmParticipants fp on fp.filmID=f.filmID
join Participants p on p.participantID=fp.participantID
join RoleType rt on rt.roleID=fp.roleID
where f.city in(
select f.city
from Films f
group by f.city
having COUNT(*)>1)
order by f.city ) t
group by [film name], city
Related
I got a table people:
ID name surname mother_id father_id
--------------------------------------------
1 John smith null null
2 kate m null null
3 philip smith 2 1
4 dimitr smith 7 3
etc.
I tried this query.
SELECT
p.name, P.surname,
c.name as 'Mothers name', c.surname as 'Mothers surname',
gm.name as 'Grandmother name', gm.surname as 'Grandmother surname'
FROM
dbo.people AS p
JOIN
dbo.people AS c ON (c.mother = p.Id)
JOIN
dbo.people AS gm ON (gm.mother = c.Id)
But it doesn't really return what I expected
Problem with ON clause :
select p.name, p.surname,
coalesce(pm.name, '') as mothername, coalesce(pm.surname, '') as mothersurname,
coalesce(pf.name, '') as grandmothername,
coalesce(pf.surname, '') as grandmothersurname
from people p left join
people pm
on pm.id = p.mother_id left join -- use mother_id from people p
people pf
on pf.id = p.father_id; -- use father_id from people p;
Here is db-fiddle.
I have 3 tables: money, student, faculty. This query returns each faculty and highest stipend in each one of them.
select
f.name as "FACULTY_NAME",
max(stipend) as "MAX_STIPEND"
from
money m, student s
inner join
faculty f on f.id_faculty = s.faculty_id
where
m.student_id = s.id_student
group by
f.id_faculty, f.name;
Query works fine:
FACULTY_NAME | MAX_STIPEND
-----------------+---------------
IT Faculty | 50
Architecture | 60
Journalism | 40
However when I add s.name to original query to also show the name of the student who received max_stipend, query is not working like it used to - it returns all of the students
select
f.name as "FACULTY_NAME",s.name,
max(stipend) as "MAX_STIPEND"
from
money m, student s
inner join
faculty f on f.id_faculty = s.faculty_id
where
m.student_id = s.id_student
group by
f.id_faculty, f.name, s.name;
Query result:
FACULTY_NAME | s.name | MAX_STIPEND
----------------+-----------+---------------
IT Faculty | Joe | 50
IT Faculty | Lisa | 10
Architecture | Bob | 60
Journalism | Fred | 5
Architecture | Susan | 5
Journalism | Tom | 40
It does the same thing using right, left and inner joins. Can someone tell where the problem is?
First, you should be using proper JOIN syntax for all your joins.
Second, you can use Oracle's keep syntax:
select f.name as FACULTY_NAME,
max(stipend) as MAX_STIPEND,
max(s.name) keep (dense_rank first order by stipend desc)
from money m join
student s
on m.student_id = s.id_student join
faculty f
on f.id_faculty = s.faculty_id
group by f.id_faculty, f.name;
However when I add s.name to original query to also show the name of the student who received max_stipend, query is not working like it used to - it returns all of the students
When you add s.name you are looking for min value for each user.
If you want the name of user who has the MAX_STIPEND you should to move to window functions. For example Dense Rank in MS SQL Server.
with cte as
(select
f.name as "FACULTY_NAME",
s.name as "STUDENT_NAME",
stipend as "MAX_STIPEND",
DENSE_RANK() OVER
(PARTITION BY f.name, s.name ORDER BY i.stipend DESC) AS Rank
from
money m
inner join student s on m.student_id = s.id_student
inner join
faculty f on f.id_faculty = s.faculty_id
)
select "FACULTY_NAME", "STUDENT_NAME"
from cte
where rank = 1
Not all sql brands have windowed functions. Here the link for dense_rank on MySQL and also dense_Rank for Oracle
I have three tables that needs to be checked in order to find out on which courses professor is active.
table_teacher
table_course; and
table_teacher_holds_course
table_teacher looks like this:
username | title
---------+----------
john | professor
mark | assistant
table_course looks like this:
course_code | course_name | semester | school_year
-------------+-------------+----------+------------
course_code1| course1 |semester1 | 2015
course_code2| course2 |semester2 | 2015
course_code3| course3 |semester3 | 2015
table_teacher_holds_course looks like this:
username | course_code
---------+-------------
john |course_code1
mark |course_code2
and when I have professors username when he logs on the page, I would like to do left inner join on these three tables in order to show professors courses from table_course
Can someone help me with this, because it is first time to me to use sql to join search in several tables.
Join on the tables common fields
SELECT *
FROM table_teacher t
INNER JOIN table_teacher_holds_course hc ON t.username = hc.username
INNER JOIN table_course c ON hc.course_code = c.course_code
Relevant example:
SELECT t.title, t.username, c.course_code, c.course_name, c.semester, c.school_year
FROM table_teacher t
INNER JOIN table_teacher_holds_course hc ON t.username = hc.username
INNER JOIN table_course c ON hc.course_code = c.course_code
WHERE t.username = 'John'
Result:
title username course_code course_name semester school_year
professor John course_code1 course1 semester1 2015
I am trying to select records that appear more than once and are part of a specific department plus other departments.
So far the query that I have is this:
SELECT employeeCode, employeeName
FROM
Employees
WHERE
Department <> 'Technology'
AND employeeCode IN (SELECT employeeCode
FROM Employees
GROUP BY employeeCode HAVING COUNT(*) > 1)
The problem is that I want to select employees which are part of the Technology department, but they also participate in other departments.
So, they must be from the Technology department, but they could also be from the Household department. In the database it could look like:
1 | A1 | Alex | Technology
2 | A2 | Thor | Household
3 | A3 | John | Cars
4 | A3 | John | Technology
5 | A4 | Kim | Technology
6 | A4 | Kim | Video Games
So basically the query should return:
A3 | John |
A4 | Kim |
I think it's a small part that I am missing but..
Any ideas on how to filter/sort it so that it always uses the technology and the other departments?
Btw, I tried searching but I couldn't find a problem like mine..
If you want employees that could be in the technology department and another department:
select e.employeeCode, e.employeeName
from employees e
group by e.employeeCode, e.employeeName
having sum(case when e.department = 'Technology' then 1 else 0 end) > 0 and
count(*) > 1;
This assumes no duplicates in the table. If it can have duplicates, then use count(distinct department) > 1 rather than count(*) > 1.
Try this:
SELECT E.employeeCode, E.employeeName
FROM Employees E
INNER JOIN (SELECT DISTINCT E1.employeeCode, E1.employeeName
FROM Employees E
WHERE E.Department = 'Technology'
) AS A ON E.employeeCode = A.employeeCode AND E.employeeName = A.employeeName
GROUP BY E.employeeCode, E.employeeName
HAVING COUNT(*) > 1;
You can use EXISTS with correlated sub-query joining on the same table with different condition.
SELECT e1.employeeCode, e1.employeeName
FROM Employees e1
WHERE e1.Department = 'Technology'
AND EXISTS (SELECT * FROM Employees e2
WHERE e1.employeeCode = e2.employeeCode
AND e2.Department <> 'Technology')
This will work for your case:
SELECT a.employeeCode, a.employeeName
FROM Employees a, Employees b
WHERE
a.Department = 'Technology'
AND
b.Department <> 'Technology'
AND
a.employeeCode = b.employeeCode
AND
a.employeeID <> b.employeeID
I have this SQL code and I want to show the sum of each item on its charge slip and on their receipt:
select item_description, sum(receipt_qty) as Samp1, sum(chargeSlip_qty) as Samp2
from Items inner join Receipt_Detail on (Receipt_Detail.item_number =
Items.item_number)
inner join ChargeSlip_Detail on (ChargeSlip_Detail.item_number =
Items.item_number)
group by item_description
It produces this output:
Acetazolamide 2681 1730
Ascorbic Acid 1512 651
Paracetamol 1370 742
Silk 576 952
But it should be:
Acetazolamide 383 173
Ascorbic Acid 216 93
Paracetamol 274 106
Silk 96 238
What's wrong with my code?
Since you are joining tables, you might have a one-to-many relationship that is causing the problem when you then get the sum(). So you can use subqueries to get the result. This will get the sum() for the receipt and chargeslip for each item_number and then you join that back to your items table to get the final result:
select i.item_description,
r.Samp1,
c.Samp2
from Items i
inner join
(
select sum(receipt_qty) Samp1,
item_number
from Receipt_Detail
group by item_number
) r
on r.item_number = i.item_number
inner join
(
select sum(chargeSlip_qty) Samp2,
item_number
from ChargeSlip_Detail
group by item_number
) c
on c.item_number = i.item_number
Do the GROUP BYs first, per Item_Number, so you don't multiply out rows from Receipt_Detail and ChargeSlip_Detail. That is, you generate the SUM values per Item_Number before JOINing back to Items
select
I.item_description,
R.Samp1,
C.Samp2
from
Items I
inner join
(SELECT item_number, sum(receipt_qty) as Samp1
FROM Receipt_Detail
GROUP BY item_number
) R
on (R.item_number = I.item_number)
inner join
(SELECT item_number, sum(chargeSlip_qty) as Samp2
FROM ChargeSlip_Detail
GROUP BY item_number
) C
on (C.item_number = I.item_number)
A left join returns rows from the left table, and for each row in the left table, all matching rows in the right table.
So for example:
create table Customers (name varchar(50));
insert Customers values
('Tim'),
('John'),
('Spike');
create table Orders (customer_name varchar(50), product varchar(50));
insert Orders values (
('Tim', 'Guitar'),
('John', 'Drums'),
('John', 'Trumpet');
create table Addresses (customer_name varchar(50), address varchar(50));
insert Addresses values (
('Tim', 'Penny Lane 1'),
('John', 'Abbey Road 1'),
('John', 'Abbey Road 2');
Then if you run:
select c.name
, count(o.product) as Products
, count(a.address) as Addresses
from Customers c
left join Orders o on o.customer_name = c.name
left join Addresses a on a.customer_name = c.name
group by name
You get:
name Products Addresses
Tim 1 1
John 4 4
Spike 0 0
But John doesn't have 4 products!
If you run without the group by, you can see why the counts are off:
select *
from Customers c
left join Orders o on o.customer_name = c.name
left join Addresses a on a.customer_name = c.name
You get:
name customer_name product customer_name address
Tim Tim Guitar Tim Penny Lane 1
John John Drums John Abbey Road 1
John John Drums John Abbey Road 2
John John Trumpet John Abbey Road 1
John John Trumpet John Abbey Road 2
Spike NULL NULL NULL NULL
As you can see, the joins end up repeating each other. For each product, the list of addresses is repeated. That gives you the wrong counts. To solve this problem, use one of the excellent other answers:
select c.name
, o.order_count
, a.address_count
from Customers c
left join
(
select customer_name
, count(*) as order_count
from Orders
group by
customer_name
) o
on o.customer_name = c.name
left join
(
select customer_name
, count(*) as address_count
from Addresses
group by
customer_name
) a
on a.customer_name = c.name
The subqueries ensure only one row is joined per customer. The result is much better:
name order_count address_count
Tim 1 1
John 2 2
Spike NULL NULL