Home All Groups Group Topic Archive Search About
Author
16 Oct 2005 3:09 AM
Luiz Horacio
Hi,

I have a query that will show results from three tables:

select num_abs, num_ref, registro,
paciente.nome as pac_nome, nasc, paciente.data, data_lancado,
unidade.nome as unid_nome, mne, cor_padrao,
sum(case when laudado = 'S' then 1 else 0 end),
sum(case when rotina = 'P' then 1 else 0 end),
sum(case when laudado = 'N' then 1 else 0 end)
From paciente, unidade, paciente_laudo
Where paciente.unidade = unidade.mne
AND paciente.num_ref =  paciente_laudo.num_abs_pac
AND paciente.data between '2005-09-06' AND '2005-10-15'
GROUP by num_abs ORDER by data DESC, registro DESC

It works fine and fast. But if there are no matching records in
'paciente_laudo' (all 'SUM' works with paciente_laudo) this record will not
be shown.

So I made a change, and added a left join:

select num_abs, num_ref, registro,
paciente.nome as pac_nome, nasc, paciente.data, data_lancado,
unidade.nome as unid_nome, mne, cor_padrao,
sum(case when laudado = 'S' then 1 else 0 end),
sum(case when rotina = 'P' then 1 else 0 end),
sum(case when laudado = 'N' then 1 else 0 end)
From paciente, unidade
left join paciente_laudo on num_ref = num_abs_pac
Where paciente.unidade = unidade.mne
AND paciente.data between '2005-09-06' AND '2005-10-15'
GROUP by num_abs ORDER by data DESC, registro DESC

Now it works fine, but takes almost 10 times longer to run...

Is there any other way of doing it?


Thanks,


Luiz Horacio

Author
16 Oct 2005 11:36 AM
Gijs Beukenoot
From Luiz Horacio :
Show quoteHide quote
> Hi,
>
> I have a query that will show results from three tables:
>
> select num_abs, num_ref, registro,
> paciente.nome as pac_nome, nasc, paciente.data, data_lancado,
> unidade.nome as unid_nome, mne, cor_padrao,
> sum(case when laudado = 'S' then 1 else 0 end),
> sum(case when rotina = 'P' then 1 else 0 end),
> sum(case when laudado = 'N' then 1 else 0 end)
> From paciente, unidade, paciente_laudo
> Where paciente.unidade = unidade.mne
> AND paciente.num_ref =  paciente_laudo.num_abs_pac
> AND paciente.data between '2005-09-06' AND '2005-10-15'
> GROUP by num_abs ORDER by data DESC, registro DESC
>
> It works fine and fast. But if there are no matching records in
> 'paciente_laudo' (all 'SUM' works with paciente_laudo) this record will not
> be shown.
>
> So I made a change, and added a left join:
>
> select num_abs, num_ref, registro,
> paciente.nome as pac_nome, nasc, paciente.data, data_lancado,
> unidade.nome as unid_nome, mne, cor_padrao,
> sum(case when laudado = 'S' then 1 else 0 end),
> sum(case when rotina = 'P' then 1 else 0 end),
> sum(case when laudado = 'N' then 1 else 0 end)
> From paciente, unidade
> left join paciente_laudo on num_ref = num_abs_pac
> Where paciente.unidade = unidade.mne
> AND paciente.data between '2005-09-06' AND '2005-10-15'
> GROUP by num_abs ORDER by data DESC, registro DESC
>
> Now it works fine, but takes almost 10 times longer to run...
>
> Is there any other way of doing it?
>
>
> Thanks,
>
>
> Luiz Horacio

You might try adding an index to the fields that you're joining
Author
17 Oct 2005 1:10 AM
Luiz Horacio
Hi Gijs,

Thanks, but they are already indexed.

Luiz Horacio


Show quoteHide quote
"Gijs Beukenoot" <Gijs.DOT.Beukenoot@A*@Wanadoo.DOT.nl> escreveu na mensagem
news:mn.83307d5a684e3dcd.23024@Wanadoo.DOT.nl...
> From Luiz Horacio :
> > Hi,
> >
> > I have a query that will show results from three tables:
> >
> > select num_abs, num_ref, registro,
> > paciente.nome as pac_nome, nasc, paciente.data, data_lancado,
> > unidade.nome as unid_nome, mne, cor_padrao,
> > sum(case when laudado = 'S' then 1 else 0 end),
> > sum(case when rotina = 'P' then 1 else 0 end),
> > sum(case when laudado = 'N' then 1 else 0 end)
> > From paciente, unidade, paciente_laudo
> > Where paciente.unidade = unidade.mne
> > AND paciente.num_ref =  paciente_laudo.num_abs_pac
> > AND paciente.data between '2005-09-06' AND '2005-10-15'
> > GROUP by num_abs ORDER by data DESC, registro DESC
> >
> > It works fine and fast. But if there are no matching records in
> > 'paciente_laudo' (all 'SUM' works with paciente_laudo) this record will
not
> > be shown.
> >
> > So I made a change, and added a left join:
> >
> > select num_abs, num_ref, registro,
> > paciente.nome as pac_nome, nasc, paciente.data, data_lancado,
> > unidade.nome as unid_nome, mne, cor_padrao,
> > sum(case when laudado = 'S' then 1 else 0 end),
> > sum(case when rotina = 'P' then 1 else 0 end),
> > sum(case when laudado = 'N' then 1 else 0 end)
> > From paciente, unidade
> > left join paciente_laudo on num_ref = num_abs_pac
> > Where paciente.unidade = unidade.mne
> > AND paciente.data between '2005-09-06' AND '2005-10-15'
> > GROUP by num_abs ORDER by data DESC, registro DESC
> >
> > Now it works fine, but takes almost 10 times longer to run...
> >
> > Is there any other way of doing it?
> >
> >
> > Thanks,
> >
> >
> > Luiz Horacio
>
> You might try adding an index to the fields that you're joining
>
>
Author
16 Oct 2005 6:04 PM
Jeff Johnson [MVP: VB]
"Luiz Horacio" <lhora***@iname.com> wrote in message
news:e4dmH8f0FHA.4032@TK2MSFTNGP15.phx.gbl...

> I have a query that will show results from three tables:

This is purely an SQL question, and as such it belongs in a group like
microsoft.public.sqlserver.programming.
Author
17 Oct 2005 1:10 AM
Luiz Horacio
Hi Jeff,

You're right, I'll post it in the right place.

Thanks,


Luiz Horacio


Show quoteHide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> escreveu na mensagem
news:O$WMUvn0FHA.1252@TK2MSFTNGP09.phx.gbl...
>
> "Luiz Horacio" <lhora***@iname.com> wrote in message
> news:e4dmH8f0FHA.4032@TK2MSFTNGP15.phx.gbl...
>
> > I have a query that will show results from three tables:
>
> This is purely an SQL question, and as such it belongs in a group like
> microsoft.public.sqlserver.programming.
>
>