Solved

How to loop a query and update the where clause.

Posted on 2014-03-12
15
338 Views
Last Modified: 2014-03-14
I will do my best to explain this. I need to loop over a query similar to the one below and replace the "Arguments.CarName" in the Where Clause each time it loops with a list of CarNames. The List of CarNames would be a "distinct" list from that same table. could someone please assist? I am using CF10. Thank you.

SELECT COUNT(CarNumber) 
	FROM  dbo.tblCars
	WHERE IncidentDate BETWEEN <cfqueryparam value="#arguments.IncidentDate1#" cfsqltype="cf_sql_date"> AND <cfqueryparam value="#dateAdd('d', 1, arguments.IncidentDate2)#" cfsqltype="cf_sql_date"> )
	and CarName = <cfqueryparam value="#arguments.CarName#" cfsqltype="cf_sql_varchar">

Open in new window

0
Comment
Question by:earwig75
  • 8
  • 7
15 Comments
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Running queries w/in a loop should be avoided if possible. Do you really need to loop? Why not just feed in a list of car names and get the counts for all in single query, ie:

CarName  | NumOfCars
Car AAAA | 22
Car BBB    | 43
Car BBB    | 32
....

Something like ...

SELECT CarName, COUNT(CarNumber)  AS NumOfCars
FROM   dbo.tblCars
WHERE IncidentDate BETWEEN {date1} AND {date2}
AND      CarName IN
(
<cfqueryparam value="#ListOfNames#" list="true" cfsqltype="cf_sql_varchar">
)
0
 

Author Comment

by:earwig75
Comment Utility
I displayed a very abbreviated example but that may work. How would I build the "ListOfNames"? I haven't done this type of ColdFusion coding in quite a while.

Thank you.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Depends. What's the source of the car names? If it's from a query, just use:

<cfset listOfNames = ValueList(yourQueryName.columnName)>
... feed into other query ...
0
 

Author Comment

by:earwig75
Comment Utility
I'm sorry. the example I gave won't work with that solution and it is probably my fault. I was hoping a loop would work. My query looks something like this. I am trying to get the list of values for each car name. It's a query with subqueries.
<cfquery .....>
SELECT 
  (SELECT COUNT(CarNumber) 
	FROM  tblCars
	WHERE (tblCars.Created BETWEEN '1/1/2014' AND '3/1/14' )
	
		   AND      CarName IN  (<cfqueryparam value="#ListOfNames#" list="true" cfsqltype="cf_sql_varchar">)
 ) as CreatedCars, 



  (SELECT COUNT(CarNumber)
    FROM  tblCars
    WHERE (tblCars.Created BETWEEN '1/1/2014' AND '3/1/14') 
	AND	(tblCars.Status = 'Closed')
	AND (ChangePerson NOT IN ('blah', 'blah2'))
   
		   AND      CarName IN  (<cfqueryparam value="#ListOfNames#" list="true" cfsqltype="cf_sql_varchar">)
) as ClosedCars, 
				
  (SELECT COUNT(tblCars.CarNumber) 
	FROM   tblCars INNER JOIN
                      tblCarsIncidents ON tblCars.ID2 = tblCarsIncidents.ID2
	WHERE (tblCars.Created BETWEEN '1/1/2014' AND '3/1/14') 
	 AND (tblCars.Status = 'Closed') AND (tblCarsIncidents = 'Red') AND tblCarsIncidents.Status = 'Closed'
		
		   AND      CarName IN  (<cfqueryparam value="#ListOfNames#" list="true" cfsqltype="cf_sql_varchar">)
     ) as ClosedCarsRed
</cfquery>

Open in new window

0
 

Author Comment

by:earwig75
Comment Utility
So, I'd like to output the query and show a list of Carnames and then numbers for each like this:

CarName     CreatedCars     Closed Cars     ClosedCarsRed
--------------------------------------------------------------------------
CoolCar              5                       7                       4
NeatCar              4                       4                       8
WowCar              4                      8                       10
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Ooops, our posts clashed.  I think I understand now.  Let me look over the subqueries..
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
(Edit fix syntax error).

Looks like all of the subqueries are filtered on the same createdDate range. Try this smaller query first (not tested).  Does it return the right numbers? Then we can add in the incidents table subquery.

SELECT c.CarName
      , COUNT(c.CarNumber) AS CreatedCars
      , SUM(CASE
                WHEN c.Status = 'Closed'  AND c.ChangePerson NOT IN ('blah', 'blah2')  THEN 1
                ELSE 0
              END
        ) as ClosedCars
FROM   tblCars c
WHERE  c.Created BETWEEN '1/1/2014' AND '3/1/14'
GROUP BY c.CarName
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:earwig75
Comment Utility
In my example I only used 3 total queries but I have about ten and most of them have joins like the one you omitted.
I can't test until tomorrow but that looks good so far.
Thank you.
0
 

