I have a LIST (query output)that has the distinct ID’s from a table “Account”.
Using that LIST I am LOOPING over a SELECT query and basically extracting some COUNT .
I am having trouble getting the COUNT value on an individual basis.
The table “Account” has the fields
1) contract_ID ,
2)vAccountID(primary key) and
3) Status_id (values=’’,V,C).
I am doing the following query
<cfquery name="qryGetid" datasource="#datasource#">
SELECT DISTINCT(contract_ID )
FROM Account
ORDER BY contract_ID DESC
</cfquery>
<!--- account details for each --->
<cfset Z =#ValueList(qryGetid.ID)# >
<cfloop list="#Z#" index="Y" >
<cfquery name="qryGetNiceAccounts" datasource="#dataSource#">
SELECT
DISTINCT(a.contract_ID )
,(SELECT count(vAccountID) FROM Account
WHERE c _ID IN (<cfqueryparam value="#x#" list="yes" cfsqltype="cf_sql_integer" separator=",">)
AND Status_id = 'V' ) AS Valid_AcntV
,(SELECT count(vAccountID) FROM Account
WHERE c _ID IN (<cfqueryparam value="#x#" list="yes" cfsqltype="cf_sql_integer" separator=",">)
AND Status_id = 'C' ) AS Valid_AcntC
FROM Account a
WHERE
a.contract_ID IN (<cfqueryparam value="#x#" list="yes" cfsqltype="cf_sql_integer" separator="," >)
ORDER BY contract_ID DESC
</cfquery>
The query =”qryGetNiceAccounts” is returning only one value for “Valid_AcntCount” even for different “c_ID” in the list .
Example if the “Account” table has the values
contract_ID count(vID)/ v_Accoun t=’v’ count(vID)/ v_Accoun t=’c’
123 10 220
124 05 110
123 01 0
contract_ID count(vID)/ v_Accoun t=’v’ count(vID)/ v_Accoun t=’c’
123 10 220
124 10 220
123 10 220
Basically I am having trouble getting COUNTS for individual IDs.
Side note:-When I do a dump the Input “contract_ID ” is showing as 123, 123 123 rather than 123,124,125
This is generally a bad idea. Especially “getting some counts” is one of the easiest things to do directly in SQL and more often than not there is no reason to execute a SELECT query in a loop. Avoid that whenever you can for performance reasons.
And in your case it’s perfectly avoidable (it even makes your code a lot simpler), just change your SQL:
You don’t need the other query at all, neither do you need the loop.
An alternative way to express the same would be this:
This would hit the
v_Accountview only once. You must determine for yourself what is the most efficient query.