Solved

Coldfusion query help URGENT

Posted on 2008-06-10
7
181 Views
Last Modified: 2013-12-24
I am trying to build the dynamic cfquery but does not seems to be working correctly it works fine when there is a asingle course but when multiple it does not .
Please help

<cfquery name="getStepsCompleted" datasource="cfelsunrise" dbtype="ODBC">
					select max(CompletionDate) as completiondate,CourseID
					from employeetraining
					where EmployeeID = #getPairs.GTLID# 
					<!---<cfif getPairs.CourseID eq 1600>
						and CourseID in (1600)
					</cfif>--->
					<cfif getPairs.CourseID eq 1634>
						or CourseID in (1647, 1648)
					</cfif>
					<cfif getPairs.CourseID eq 1635>
						or CourseID in (1645,1646)
						</cfif>
					<cfif getPairs.CourseID eq 1636>
						or CourseID in (1649,1650)
					</cfif>
					<cfif getPairs.CourseID eq 1632>
						or CourseID in (1651,1652)
					</cfif>
					<cfif getPairs.CourseID eq 1637>
						or CourseID in (1653,1654)
				</cfif>
					<cfif getPairs.CourseID eq 1633>
						or CourseID in (1655,1656)
					</cfif>
						group by CourseID 
				</cfquery>

Open in new window

0
Comment
Question by:erikTsomik
  • 5
  • 2
7 Comments
 
LVL 4

Expert Comment

by:msfletch
ID: 21756171
Erik,

You are missing an AND statement between the "EmployeeID = #getPairs.GTLID#" and the "CourseID". Try attached code.
<cfset courseList = "">
<cfif getPairs.CourseID eq 1634>
	<cfif (courseList) IS NOT "">
		<cfset courseList = "#courseList#,1647, 1648">
	<cfelse>
		<cfset courseList = "1647, 1648">
	</cfif>
</cfif>
<cfif getPairs.CourseID eq 1635>
	<cfif (courseList) IS NOT "">
		<cfset courseList = "#courseList#,1645,1646">
	<cfelse>
		<cfset courseList = "1645,1646">
	</cfif>
</cfif>
<cfif getPairs.CourseID eq 1636>
	<cfif (courseList) IS NOT "">
		<cfset courseList = "#courseList#,1649,1650">
	<cfelse>
		<cfset courseList = "1649,1650">
	</cfif>
</cfif>
<cfif getPairs.CourseID eq 1632>
	<cfif (courseList) IS NOT "">
		<cfset courseList = "#courseList#,1651,1652">
	<cfelse>
		<cfset courseList = "1651,1652">
	</cfif>
</cfif>
<cfif getPairs.CourseID eq 1637>
	<cfif (courseList) IS NOT "">
		<cfset courseList = "#courseList#,1653,1654">
	<cfelse>
		<cfset courseList = "1653,1654">
	</cfif>
</cfif>
<cfif getPairs.CourseID eq 1633>
	<cfif (courseList) IS NOT "">
		<cfset courseList = "#courseList#,1655,1656">
	<cfelse>
		<cfset courseList = "1655,1656">
	</cfif>
</cfif>
 
<cfquery name="getStepsCompleted" datasource="cfelsunrise" dbtype="ODBC">
	SELECT max(CompletionDate) as completiondate,
		CourseID
	FROM employeetraining
	WHERE EmployeeID = #getPairs.GTLID# 
		AND CourseID IN (#courseList#)
	GROUP BY CourseID 
</cfquery>

Open in new window

0
 
LVL 19

Author Comment

by:erikTsomik
ID: 21756334
i did not miss anyhting
If you look at the condition that where it gets added
0
 
LVL 4

Expert Comment

by:msfletch
ID: 21756397
Am I missing something? In the code you included in your question, the only AND clause is commented out. Also, if you are going to add an AND clause and a bunch of OR clauses, then you probably need to use parenthesis around all the OR statements or the AND clause is escaped in the second OR.

What you have is something like:

condition1 AND condition2 OR condition3 OR condition4

Which is the same as saying:

(condition1 AND condition2)
OR
(condition3)
OR
(condition4)

This works if you are trying to get information for the employee or the course. If I understand what you want (getting info for employee and course), then it should be more like:

(condition1) AND (condition2 OR condition3 OR condition4)

... which produces the logic:

(condition1 AND condition2)
OR
(condition1 AND condition3)
OR
(condition1 AND condition4)

The query is pretty straight forward and I can't really think of anything else it could be.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 4

Expert Comment

by:msfletch
ID: 21756440
Erik,

If the "getPairs.CourseID" is supposed to return multiple values, then the problem will be in the way you output that from the "getPairs" query. Is it an array or list? If so, then you can try something like:

<cfset courseList = #ArrayToList(getPairs.CourseID, ",")#>
<cfset courseList = #REReplace(courseList, "1634", "1647,1648", "ALL")#>
<cfset courseList = #REReplace(courseList, "1635", "1645,1646", "ALL")#>
<cfset courseList = #REReplace(courseList, "1636", "1649,1650", "ALL")#>
<cfset courseList = #REReplace(courseList, "1632", "1651,1652", "ALL")#>
<cfset courseList = #REReplace(courseList, "1637", "1653,1654", "ALL")#>
<cfset courseList = #REReplace(courseList, "1633", "1655,1656", "ALL")#>

<cfquery name="getStepsCompleted" datasource="cfelsunrise" dbtype="ODBC">
      SELECT max(CompletionDate) as completiondate,CourseID
      FROM employeetraining
      WHERE EmployeeID = #getPairs.GTLID# AND
            (CourseID in (#courseList#)
      GROUP BY CourseID
</cfquery>

Otherwise, you may have to address the way the first query returns its values.
0
 
LVL 4

Expert Comment

by:msfletch
ID: 21756448
Sorry ... missing parenthesis after "(CourseID in (#courseList#)". Should be:

(CourseID in (#courseList#))
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 21756469
it is an array
0
 
LVL 4

Accepted Solution

by:
msfletch earned 500 total points
ID: 21756517
In that case, an EQ will not work in the WHERE clause. Easiest is to convert it to a list so you can then swap out the CourseIDs as you do currently in the WHERE clause, and then simply include the list in the IN statement.

Did the included code snippet not work?
<cfset courseList = #ArrayToList(getPairs.CourseID, ",")#>
<cfset courseList = #REReplace(courseList, "1634", "1647,1648", "ALL")#>
<cfset courseList = #REReplace(courseList, "1635", "1645,1646", "ALL")#>
<cfset courseList = #REReplace(courseList, "1636", "1649,1650", "ALL")#>
<cfset courseList = #REReplace(courseList, "1632", "1651,1652", "ALL")#>
<cfset courseList = #REReplace(courseList, "1637", "1653,1654", "ALL")#>
<cfset courseList = #REReplace(courseList, "1633", "1655,1656", "ALL")#>
 
<cfquery name="getStepsCompleted" datasource="cfelsunrise" dbtype="ODBC">
      SELECT max(CompletionDate) as completiondate,CourseID
      FROM employeetraining
      WHERE EmployeeID = #getPairs.GTLID# 
            AND (CourseID in (#courseList#))
      GROUP BY CourseID 
</cfquery>

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CFFILE upload help 98 160
spamming  on Hosted svrs? 6 91
Point a domain to a 4rd party web host without changing nameservers 2 30
Run ColdFusion website locally 1 21
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question