Unable to get the required output using a LIST, <cfloop> and <cfquery>
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)vAccount开发者_高级运维ID(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
I think you're overcomplicating it, and it can be solved with a single query with a few joins:
SELECT a.contract_ID, COUNT(vav.vAccountID) AS Valid_AcntV, COUNT(vac.vAccountID) AS Valid_AcntC FROM Account a LEFT JOIN v_Account AS vav ON vav.c_ID = a.contract_ID AND vav.Status_id = 'V' LEFT JOIN v_Account AS vac ON vac.c_ID = a.contract_ID AND vac.Status_id = 'C' GROUP BY a.contract_ID ORDER BY a.contract_ID DESC
Should do what you're after, if I'm deciphering your queries correctly.
[…] Using that LIST I am LOOPING over a SELECT query and basically extracting some COUNT. […]
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:
<cfquery name="qryGetNiceAccounts" datasource="#dataSource#">
SELECT
contract_ID,
(SELECT COUNT(vAccountID) FROM v_Account
WHERE c_ID = a.contract_ID AND Status_id = 'V'
) AS Valid_AcntV,
(SELECT COUNT(vAccountID) FROM v_Account
WHERE c_ID = a.contract_ID AND Status_id = 'C'
) AS Valid_AcntC
FROM
(SELECT contract_ID FROM Account GROUP BY contract_ID) AS a
ORDER BY
contract_ID DESC
</cfquery>
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:
SELECT
a.contract_ID,
SUM(CASE Status_id WHEN 'V' THEN 1 ELSE 0 END) AS Valid_AcntV,
SUM(CASE Status_id WHEN 'C' THEN 1 ELSE 0 END) AS Valid_AcntC
FROM
Account AS a
INNER JOIN v_Account AS c ON c.c_id = a.contract_ID
GROUP BY
a.contract_ID
ORDER BY
a.contract_ID DESC
This would hit the v_Account
view only once. You must determine for yourself what is the most efficient query.
精彩评论