Subject | help with a select |
---|---|
Author | Sergio H. Gonzalez |
Post date | 2010-04-27T12:33:15Z |
Hello ! I'm trying to do this: I have a table in which I store visits to a
doctor. In each visit, the patient can be diagnosed with *ONE OR MORE* illness
(they are stored in the table "consultas_mc"). With this select I get all the
visits (table "consultas") but if one visit has more than one illness I get the
visit duplicated:
select
mc.id,
c.id,
d.rubrica_completa,
c.fecha,
p.dni,
p.apellido || ' ' || p.nombre
from consultas c
inner join consultas_mc mc on (c.id = mc.id_consultas)
inner join diagnosticos d on (mc.id_diagnosticos = d.id)
inner join pacientes p on (c.id_pacientes = p.id)
where
c.id_pacientes = :id
order by mc.id desc
*I want to get just the first illnes of each visit, in order to get each visit
once*. So I did this:
select
min(mc.id),
min(c.id),
min(d.rubrica_completa),
c.fecha,
p.dni,
p.apellido || ' ' || p.nombre
from consultas c
inner join consultas_mc mc on (c.id = mc.id_consultas)
inner join diagnosticos d on (mc.id_diagnosticos = d.id)
inner join pacientes p on (c.id_pacientes = p.id)
where
c.id_pacientes = :id
group by
c.fecha,
p.dni,
p.apellido || ' ' || p.nombre
order by 1
But comparing both results I've find that the second does not return all the
rows. So I gess I did something wrong..
Any help, please?
Thanks!!
sergio
doctor. In each visit, the patient can be diagnosed with *ONE OR MORE* illness
(they are stored in the table "consultas_mc"). With this select I get all the
visits (table "consultas") but if one visit has more than one illness I get the
visit duplicated:
select
mc.id,
c.id,
d.rubrica_completa,
c.fecha,
p.dni,
p.apellido || ' ' || p.nombre
from consultas c
inner join consultas_mc mc on (c.id = mc.id_consultas)
inner join diagnosticos d on (mc.id_diagnosticos = d.id)
inner join pacientes p on (c.id_pacientes = p.id)
where
c.id_pacientes = :id
order by mc.id desc
*I want to get just the first illnes of each visit, in order to get each visit
once*. So I did this:
select
min(mc.id),
min(c.id),
min(d.rubrica_completa),
c.fecha,
p.dni,
p.apellido || ' ' || p.nombre
from consultas c
inner join consultas_mc mc on (c.id = mc.id_consultas)
inner join diagnosticos d on (mc.id_diagnosticos = d.id)
inner join pacientes p on (c.id_pacientes = p.id)
where
c.id_pacientes = :id
group by
c.fecha,
p.dni,
p.apellido || ' ' || p.nombre
order by 1
But comparing both results I've find that the second does not return all the
rows. So I gess I did something wrong..
Any help, please?
Thanks!!
sergio