Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compare two SQL queries

Posted on 2010-11-15
6
Medium Priority
?
307 Views
Last Modified: 2012-06-27
I have two version of the code below:
Ver 1:
Select
p.facnum, 
count(distinct case when m.contr = 3 then p.resnum else NULL end),
count(distinct p.resnum)
from patients p, patients_meds pm, meds m
where p.resnum=pm.resnum and pm.pmcode=m.pmcode
group by p.facnum

Ver 2:
Select
p.facnum,
      sum(case when exists (
            Select *
            from patients_meds pm
            inner join meds m on pm.pmcode=m.pmcode
            where p.resnum=pm.resnum and m.contr = 3) then 1 else 0 end),
count(p.resnum)
from patients p
group by p.facnum

Open in new window


The result is different a little bit. Below is data I extract from the result. First 3 columns for V1 of code and next 3 columns is for V2 of code. Next 3 columns is for V1 of code but do not have the distinct in count
 data.xls

Why there is the different? I don't understand first is about:
Query1: count(distinct p.resnum)
Query2: count(p.resnum)
If I remove distinct in the first version of the code (Query 1) the result will change so much
There is no change if I include distinct in V2 of code (Query 2)

I think maybe the different if we use "then p.resnum else NULL end" in V1 and "then 1 else 0 end" in V2 is also a problem.

Do you have any ideas?
0
Comment
Question by:YANKAUSKAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34141934
I think I was mislead by your diagram which showed

patient : 1/1-1/M : patients_med  : 1/M-1/1 : med

But in fact, I can tell that the data is actually

patient : 1/1-0/M : patients_med  : 1/M-?/1 : med   (notice the 0 and ?)

The 2nd one:
It will list ALL patients.  This is regardless of whether they have patients_med records.

The 1st one:
The joins between the 3 tables will result in ONLY patients that have a link to patients_med and from there to med.  If a patient has no record in patients_med, it will disappear from the 3rd column count.

The 3rd column counts the number of patients, so depending on which query is used, you get a different result.
The 2nd column count will always be the same because it counts only records that exist.
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 400 total points
ID: 34141939
>> Query1: count(distinct p.resnum)
Query2: count(p.resnum)

Having DISTINCT keyword inside would count only the unique / distinct resnum values. Say if you have 2 resnum records with values 1, 1 & 2, then
count(distinct p.resnum)  = 2
count(p.resnum) = 3

Therefore resultset would have difference based upon that.

>> count(distinct case when m.contr = 3 then p.resnum else NULL end),
>> sum(case when exists ( ) then 1 else 0 end),

On the same logic, count distinct would count all values of p.resnum if m.contr = 3 even if it is decoded as NULL.
two values are NULL are not equal and hence those will also be counted and hence the correct version should be

sum(case when m.contr = 3 then 1 else 0 end)

Hope this clarifies.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1600 total points
ID: 34141940
If you turn ver1 around to left joins:

Select
p.facnum,
count(distinct case when m.contr = 3 then p.resnum else NULL end),
count(distinct p.resnum)
from patients p
left join patients_meds pm on p.resnum=pm.resnum
left join meds m on pm.pmcode=m.pmcode
group by p.facnum

You should now get exactly the same result as ver 2.
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34141946
rrjegan17,
sum(case when m.contr = 3 then 1 else 0 end)
That won't give the right result because it will count the same person multiple times due to the cartesian product from joining across the 3 tables.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34142144
>> That won't give the right result because it will count the same person multiple times

Yes, it won't and that's what I tried to explain that COUNT would count all values even NULL values in the ver 1.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34142338
>> Yes, it won't and that's what I tried to explain that COUNT would count all values even NULL values in the ver 1.

Are you sure NULLs are counted?
0

Featured Post

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question