kimcarr
asked on
query inner join vs cfloop
I am fairly new to both SQL and CF. I have 2 tables. The first table named drug contains an keynum, name and type column.
It looks like this
NAME Type KeyNum
Secobarbital Depressant 1
Methamphetamine Stimulants 2
Cocaine Stimulants 3
Toluene Inhalants 4
Paint Inhalants 5
The second table named evalform contains 7 columns where data is collected from an online form multiple select box for each type of drug (ie depressant, stimulant, inhalant etc.) and some of the fields contain a delimited list of drugs while other fields will only contain the name of only 1 drug.
I have tried the following loop method but only get the last record on the output (I limited my search to the depressants only until I get it working).
<cfquery name="GetDepr" datasource="dre_new" dbtype="ODBC">
Select * from drug
WHERE type = 'depressants'
</cfquery>
<cfloop query="GetDepr"><cfset drugname = '#GetDepr.Name#'>
<cfquery name="GetDrugNum" datasource="dre_new" dbtype="ODBC">
Select conf_CNS_Depr_list
from dreeval
where conf_CNS_Depr_list like '#Variables.drugname#'
</cfquery>
</cfloop>
<table>
<cfoutput query="GetDepr">
<tr><td>#GetDepr.Name# - #Variables.drugname#</td>< td>#GetDru gNum.Recor dCount#</t d></tr> </cfoutput>
</table>
Then I tried an inner join but this does not recognize any fields that contain a delimited list
Select drug.Name,count(dreeval.co nf_CNS_Dep r_list) as dn
FROM drug
INNER JOIN dreeval
ON drug.name LIKE dreeval.conf_CNS_Depr_list
Group by drug.name
</cfquery>
<cfoutput query="getdepr" group="name">#GetDepr.Name # #GetDepr.dn#<br>
</cfoutput>
Please tell me what I'm doing wrong in the output in the loop or if there is something I can do in the inner join to match a delimited list.
TIA,
Kim
It looks like this
NAME Type KeyNum
Secobarbital Depressant 1
Methamphetamine Stimulants 2
Cocaine Stimulants 3
Toluene Inhalants 4
Paint Inhalants 5
The second table named evalform contains 7 columns where data is collected from an online form multiple select box for each type of drug (ie depressant, stimulant, inhalant etc.) and some of the fields contain a delimited list of drugs while other fields will only contain the name of only 1 drug.
I have tried the following loop method but only get the last record on the output (I limited my search to the depressants only until I get it working).
<cfquery name="GetDepr" datasource="dre_new" dbtype="ODBC">
Select * from drug
WHERE type = 'depressants'
</cfquery>
<cfloop query="GetDepr"><cfset drugname = '#GetDepr.Name#'>
<cfquery name="GetDrugNum" datasource="dre_new" dbtype="ODBC">
Select conf_CNS_Depr_list
from dreeval
where conf_CNS_Depr_list like '#Variables.drugname#'
</cfquery>
</cfloop>
<table>
<cfoutput query="GetDepr">
<tr><td>#GetDepr.Name# - #Variables.drugname#</td><
</table>
Then I tried an inner join but this does not recognize any fields that contain a delimited list
Select drug.Name,count(dreeval.co
FROM drug
INNER JOIN dreeval
ON drug.name LIKE dreeval.conf_CNS_Depr_list
Group by drug.name
</cfquery>
<cfoutput query="getdepr" group="name">#GetDepr.Name
</cfoutput>
Please tell me what I'm doing wrong in the output in the loop or if there is something I can do in the inner join to match a delimited list.
TIA,
Kim
When you do the CFSET, don't include the query identifier on the variable (ie. <CFSET drugname = '#Name#'>)..
It is a common mistake to do that, but when you do, it only gives you the first record of the query. There is no need to specify the query identifier because you are already CFLOOP'ing based on that query..
That should work for ya, i believe..
I personally would code the CFSET like this:
<CFSET drugname = name>
It is a common mistake to do that, but when you do, it only gives you the first record of the query. There is no need to specify the query identifier because you are already CFLOOP'ing based on that query..
That should work for ya, i believe..
I personally would code the CFSET like this:
<CFSET drugname = name>
ASKER
Paulkd,
The result I am wanting is to get a list of drugs and their RecordCount that have been entered into the evalform table. An example of the rows in the evalform table is as follows.
conf_csn_depr_list KeyNum
Alprazolam 1
Alprazolam,Benzodiazepines ,Hydroxyal prazolam 2
Alprazolam,Benzodiazepines 3
Another column is
conf_csn_stim_list KeyNum
Cocaine 5
Amphetamine,Cocaine,Metham phetamine 20
dlewis9,
The loop itself is working but the output is not. It is only reporting on the last variable
Sample output
#GetDepr.Name# - #Variables.drugname# #GetDrugNum.RecordCount#
Alprazolam - Chlorpheniramine 0
Benzodiazepines - Chlorpheniramine 0
Chlorpheniramine - Chlorpheniramine 0
I need it to compare Alprazolam with Alprazolam, Benzodiazepines with Benzodiazepines but only looks at the last record which is Chlorpheniramine.
Thanks,
Kim
The result I am wanting is to get a list of drugs and their RecordCount that have been entered into the evalform table. An example of the rows in the evalform table is as follows.
conf_csn_depr_list KeyNum
Alprazolam 1
Alprazolam,Benzodiazepines
Alprazolam,Benzodiazepines
Another column is
conf_csn_stim_list KeyNum
Cocaine 5
Amphetamine,Cocaine,Metham
dlewis9,
The loop itself is working but the output is not. It is only reporting on the last variable
Sample output
#GetDepr.Name# - #Variables.drugname# #GetDrugNum.RecordCount#
Alprazolam - Chlorpheniramine 0
Benzodiazepines - Chlorpheniramine 0
Chlorpheniramine - Chlorpheniramine 0
I need it to compare Alprazolam with Alprazolam, Benzodiazepines with Benzodiazepines but only looks at the last record which is Chlorpheniramine.
Thanks,
Kim
Kim,
I am a little confused about the EVALFORM table - are you saying it looks like this
http://freespace.virgin.net/paul.dunderdale/drug.htm
but with four more columns...
I am a little confused about the EVALFORM table - are you saying it looks like this
http://freespace.virgin.net/paul.dunderdale/drug.htm
but with four more columns...
ASKER
Yes that is what it looks like, I did a screen shot that you can see at this address
http://teexweb.tamu.edu/kim/kim.cfm
Kim
http://teexweb.tamu.edu/kim/kim.cfm
Kim
Kim,
Apologies for dragging this out, I don't know what you mean by comparing Alprazolam with Alprazolam etc..
I can produce code that can cycle through each specific drug and tell you how many times (records) this drug is listed in conf_CNS_Depr_list, then how many times it is listed in conf_CNS_stim_list etc..
Apologies for dragging this out, I don't know what you mean by comparing Alprazolam with Alprazolam etc..
I can produce code that can cycle through each specific drug and tell you how many times (records) this drug is listed in conf_CNS_Depr_list, then how many times it is listed in conf_CNS_stim_list etc..
ASKER
That is exactly what I need. You can see what output I'm getting at this address
http://teexweb.tamu.edu/kim/drugsused.cfm
Thanks a ton!
Kim
http://teexweb.tamu.edu/kim/drugsused.cfm
Thanks a ton!
Kim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You have 2 tables
Table 1
KeyNum
=============
1
2
3
4
5
NAME
=============
Secobarbital
Methamphetamine
Cocaine
Toluene
Paint
Type
=============
Stimulants
Depressant
Stimulants
Inhalants
Inhalants
Inhalants
Give me a demo of the second table
My guess at table 1
Col1 = Toluene,Secobarbital,Secob arbital
Col2 = Secobarbital,Toluene
Col3 = Secobarbital,Paint
Col4 = Paint,Cocaine
Col5 = Secobarbital,Cocaine
Col6 = Secobarbital
Col7 = Paint
Is this rows or columns???
Table 1
KeyNum
=============
1
2
3
4
5
NAME
=============
Secobarbital
Methamphetamine
Cocaine
Toluene
Paint
Type
=============
Stimulants
Depressant
Stimulants
Inhalants
Inhalants
Inhalants
Give me a demo of the second table
My guess at table 1
Col1 = Toluene,Secobarbital,Secob
Col2 = Secobarbital,Toluene
Col3 = Secobarbital,Paint
Col4 = Paint,Cocaine
Col5 = Secobarbital,Cocaine
Col6 = Secobarbital
Col7 = Paint
Is this rows or columns???
My results would look like this
Secobarbital
conf_csn_depr_list - 3
conf_csn_stim_list - 4
next list - 0
next list - 14
Methamphetamine
conf_csn_depr_list - 3
conf_csn_stim_list - 5
next list - 0
etc...
or
conf_csn_stim_list
Secobarbital - 6
Methamphetamine - 3
next drug - 5
next drug - 6
conf_csn_depr_list
Secobarbital - 0
Methamphetamine - 0
next drug - 4
next drug - 1
If this is the type of output you require say so and we'll finish the task...
Secobarbital
conf_csn_depr_list - 3
conf_csn_stim_list - 4
next list - 0
next list - 14
Methamphetamine
conf_csn_depr_list - 3
conf_csn_stim_list - 5
next list - 0
etc...
or
conf_csn_stim_list
Secobarbital - 6
Methamphetamine - 3
next drug - 5
next drug - 6
conf_csn_depr_list
Secobarbital - 0
Methamphetamine - 0
next drug - 4
next drug - 1
If this is the type of output you require say so and we'll finish the task...
ASKER
Paulkd
No, a depressant will only appear in the conf_cns_depr_list and no other list. I think what dlewis9 suggested will work. Thank you all for your time and effort. I truly appreciate it!
Kim
No, a depressant will only appear in the conf_cns_depr_list and no other list. I think what dlewis9 suggested will work. Thank you all for your time and effort. I truly appreciate it!
Kim
ASKER
I needed to include the output in the loop! Thanks for all your help!
Kim
Kim
OK, scratch what I said above..it really doesn't apply in this situation..only if you are outside of a CFLOOP or CFQUERY.
I see two possible problems..
1) The queries may be working correctly (look at your debugging info) but the output is in the wrong place..it needs to be up in the CFLOOP.
2) I've never used LIKE in a query when comparing to a comma seperated list like that..does it work? I would use the % wildcard for that..
Lemme see, try something like this:
<cfquery name="GetDepr" datasource="dre_new" dbtype="ODBC">
Select * from drug
WHERE type = 'depressants'
</cfquery>
<table>
<cfloop query="GetDepr">
<cfset drugname = '#GetDepr.Name#'>
<cfquery name="GetDrugNum" datasource="dre_new" dbtype="ODBC">
Select conf_CNS_Depr_list
from dreeval
where conf_CNS_Depr_list like '%#Variables.drugname#%'
</cfquery>
<CFOUTPUT>
<tr>
<td>#GetDepr.Name# - #Variables.drugname#</td>
<td>#GetDrugNum.RecordCoun t#</td>
</tr>
</CFOUTPUT>
</cfloop>
</table>
too bad i can't test it without the database :) sorry!
I see two possible problems..
1) The queries may be working correctly (look at your debugging info) but the output is in the wrong place..it needs to be up in the CFLOOP.
2) I've never used LIKE in a query when comparing to a comma seperated list like that..does it work? I would use the % wildcard for that..
Lemme see, try something like this:
<cfquery name="GetDepr" datasource="dre_new" dbtype="ODBC">
Select * from drug
WHERE type = 'depressants'
</cfquery>
<table>
<cfloop query="GetDepr">
<cfset drugname = '#GetDepr.Name#'>
<cfquery name="GetDrugNum" datasource="dre_new" dbtype="ODBC">
Select conf_CNS_Depr_list
from dreeval
where conf_CNS_Depr_list like '%#Variables.drugname#%'
</cfquery>
<CFOUTPUT>
<tr>
<td>#GetDepr.Name# - #Variables.drugname#</td>
<td>#GetDrugNum.RecordCoun
</tr>
</CFOUTPUT>
</cfloop>
</table>
too bad i can't test it without the database :) sorry!
it was hand-written for example only, the code itself would have not produced incorrect results. All the best.
Sorry, i hit the refresh button and posted my comment again..it's been a rough day..
You've not actually said what you want to accomplish (e.g. user will select x,y,z and the resulting page should display x,y,z,a,b,c etc), also can you post the form code, and a couple of example rows from the evalform table.