soozh
asked on
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:
However i dont get all the clinics and i suspect its because i have a where condition. How should i re-write it?
/richard
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
However i dont get all the clinics and i suspect its because i have a where condition. How should i re-write it?
/richard
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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
@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_Klini knr
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_Klini knr
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"
----------
Regarding the date range condition/filter now in use:
LEFT OUTER JOIN dbo.Measurements
ON pharosBPSD.dbo.Units.id = dbo.Measurements.kli_Klini
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_Klini
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"
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?
I've never seen that syntax in a JOIN before, and certainly never tried it in Access. Is that syntax exclusive to SQL Server?
>> 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 :)
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 :)
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.
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.
ASKER
However as PortletPaul pointed out i needed to move the where clause to the join statement.