What I want to do (for each flight), is to select Flight_number, Departure_airport' s Name And Arrival_airport' s Name . Departure has MIN Leg_number, Arrival has MAX Leg_number.
I have tried this. But join parts or what else missing, here is the link:http://sqlfiddle.com/#!2/263a2/5
Seems odd.. but this might be what you're after...
We get the min/max leg for each flight in subquery aliased "Z"
We use this to join back to flight_leg twice, once for departure and once for arrivals
and again join back twice to airport once for departures once for arrivals.
SELECT Z.Flight_Number, DA.Name DeptName, AA.Name ArrivName
FROM (SELECT MIN(Leg_Number) MLN, MAX(Leg_Number) MxLN, Flight_Number
FROM Flight_Leg Group by Flight_Number) Z
INNER JOIN Flight_Leg D
on D.Flight_Number = Z.Flight_Number
and D.Leg_Number = Z.MLN
INNER JOIN Flight_Leg A
on A.Flight_Number = Z.Flight_Number
and A.Leg_Number = Z.MxLN
INNER JOIN AirPort DA
on DA.AirPort_Code = D.Departure_AirPort_Code
INNER JOIN AirPort AA
on AA.AirPort_Code = A.Arrival_AirPort_Code
http://sqlfiddle.com/#!2/263a2/56
Not entirely sure if this is what you're after. It's written in MS SQL so the syntax will need some minor tweaks.
SELECT fl.Flight_number,
ao.Name,
ai.Name,
(select min(Leg_number) from FLIGHT_LEG fa where fl.Flight_number
= fa.Flight_number) as min_leg_number,
(select max(Leg_number) from FLIGHT_LEG fb where fl.Flight_number
= fb.Flight_number) as max_leg_number
FROM Flight_leg Fl
inner join AIRPORT as ao on fl.Departure_airport_code =
ao.Airport_code
inner join AIRPORT as ai on fl.Arrival_airport_code =
ai.Airport_code
Related
I'm trying to run a query but my outcome is not what i need.
So the problem is:
A user can be diretor and if this is the case he can see all activities from his department, and he can be user of another department too, in this case not director but only user.
I have 8 departments each with one director, so the following query should give me the activities of the department and the activities of this particular user in other department:
SELECT t1.idAtividade,
t1.idProfessor,
t2.Escola,
t1.Atividade,
t1.Periodo,
t1.Mes,
t1.haveClasses,
t1.DataPrevista,
t1.Destinatarios,
t1.Orcamento,
t1.PdfAtividade,
t1.Avaliacao,
t1.PdfAvaliacao,
t1.idProfessor,
p.Nome,
g.Grupo,
d.Departamento,
p2.Projeto,
t1.idProjeto
FROM atividades AS t1
INNER JOIN professores p on t1.idProfessor = p.idProfessor
INNER JOIN atividadesgrupos ag on t1.idAtividade = ag.idAtividade
INNER JOIN grupos g on ag.idGrupo = g.idGrupo
INNER JOIN departamentosatividades da on t1.idAtividade = da.idAtividade
INNER JOIN departamentos d on da.idDepartamento = d.idDepartamento
INNER JOIN escolas AS t2 ON (t2.idEscola = t1.idEscola)
INNER JOIN anosescolares AS ae ON (t1.idAnoEscolar = ae.idAnoEscolar)
INNER JOIN projetos p2 on t1.idProjeto = p2.idProjeto
WHERE ae.Estado = 1 AND (da.idDepartamento = :id_dpt and ag.idGrupo = :idGrupo)
ORDER BY (t1.idProfessor = :idProfessor) DESC, t1.idProfessor;");
This query is not working because the department have 22 activities but this user (idProfessor) has 5 in this department and 14 more in another department (defined by idGrupo)
I think i will need a subquery right?
looking for a bit of help here if possible?
I have the following query:-
On or database we have a table called Linkfile, in this table are "Types" all beginning with "YG". I need to return those rows that do not have the type of "YG8" but just cannot seem to do it. I know ill need to use a sub query but am stuck!
This is my code and the fields I need to return. I just need to only show those that do not have the lk.type of "YG8"
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
inner join linkfile lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016'
and lk.type like 'YG%' and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type
Use below query :
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type,lk.parent_object_ref
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
left join (select displayname, type,parent_object_ref from linkfile where lk.type like 'YG8%' )lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016' and lk.parent_object_ref is null
and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type;
I've used left join on linkfile with type like 'YG8%' and fetching the only records which are not matched
I think you can just replace the
lk.type like 'YG%'
with the following:
(lk.type >= 'YG' and lk.type <'YG8') or (lk.type > 'YG8' and lk.type <='YGZ')
this should accomplish what you are trying to do and also avoid using "like" which is less efficient (assuming you have an index on lk.type, at least).
You may refine this a bit by knowing which are the possible values of lk.type of course. I.e. what are the extremes for the YG "subtype"? YG00-YG99? YG-YGZ?
(Be especially careful if you may have YG81 or YG87 for example, because then my clause will not work properly... on the other hand if your YG subtype can have values like YG34 it would have been better to use YG08 instead of YG8)
I made a query that list all the players on all the teams for each of the clubs in my site.
A "club" has "teams" and each team has "players", I solve the listing using this query:
SELECT
Club.*,
Teams.*,
Players.*
FROM
(Club LEFT JOIN Teams ON Club.idClub = Teams.idClub)
LEFT JOIN
Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias
WHERE
Teams.idTemporadas = 2017
It works great, now I want to list just the players than "has played" from other table "hasPlay" I made this changes, but It returns empty:
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM
((Club LEFT JOIN Teams ON Club.idClub = Teams.idClub)
LEFT JOIN
Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias) LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
WHERE
Teams.idTemporadas = 2017
I think I mess it up in the last join...
Any advice is well recieved
I doubt your original query works
as it contains various errors and anomalies
anyway you should avoid useless ()
and especially use JOIN on the Equipos_Categorias table eg:
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM Club LEFT JOIN Teams ON Club.idClub = Teams.idClub
LEFT JOIN Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias
LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
INNER JOIN Equipos_Categorias ON Teams.idEquipos_Categorias = Equipos_Categorias.idEquipos_Categorias
and Equipos_Categorias.idTemporadas = 2017
ORDER BY Club.nombre, Equipos_Categorias.idEquipos_Categorias ASC,
Jugadores_Equipos.categoriaJugador ASC, Jugadores_Equipos.nombre ASC
and as verified by OP uisng team table
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM Club LEFT JOIN Teams ON Club.idClub = Teams.idClub
INNER JOIN Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias and team.idTemporadas = 2017
LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
ORDER BY Club.nombre, Equipos_Categorias.idEquipos_Categorias ASC,
Jugadores_Equipos.categoriaJugador ASC, Jugadores_Equipos.nombre ASC
I have two tables: calls and employees. In the calls tables I have a field enter_emp_id and another follow_emp_id. They hold the values for which employee entered the call and which employee is assigned to the call. My second table employees has employee_id and employee fields. I am able to write a query to show results with an employee name for who entered the call, but not who it is assigned to. I do have other values in the query, but I need to get the 2nd employee name to show in the results. Here is what I am using so far:
SELECT
`calls`.`call_id`,
`calls`.`enter_date`,
`call_state`.`call_state`,
`customers`.`customer_name`,
`calls`.`comments`,
`employees`.`employee`,
`call_reasons`.`call_reason`
FROM
`customers`
INNER JOIN `calls` ON (`customers`.`customer_id` = `calls`.`customer_id`)
INNER JOIN `employees` ON (`employees`.`employee_id` = `calls`.`enter_emp_id`)
INNER JOIN `call_state` ON (`call_state`.`call_state_id` = `calls`.`call_state_id`)
INNER JOIN `call_reasons` ON (`call_reasons`.`call_reason_id` = `calls`.`call_reason_id`)
WHERE
`calls`.`call_id` = $call_id;
You need to join to the employee table TWICE... once for each employee id association, and use the ALIAS of the respective to get the values intended. And aliasing the tables and removal of unnecessary tick marks not required. Only needed for possible reserved word conflicts.
SELECT
c.call_id,
c.enter_date,
cs.call_state,
cust.customer_name,
c.comments,
entered.employee,
assigned.employee as AssignedEmployee,
cr.call_reason
FROM
calls c
INNER JOIN customers cust
ON c.customer_id = cust.customer_id
INNER JOIN employees entered
ON c.enter_emp_id = entered.employee_id
INNER JOIN employees assigned
ON c.enter_emp_id = assigned.employee_id
INNER JOIN call_state cs
ON c.call_state_id = cs.call_state_id
INNER JOIN call_reasons cr
ON c.call_reason_id = cr.call_reason_id
WHERE
c.call_id = $call_id;
I hope i understand you problem. You can use AS to change or shorten table name for sql usage
SELECT
`calls`.`call_id`,
`calls`.`enter_date`,
`call_state`.`call_state`,
`customers`.`customer_name`,
`calls`.`comments`,
`employees`.`employee`,
`assigned`.`employee` as assigned_employee,
`call_reasons`.`call_reason`
FROM
`customers`
INNER JOIN `calls` ON (`customers`.`customer_id` = `calls`.`customer_id`)
INNER JOIN `employees` ON (`employees`.`employee_id` = `calls`.`enter_emp_id`)
INNER JOIN `employees` AS `assigned` ON (`assigned`.`employee_id` = `calls`.`follow_emp_id`)
INNER JOIN `call_state` ON (`call_state`.`call_state_id` = `calls`.`call_state_id`)
INNER JOIN `call_reasons` ON (`call_reasons`.`call_reason_id` = `calls`.`call_reason_id`)
WHERE
`calls`.`call_id` = $call_id;
Thanks the following worked:
SELECT
c.call_id,
c.enter_date,
cs.call_state,
cust.customer_name,
c.comments,
entered.employee,
entered.employee,
cr.call_reason
FROM
calls c
INNER JOIN customers cust
ON c.customer_id = cust.customer_id
INNER JOIN employees entered
ON c.enter_emp_id = entered.employee_id
INNER JOIN employees assigned
ON c.enter_emp_id = assigned.employee_id
INNER JOIN call_state cs
ON c.call_state_id = cs.call_state_id
INNER JOIN call_reasons cr
ON c.call_reason_id = cr.call_reason_id
WHERE
c.call_id = $call_id;
My table structure:
Lantern
Lantern_type
Location
Loan
Borrower
My query returns duplicates, I am looking for a solution which does not include repeated data.
Code
SELECT
l.lantern_id, l.lantern_name, l.lantern_state,lt.lantern_type,
lt.lantern_type_description, lt.lantern_type_tech_info,
lt.lantern_type_lens, lo.location_id, lo.location_name, loa.loan_id,
loa.loan_start_date, loa.loan_end_date, b.borrower_id, b.borrower_name, b.user_id
FROM lantern as l
INNER JOIN lantern_type as lt
ON l.lantern_id = lt.lantern_type_id
INNER JOIN location as lo
ON lt.LANTERN_TYPE_ID = l.lantern_id
INNER JOIN loan as loa
ON lo.LOCATION_ID = loa.LOAN_ID
INNER JOIN borrower as b
ON loa.loan_id = b.borrower_id
;
I know this is an old question and probably not relevant to your case now but I cannot agree with the accepted answer. In this case the accepted answer is just masking the problem.
From looking at your join statements your issue was probably with this join:
INNER JOIN location as lo
ON lt.LANTERN_TYPE_ID = l.lantern_id
You don't use the correct table alias on this join. I suspect the alias after the = should have been lo instead of l.
Try:
select l.lantern_id, l.lantern_name, l.lantern_state,lt.lantern_type,
lt.lantern_type_description, lt.lantern_type_tech_info,
lt.lantern_type_lens, lo.location_id, lo.location_name, loa.loan_id,
loa.loan_start_date, loa.loan_end_date, b.borrower_id, b.borrower_name, b.user_id
From lantern as l
INNER JOIN lantern_type as lt ON
l.lantern_id = lt.lantern_type_id
INNER JOIN location as lo ON
lt.LANTERN_TYPE_ID = l.lantern_id
INNER JOIN loan as loa ON
lo.LOCATION_ID = loa.LOAN_ID
INNER JOIN borrower as b ON
loa.loan_id = b.borrower_id
GROUP BY l.lantern_id;
See MySql GROUP BY