on 2013 Feb 15 6:54 AM
Hi all SQL Anywhere 8.0
I am using the following SQL successfully
SELECT cr_no, dated, height, weight FROM malaviya_vitals WHERE cr_no IN ( SELECT cr_no FROM mal_diagnosis WHERE mal_diagnosis.diag_code LIKE M05.%' ) order by cr_no, dated;
with a result set of 6605 rows
However I want to refine the result set a little more to get only the weight and height data of this group of patients only the first time the patient was seen. [dated = MIN (dated)] Need help to construct the exact syntax (should lead to a total of around 1215 rows = no. of patients with diag_code M05.---)
Any help will be appreciated
Regards
Request clarification before answering.
Try adding the following subquery to the outer WHERE
clause:
AND dated = ( SELECT MIN( t.dated ) FROM malaviya_vitals AS t WHERE t.cr_no = malaviya_vitals.cr_no )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You could also try: SELECT cr_no, min(dated) as theDated, height, weight FROM malaviya_vitals WHERE cr_no IN ( SELECT cr_no FROM mal_diagnosis WHERE mal_diagnosis.diag_code LIKE ''%M05.%'' ) group by cr_no, height, weight order by cr_no, theDated
This will create a worktable as part of the query optimisation, but if your caching is sufficient and fast enough, your performance should be good too.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think that gives a row for the earliest date of each unique (cr_no, height, weight) combination, rather than only a row for the first time each patient was seen.
User | Count |
---|---|
54 | |
6 | |
6 | |
5 | |
5 | |
5 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.