Page 1 of 1
Need help writing User Query
Posted: Sat Oct 04, 2014 10:55 am
by mikebarrdds
I haven't quite figured out how to write queries (SQL language).
I created a Patient Field Definition called "QDP Anniversary" with a date.
I'd like to run a report that would list all my QDP patients. So, the report would be active patients with a date in that field that is greater than January 1st, 2013.
This is what I came up with. But, I'm getting an "unknown column - QDPanniversary" error. I came up with this by copying from another report and modifying it where I thought was appropriate. But, so far, no go.
SELECT patient.LName, patient.FName, patient.EMail
FROM patient
WHERE PatStatus=0 AND QDPanniversary > "2013-01-01";
Anyone handy with SQL? Thanks!
Mike
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 11:59 am
by Jorgebon
Your problem is that those added fields get stored in another table called "patfield". So you would have to link both tables using a common field like the patnum field. This is what you would get:
select patient.Lname, patient.Fname, patient.email, patfield.fieldvalue
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary''
AND patfield.fieldvalue > "2013-01-01"
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 12:10 pm
by mikebarrdds
Jorgebon wrote:Your problem is that those added fields get stored in another table called "patfield". So you would have to link both tables using a common field like the patnum field. This is what you would get:
select patient.Lname, patient.Fname, patient.email, patfield.fieldvalue
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary''
AND patfield.fieldvalue > "2013-01-01"
thanks for your help. I ran that and got an error:

Re: Need help writing User Query
Posted: Sat Oct 04, 2014 12:12 pm
by mikebarrdds
OK... figured out that the "anniversary" line had an extra " ' " at the end. Deleted that. No more error, but the report comes up empty.
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 3:00 pm
by Jorgebon
Sorry about the double quotes. The date also should use single quotes, but for some reason it won't return any results. Apparently this field isn't recognized as a date. We'll have to wait for the solution from the guys at OD.
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 3:35 pm
by Jorgebon
I tried treating the field value as a text instead of as a date, and came up with this:
Select patient.Lname, patient.Fname, patient.email, patfield.fieldvalue as QDPDate
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary'
AND patfield.fieldvalue Like '%2013'
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 4:06 pm
by Jorgebon
OK, my suspicion is confirmed. I wrote you a query where we convert the text string in your custom QDPAnniversary field to a date type value and it should work. Please try this:
Select patient.Lname, patient.Fname, patient.email, patfield.fieldvalue as QDPAnniversary
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary'
AND str_to_date(patfield.fieldvalue,'%m/%d/%Y') > '2013/01/01'
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 4:11 pm
by mikebarrdds
Jorgebon wrote:OK, my suspicion is confirmed. I wrote you a query where we convert the text string in your custom QDPAnniversary field to a date type value and it should work. Please try this:
Select patient.Lname, patient.Fname, patient.email, patfield.fieldvalue as QDPAnniversary
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary'
AND str_to_date(patfield.fieldvalue,'%m/%d/%Y') > '2013/01/01'
It worked! Thanks! Very cool.
Is it possible to have the rows numbered on the left side?
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 4:54 pm
by Jorgebon
Like this?
SET @pos=0;
SELECT @pos:=@pos+1 as Patient, patient.Lname, patient.Fname, patient.email, patfield.fieldvalue as QDPAnniversary
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary'
AND str_to_date(patfield.fieldvalue,'%m/%d/%Y') > '2013/01/01'
Re: Need help writing User Query
Posted: Sat Oct 04, 2014 5:03 pm
by mikebarrdds
Jorgebon wrote:Like this?
SET @pos=0;
SELECT @pos:=@pos+1 as Patient, patient.Lname, patient.Fname, patient.email, patfield.fieldvalue as QDPAnniversary
From patient, patfield
Where patient.patstatus = 0
AND patient.patnum = patfield.patnum
AND patfield.fieldname = 'QDPAnniversary'
AND str_to_date(patfield.fieldvalue,'%m/%d/%Y') > '2013/01/01'
You're the best!
Muchas gracias! Te lo agradezco!