Author Comment

by:earwig75
Comment Utility
Could I put my original query in a CFC and then send the carname as in invoke argument in a loop somehow?
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Yeah, you can put it in a CFC, but so far, I don't think you need a loop.  If you simply filter the outer query on the car names you want , ie "CoolCar,NeatCar,WowCar" .... the results will contain the totals for each of those car names:

<!--- obviously use cfqueryparam in the real query --->
SELECT c.CarName
      , COUNT(c.CarNumber) AS CreatedCars
      , SUM(CASE
                WHEN c.Status = 'Closed'  AND c.ChangePerson NOT IN ('blah', 'blah2')  THEN 1
                ELSE 0
              END
        ) as ClosedCars
FROM   tblCars c
WHERE  c.Created BETWEEN '1/1/2014' AND '3/1/14'
AND       c.CarName IN ( #ListOfCarNamesYouWant# )  <=== add list as a filter
GROUP BY c.CarName
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
For tomorrow, assuming the base query works, something like this should do it for the subquery. Note, I think the earlier example omitted the name of the color column, so be sure to update it in the query:

SELECT c.CarName
      , COUNT(c.CarNumber) AS CreatedCars
      , SUM(CASE
                WHEN c.Status = 'Closed'  AND c.ChangePerson NOT IN ('blah', 'blah2')  THEN 1
                ELSE 0
              END
        ) AS ClosedCars
      , (
         SELECT COUNT(*)
           FROM    tblCarsIncidents i
           WHERE  i.ID2 = c.ID2
           AND      i.Status = 'Closed'
           AND      i.Status = c.Status
           AND      i.MissingColumnName = 'Red'
      )
        AS ClosedCarsRed
FROM   tblCars c
WHERE  c.Created BETWEEN '1/1/2014' AND '3/1/14'
AND       c.CarName IN
(
<cfqueryparam value="#ListOfCarNamesYouWant#" list="true"
                cfsqltype="(your type here)...">
)
GROUP BY c.CarName
0
 

Author Comment

by:earwig75
Comment Utility
I have one more obstacle with this method. When I output  the query for each row I need to add columns for each CarName that will have some calculations of the other columns. Will that be possible?
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Probably.  All depends on what the calculations are ... if it's some sort of summary of the counts for example, you might just want to do that in CF code.  ie

CarName     CreatedCars     Closed Cars     ClosedCarsRed            Total
------------------------------------------------------------------------------------
CoolCar              5                       7                       4                     16
NeatCar              4                       4                       8                     16
WowCar              4                      8                       10                    22
0
 

Author Comment

by:earwig75
Comment Utility
I am putting percentages and other calculations next to the other numbers. for example:

CarName     CreatedCars     Closed Cars     ClosedCarsRed            Total
------------------------------------------------------------------------------------
CoolCar              10                       7                       7   (75%)             24
NeatCar              4                       4                       8                          16
WowCar              4                      8                       10                         22

I think I have it working by looping over the cfinvoke. Using your method, I'm not sure how I would add other hard-coded columns and other numbers in the existing columns since the rest of the output is coming from the query alone. You answer my original question; I should have provided much more detail.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
(Edit) Well, since you need to aggregate twice: total per car name, total overall you cannot do it in one step. First you have to get the aggregate totals per car. Then calculate the overall totals.

Normally I'd suggest doing that in SQL, using a derived table, like this:

SELECT   t.CarName
              , t.CreatedCars
              , t.ClosedCars
              , t.ClosedCarsRed
              , (t.CreatedCars + t.ClosedCars + t.ClosedCarsRed ) AS TotalCars
FROM   (
                    SELECT  CarName, CreatedCars, ClosedCars, ClosedCarsRed
                    FROM     Table
                    WHERE ....
            )  AS t

Then using the "Total" column to generate percentages in your output loop

<cfoutput query="queryName">
       #ClosedCarsRed#  ( #ClosedCarsRed/Total * 100)# % )
</cfoutput>

But given the complexity of your base query that could get a little ugly.  You might consider using temp tables. Insert the numbers into a temp table. Then do a self join to return the data and totals in a single resultset.

Another option is to run the query as usual. Then add a "Total" column to it:

        <cfset queryAddColumn(yourQuery, "Total", [])>

Then loop through it to update the "Total" value for each row.  

        <cfloop query="yourQuery">
              <!--- calculate total cars in all relevant columns --->
              <!--- where columnNamesYouWantToTotal = "CreatedCars,ClosedCars,..." --->
              <cfset total = 0>
              <cfloop list="#columnNamesYouWantToTotal#" index="colName">
                       <cfset total += val(yourQuery[colName][currentRow])>
              </cfloop>
              <!--- store total --->
              <cfset yourQuery["Total"][currentRow] = total>
        </cfloop>

Then use the "Total" to calculate the percentages as described above.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now