Ask Ben: Adding A Column From Second Query To First Query

Posted May 3, 2007 at 9:44 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

I have two queries and I want to add a column from the second query to the first query. What is the best way to do this?

First off, I just want to say that I am not sure why you would want to do something like that, so maybe (likely?) I am misunderstanding what you mean. If you are talking about joining the two queries and selecting all columns from the first and only one column from the second, that can be done using a simple ColdFusion query of queries which supports inner joins and full outer joins. If you are not talking about that, the only other option is that you simply want to add one column, in order, to the other column.

Since I have no idea what you are talking about, let's quickly look at both. But first, let's build two queries with which to experiment:

  • <!--- Create the first empty query. --->
  • <cfset qOne = QueryNew( "" ) />
  •  
  • <!---
  • Populate the first query with a column of ids
  • (think primary key / identifier).
  • --->
  • <cfset QueryAddColumn(
  • qOne,
  • "id",
  • "CF_SQL_INTEGER",
  • ListToArray( "1,2" )
  • ) />
  •  
  • <!--- Populate the first query with a column of values. --->
  • <cfset QueryAddColumn(
  • qOne,
  • "letter",
  • "CF_SQL_VARCHAR",
  • ListToArray( "A,B" )
  • ) />
  •  
  •  
  • <!--- Create the second empty query. --->
  • <cfset qTwo = QueryNew( "" ) />
  •  
  • <!---
  • Populate the first query with a column of ids
  • (think primary key / identifier).
  • --->
  • <cfset QueryAddColumn(
  • qTwo,
  • "id",
  • "CF_SQL_INTEGER",
  • ListToArray( "2,3" )
  • ) />
  •  
  • <!--- Populate the second query with a column of values. --->
  • <cfset QueryAddColumn(
  • qTwo,
  • "number",
  • "CF_SQL_VARCHAR",
  • ListToArray( "1,2" )
  • ) />

Now we have two queries: one has a column of letters and the other has a column of numbers (both have an identifier column in order to mimic a more real-world situation). If we go the query of queries route, it would look something like this:

  • <!---
  • Join both queries using an outer join (no "ON" where
  • clause defined and no column to match on). Grab all
  • columns from the first query and only ONE column (the
  • one we are adding) from the second column.
  • --->
  • <cfquery name="qThree" dbtype="query">
  • SELECT
  • qOne.*,
  • qTwo.number
  • FROM
  • qOne,
  • qTwo
  • </cfquery>
  •  
  • <!--- Output the resultant cartesian product. --->
  • <cfdump
  • var="#qThree#"
  • label="Query of Queries Method"
  • />

Notice that in our ColdFusion query of queries, we are not specifying any join condition. This acts like a FULL OUTER JOIN and produces the cartesian product of all joined query columns:


 
 
 

 
ColdFusion Query of Queries Full Outer Join  
 
 
 

This adds the entire column of the second query to every row of the first query. Not sure how useful this would be to you unless you included a join condition (in the WHERE clause).

If you actually mean just adding appending a column of one query to the existing columns of another query, well, this is quite easy. If you look above at how we created the original test queries, you might get an idea as to how we are going to do this - just add the column:

  • <!---
  • Add the number column of query two the first query. When
  • doing this, it is essential that we reference the column
  • using array notation (ex. [ "column" ]). If we do NOT do
  • that, then ColdFusion things we are referring to the first
  • column value and NOT the column itself.
  • --->
  • <cfset QueryAddColumn(
  • qOne,
  • "number",
  • "CF_SQL_VARCHAR",
  • qTwo[ "number" ]
  • ) />
  •  
  • <!--- Display the results. --->
  • <cfdump
  • var="#qOne#"
  • label="QueryAddColumn() Method"
  • />

Note that we added the column using a simple QueryAddColumn() method. ColdFusion rocks so hard core! Also note that I needed to refer to the second query's column using array notation:

qTwo[ "number" ]

This is required. Remember, ColdFusion is used to getting cell values from a query - it's not used to referencing the entire record set column. If you were to add this:

qTwo.number

... to the first query, ColdFusion would assume you are trying to pass in the value "1" which is the first column value. Doing so would throw the error:

Object of type class java.lang.String cannot be used as an array

That being said, performing the above action results in the following output:


 
 
 

 
ColdFusion QueryAddColumn()  
 
 
 

This outcome here is quite different than the outcome of the ColdFusion query of queries. Using QueryAddColumn() simply appends the column. It doesn't change the order of either query not does it check any join information; it simply adds the column.

Again, I am not sure what exactly you were going for. The ColdFusion query of queries method seems the more useful of the two, but it didn't sound like what you were trying to do. The QueryAddColumn() is quite literal, but as far as I can see, not very useful (in this type of a scenario). But, if you can find a use for it, I would be excited to see it.



Reader Comments

May 3, 2007 at 1:26 PM // reply »
1 Comments

I have had to do this with data from databases on different servers because I wanted to use the grouping functions of cfoutput.

I always did it by defining a fake field in the first query like so:

select Field1, '' AS Field2
from Table1

Then, with the data from the second database I looped over it and set the field manually.
<cfloop query="FirstQuery">
<!---Logic here to find the right row in the second query--->
<cfset FirstQuery.Field2 = ValueYouWantFromSecondQuery>
</cfloop>

It may not be the best solution, but it was intuitive and easy.


May 3, 2007 at 8:01 PM // reply »
10,743 Comments

@Lyle,

Yeah, I do that a lot. I actually do that even on a single query - sometimes it's easier to do crazy calculation after the fact. For instance, if I have a query that has two aggregate columns or sub-query columns and then I need a third column that averages them (divides one over the other), I find that it's easier to do (0 AS average) and then do the division after the query runs. Easy AND faster (since I don't have to re-calc the other columns for the average).


Jul 5, 2010 at 11:11 AM // reply »
21 Comments

I'm not able to do an outer join with 2 condtitions.

I have 2 queries

Have you ever been able to do this?


Aug 1, 2010 at 8:06 PM // reply »
10,743 Comments

@Nikos,

You mean two conditions in the WHERE clause? I am not sure I have ever had a compiling issue with that?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »