TypePatient
[ednum] int NOT NULL, PK
[BackgroundID] int NOT NULL, FK
[Patient_No] varchar(50) NULL, FK
[Last_Name] varchar(30) NULL,
[First_Name] varchar(30) NULL,
[ADateTime] datetime NULL,
Treat
[ID] int NOT NULL, PK
[Ednum] numeric(10, 0) NOT NULL, FK
[Doctor] char(50) NULL,
[Dr_ID] numeric(10, 0) NULL,
background
[ID] int NOT NULL, PK
[Patient_No] varchar(50) NULL, FK
[Last_Name] char(30) NULL,
[First_Name] char(30) NULL,
[DateofBirth] datetime NULL,
pdiagnose
[ID] int NOT NULL, PK
[Ednum] int NOT NULL, FK
[DSMNo] char(10) NULL,
[DSMNoIndex] char(5) NULL,
substance
[ID] int NOT NULL, PK
[Ednum] int NOT NULL, FK
[Substance] varchar(120) NULL,
DXCAT
[id] int NULL, PK
[dx_description] char(100) NULL,
[dx_code] char(10) NULL,
[dx_category_description] char(100) NULL,
[diagnosis_category_code] char(10) NULL)
Substance | ||
ID | Ednum | Substance |
1 | 100 | Alcohol Dependence |
4 | 200 | Caffeine Dependence |
5 | 210 | Cigarettes |
dxcat | ||||
id | dx_description | dx_code | dx_category_description | diagnosis_category_code |
10 | Tipsy | zzz | Alcohol | SA |
20 | Mellow | ppp | Mary Jane | SA |
30 | Spacey | fff | LSD | SA |
50 | Smoker | ggg | Nicotine | SA |
pdiagnose |
| |||||||
ID | Ednum | DSMNo | Diagnosis |
| ||||
1 | 100 | zzz | Alcohol |
| ||||
2 | 100 | ddd | Caffeine |
| ||||
3 | 210 | ggg | Smoker |
| ||||
4 | 130 | ppp | Mary Jane |
| ||||
TypePatient | ||||||||
ednum | Patient_No | Last_Name | First_Name | ADateTime | ||||
100 | sssstttt | Wolly | Polly | 12/4/2013 | ||||
130 | rrrrqqqq | Jolly | Molly | 12/8/2013 | ||||
200 | bbbbcccc | Wop | Doo | 12/12/2013 | ||||
210 | vvvvwww | Jazz | Razz | 12/14/2013 | ||||
Treat | |||
ID | Ednum | Doctor | Dr_ID |
2500 | 100 | Welby, Marcus | 1000 |
2550 | 200 | Welby, Marcus | 1000 |
3000 | 210 | Welby, Marcus | 1000 |
3050 | 130 | Welby, Marcus | 1000 |
background | ||||
ID | Patient_No | Last_Name | First_Name | DateofBirth |
2 | sssstttt | Wolly | Polly | 8/6/1974 |
3 | rrrrqqqq | Jolly | Molly | 3/10/1987 |
5 | bbbbcccc | Wop | Doo | 8/12/1957 |
6 | vvvvwww | Jazz | Razz | 7/16/1995 |
Desired output:
Staff ID | Doctor | Patient_No | Client Name | Date of Service | Ednum | DX Code | DX Cat | DX Desc | Substance |
1000 | Welby, Marcus | bbbcccc | Wop, Doo | 12/12/2013 | 200 | Caffeine Dependence | |||
1000 | Welby, Marcus | rrrqqq | Jolly, Molly | 12/8/2013 | 130 | ppp | SA | Mary Jane | |
1000 | Welby, Marcus | sssttt | Wolly, Polly | 12/4/2013 | 100 | zzz | SA | Alcohol | |
1000 | Welby, Marcus | sssttt | Wolly, Polly | 12/4/2013 | 100 | ddd | SA | LSD | |
1000 | Welby, Marcus | sssttt | Wolly, Polly | 12/4/2013 | 100 | Alcohol Dependence | |||
1000 | Welby, Marcus | vvvvwww | Jazz, Razz | 12/14/2013 | 210 | ggg | SA | Smoker | |
1000 | Welby, Marcus | vvvvwww | Jazz, Razz | 12/14/2013 | 210 | Cigarettes |
A patient is assigned an ednum. There are two different menus for staff to enterdiagnoses. Each menu stores the entries in a different table. The two tables are substance and pdiagnose. A patient’s diagnosis for a substance abuse can be entered in one table and not the other. The number of entries for different substances for each patient can vary between the two tables. John Doe might be entered for alcohol and caffeine abuse in the pdiagnosis table and entered only for caffeine abuse in the substance table. They are only linked by the ednum which has nothing to do with the diagnosis/substance. The substance entered in one table is not linked to the substance entered in the other. A query will not put an entry for alcohol from the pdiagnosis table on the same row as an alcohol entry from the substance table except by chance. That is the reason for the way the query is written.
The query accepts parameters for a Dr ID and a start and end date. It takes about 7 to 15 seconds to run. Hard coding the dates cuts it down to about a second.
I might be able to select directly from the union all query instead of having it separate. But then I’m not sure about the order by clauses using aliases.
Is there a way to rewrite the query to speed it up?
I did not design the tables or come up with the process of entering diagnoses. It can’t be changed at this time.
Please let me know if you notice any inconsistencies between the DDLs, data, and output. I did a lot of editing.
Thanks for any suggestions.
with cte_dxcat (Dr_ID, Doctor, Patient_No,Last_Name, First_Name, Adatetime,Ednum, dx_code,diagnosis_category_code,dx_description,substance, DateofBirth) as (Select distinct t.Dr_ID, t.Doctor, TP.Patient_No,TP.Last_Name, TP.First_Name, TP.Adatetime as 'Date of Service',TP.Ednum, DXCAT.dx_code,DXCAT.diagnosis_category_code,DXCAT.dx_description, null as 'substance',BG.DateofBirth From TypePatient TP inner join treat t on TP.ednum = t.Ednum inner join background BG on BG.Patient_No = TP.Patient_No inner join pdiagnose PD on TP.Ednum = PD.Ednum inner join Live_Knowledge.dbo.VA_DX_CAT_MAPPING DXCAT on DXCAT.dx_code = PD.DSMNo Where (TP.Adatetime >= convert(varchar(10), :ST, 121)+ ' 00:00:00.000' and TP.Adatetime <= convert(varchar(10), :SP, 121)+ ' 23:59:59.000') and DXCAT.diagnosis_category_code = 'SA' and t.Dr_ID =:DBLookupComboBox2 ), cte_substance (Dr_ID, Doctor, Patient_No,Last_Name, First_Name,Adatetime, Ednum, dx_code,diagnosis_category_code,dx_description,Substance,DateofBirth) as (Select distinct t.Dr_ID, t.Doctor, TP.Patient_No,TP.Last_Name, TP.First_Name, TP.Adatetime as 'Date of Service', TP.Ednum, null as 'dx_code',null as 'diagnosis_category_code',null as 'dx_description',s.Substance, BG.DateofBirth From TypePatient TP inner join treat t on TP.ednum = t.Ednum inner join background BG on BG.Patient_No = TP.Patient_No inner join pdiagnose PD on TP.Ednum = PD.Ednum inner join substance s on TP.Ednum = s.Ednum Where (TP.Adatetime >= convert(varchar(10), '12/1/2013', 121)+ ' 00:00:00.000' and TP.Adatetime <= convert(varchar(10), '12/31/2013', 121)+ ' 23:59:59.000') and t.Dr_ID =:DBLookupComboBox2 ), cte_all (Dr_ID, Doctor, Patient_No,Last_Name, First_Name,Adatetime, Ednum, dx_code,diagnosis_category_code,dx_description,Substance,DateofBirth) as (select cte_dxcat.Dr_ID as 'Staff ID', cte_dxcat.Doctor as 'Doctor', cte_dxcat.Patient_No as 'Patient_No', cte_dxcat.Last_Name as 'Last',cte_dxcat.First_Name as 'First', cte_dxcat.Adatetime as 'Date of Service',cte_dxcat.Ednum as 'Ednum', cte_dxcat.dx_code as 'DX Code',cte_dxcat.diagnosis_category_code as 'DX Category Code', cte_dxcat.dx_description as 'DX Description', cte_dxcat.substance as 'Substance',cte_dxcat.DateofBirth as 'DOB' from cte_dxcat union all select cte_substance.Dr_ID as 'Staff ID', cte_substance.Doctor as 'Doctor', cte_substance.Patient_No as 'Patient_No', cte_substance.Last_Name as 'Last',cte_substance.First_Name as 'First', cte_substance.Adatetime as 'Date of Service',cte_substance.Ednum as 'Ednum', cte_substance.dx_code as 'DX Code',cte_substance.diagnosis_category_code as 'DX Category Code', cte_substance.dx_description as 'DX Description', cte_substance.substance as 'Substance',cte_substance.DateofBirth as 'DOB' from cte_substance) select cte_all.Dr_ID as 'Staff ID', cte_all.Doctor as 'Doctor', cte_all.Patient_No as 'Patient_No', (cte_all.Last_Name + ', '+ cte_all.First_Name) as 'Client Name', cte_all.Adatetime as 'Date of Service',cte_all.Ednum as 'Ednum', cte_all.dx_code as 'DX Code',cte_all.diagnosis_category_code as 'DX Category Code', cte_all.dx_description as 'DX Description', cte_all.substance as 'Substance', CONVERT(char(10), cte_all.DateofBirth,101) as 'DOB' from cte_all order by cte_all.Patient_No,cte_all.Adatetime