Solved

Subquery in coldfusion

Posted on 2014-03-24
4
636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

617 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