SQL syntax

i have a table of clinics(units) and a table of visits (to the clinics).

I would like to produce a list of all the clinics (Units) with a count of the number visits in a given period (Measurements)

i have tried using the following:

SELECT
  pharosBPSD.dbo.Units.id, 
  count( msr_date ) as AntalSkattning,
FROM pharosBPSD.dbo.Units  
LEFT OUTER JOIN dbo.Measurements ON  pharosBPSD.dbo.Units.id = dbo.Measurements.kli_Kliniknr
inner join pharosBPSD.dbo.Counties ON pharosBPSD.dbo.Units.CountyId = pharosBPSD.dbo.Counties.id
where (( dbo.Measurements.msr_Date >=  '2013-01-01') and ( dbo.Measurements.msr_Date <= '2012-06-01')) 
group by 
  pharosBPSD.dbo.Units.id

Open in new window


However i dont get all the clinics and i suspect its because i have a where condition.  How should i re-write it?

/richard
soozhCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Curiosity overwhelms me ... name me a day that is greater than '2013-01-01' AND less than '2012-06-01'?
0
Dale FyeCommented:
Jim is obviously correct that none of your records can possibly meet both of those criteria.  I suspect that you meant :

where dbo.Measurements.msr_Date >=  '2012-01-01'
   AND dbo.Measurements.msr_Date < '2012-06-01'

Not quite sure why you would want to include 2012/06/01 in that computation, so I removed the equal sign.

However, with that syntax, your Left Join would be negated, and you would only get those units that have at least one visit (I would expect that all would, except maybe new units) during the Jan-May of 2012.

I'm also not sure why you are including the following:

inner join pharosBPSD.dbo.Counties ON pharosBPSD.dbo.Units.CountyId = pharosBPSD.dbo.Counties.id

in your query, since there is no reference to the Counties table in the SELECT clause.
0
PortletPaulfreelancerCommented:
Fix the date issues as discussed above
Resolve the inner join we don't understand (probably an oversight when pasting a 'simplified version')

fyed refers to 'your left join is negated', to fix this:
     move the current where clause to underneath the left join
     then, change 'where' to 'and'

In other words, you need a join condition instead of that where clause. This will allow all pharosBPSD.dbo.Units.id to display but still get the count of measurements you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

soozhCEOAuthor Commented:
Thank guys.  To answer some questions... yes a typo with the date... that did not help!  And I did remove a couple of the columns in the select statement which would have made the join with Countries table...

However as PortletPaul pointed out i needed to move the where clause to the join statement.
0
Dale FyeCommented:
Paul,

By Join condition, are you referring to a HAVING clause?

If I wanted all the Units, and only wanted to count those Measurement records within in that date range, I would normally create a nested subquery to limit the dbo.Measurements result set, prior to the join.
0
soozhCEOAuthor Commented:
This is what i ended up with:

SELECT
  pharosBPSD.dbo.Units.id, 
  max(pharosBPSD.dbo.Units.Name) as kli_Namn, 
  max(pharosBPSD.dbo.Units.Id) as kli_KlinikNr,
  max(pharosBPSD.dbo.Counties.Code) as kli_Lanskod,
  max(pharosBPSD.dbo.Units.PostTown) as kli_PostOrt,
  max(pharosBPSD.dbo.Units.PostCode) as kli_PostNr,
  min( msr_date ) as FörstaSkattning,
  MAX( msr_date ) as SistaSkattning,
  count( msr_date ) as AntalSkattning,
  count( distinct( pat_id) ) as AntalPatienter
FROM pharosBPSD.dbo.Units  
LEFT OUTER JOIN dbo.Measurements ON  pharosBPSD.dbo.Units.id = dbo.Measurements.kli_Kliniknr and ((msr_Date >= @startdate) and (msr_Date <= @enddate))
inner join pharosBPSD.dbo.Counties ON pharosBPSD.dbo.Units.CountyId = pharosBPSD.dbo.Counties.id 
group by 
  pharosBPSD.dbo.Units.id

Open in new window

0
PortletPaulfreelancerCommented:
@fyed, a subquery would be a good alternative (I use nested subqueries frequently). Here simply moving the date range filter to a join condition seemed appropriate to me.
----------

Regarding the date range condition/filter now in use:

LEFT OUTER JOIN dbo.Measurements
                       ON  pharosBPSD.dbo.Units.id = dbo.Measurements.kli_Kliniknr
                    AND ( msr_Date >= @startdate and msr_Date <= @enddate )

I would like reinforce a point made by fyed earlier, that using less than or equal to in the date range filter may be incorrect.

For the dates of '2012-01-01' and  '2012-06-01'  I would treat this as meaning:
I want everything in the 5 months: Jan, Feb, Mar, Apr, May
(and nothing from June and beyond)

In which case you would remove the or equal to after the less than.

LEFT OUTER JOIN dbo.Measurements
                       ON  pharosBPSD.dbo.Units.id = dbo.Measurements.kli_Kliniknr
                    AND ( msr_Date >= @startdate and msr_Date < @enddate )

Right now you are using the direct equivalent to 'between' which I don't recommend for date ranges. I attempt an explanation for this in: "Beware of Between"
0
Dale FyeCommented:
Paul,

I've never seen that syntax in a JOIN before, and certainly never tried it in Access.  Is that syntax exclusive to SQL Server?
0
PortletPaulfreelancerCommented:
>> Is that syntax exclusive to SQL Server?
no, available in most that I'm familiar with, certainly Oracle, MySQL, Postgres
probably Sybase, I'd expect it for DB2
might not work in 'small footprint' db's like SQLite (but could be tested at sqlfiddle)

I don't know if it would work in Access, it might, but would probably need a dozen or so redundant parentheses :)
- just kidding, but Access does love parentheses

Now I understand why you didn't propose it yourself. It was a very thorough response with just that bit missing :)
0
Dale FyeCommented:
Paul,

I totally agree with the parentheses statement!

Have not done much of anything with SQL Server since 2003.  My clients/employers have not wanted to bother with it.  Think it has more to do with not wanting to have to maintain and monitor (dba) it, even the Express edition.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.