Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Subquery in coldfusion

Posted on 2014-03-24
4
Medium Priority
?
655 Views
Last Modified: 2014-03-25
Hi,

can I do a sub query in ColdFusion?

How can I accomplish a sub query result.

<cfquery name = "QSearchResultRecord"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="r">
  SELECT case when L.ParentIssueID is null then L.IssueId
         else L.ParentIssueID
		 end  as TopIssueID
		 , L.IssueID
		 , L.ParentIssueID
		 , T.TicketNumber
		 , L.TitleOfIssue
		 , I.Status
		 , L.DateCreated
         , C.Category
   FROM LogEntry L
     LEFT JOIN IssueStatus I on I.StatusID = L.StatusID
     LEFT JOIN CategoryType C on C.CategoryTypeID = L.CategoryTypeID
     LEFT JOIN TicketNumber T on T.TicketNumberID = L.TicketNumberID
  <cfif len(FoundIssueIDs) eq 0 >
   where 1 = 2 
  <cfelse>
   where L.IssueID in  (#FoundIssueIDs#)
  </cfif>
  order by 1 desc, case when L.parentIssueID is null then 1 else 2 end 
</cfquery>

 <cfquery name = "QSearchResult"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="rcr">

select * from QSearchResultRecord

</cfquery>

Open in new window

0
Comment
Question by:lulu50
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:gdemaria
ID: 39950945
What is it you want to do?  

You can have a subquery in your select or your join or in your where clause..

(see bold text as examples)

SELECT case when L.ParentIssueID is null then L.IssueId
         else L.ParentIssueID
             end  as TopIssueID
             , L.IssueID
             , L.ParentIssueID
             , T.TicketNumber
             , L.TitleOfIssue
             , I.Status
             , L.DateCreated
         , C.Category
       , (select count(*) from xxxx where issueId = xxx) as CountRelated
  FROM LogEntry L
     LEFT JOIN IssueStatus I on I.StatusID = L.StatusID
     LEFT JOIN CategoryType C on C.CategoryTypeID = L.CategoryTypeID
     LEFT JOIN TicketNumber T on T.TicketNumberID = L.TicketNumberID
    LEFT JOIN (select ONe,  Two, Three
             from aTable) sub on sub.issueId = l.issueID
0
 

Author Comment

by:lulu50
ID: 39950965
Hi gdemaria,

Question:

It has been a long time since I have worked with coldfusion. but I know that I did it before
where I have a query and I can create a subquery to select from the first query.

Something like this:


<cfquery name = "QSearchResultRecord"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="r">
  SELECT case when L.ParentIssueID is null then L.IssueId
         else L.ParentIssueID
		 end  as TopIssueID
		 , L.IssueID
		 , L.ParentIssueID
		 , T.TicketNumber
		 , L.TitleOfIssue
		 , I.Status
		 , L.DateCreated
         , C.Category
   FROM LogEntry L
     LEFT JOIN IssueStatus I on I.StatusID = L.StatusID
     LEFT JOIN CategoryType C on C.CategoryTypeID = L.CategoryTypeID
     LEFT JOIN TicketNumber T on T.TicketNumberID = L.TicketNumberID
  <cfif len(FoundIssueIDs) eq 0 >
   where 1 = 2 
  <cfelse>
   where L.IssueID in  (#FoundIssueIDs#)
  </cfif>
  order by 1 desc, case when L.parentIssueID is null then 1 else 2 end 
</cfquery>

Open in new window


 <cfquery name = "QSearchResult"  datasource='#strDSN#' username='#strUID#' password='#strPWD#' result="rcr">

select * from QSearchResultRecord

</cfquery>

Open in new window

0
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 total points
ID: 39951282
Perhaps you are looking for a "query of query"

This allows you to use the first query as the "table" in the another query.



<cfquery name="getSubSet" dbtype="query">
   select * from QSearchResultRecord   <!=== =the table here is the query name
   where IssueID between 1 and 100
</cfquery>
0
 

Author Closing Comment

by:lulu50
ID: 39953092
Thank you
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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