无法使用LIST,<cfloop>和<cfquery> </cfquery> </cfloop>获取所需的输出

时间:2010-12-02 16:10:25

标签: sql coldfusion

我有一个LIST(查询输出),它具有来自表“Account”的不同ID。 使用该LIST我在SELECT查询上循环并基本上提取一些COUNT。 我无法单独获取COUNT值。

表“帐户”包含字段 1)contract_ID,
2)vAccountID(主键)和 3)Status_id(values ='',V,C)。

我正在进行以下查询

    <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>

query =“qryGetNiceAccounts”只返回“Valid_AcntCount”的一个值,即使列表中的“c_ID”不同。

示例如果“帐户”表具有值

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

基本上我无法获得个人ID的COUNTS。

旁注: - 当我进行转储时,输入“contract_ID”显示为123,123 123而不是123,124,125

2 个答案:

答案 0 :(得分:4)

我认为你过度复杂了,它可以通过一些带有几个连接的查询来解决:

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

如果我正确地解读你的问题,你应该做你想做的事。

答案 1 :(得分:2)

  

[...]使用该LIST我正在循环SELECT查询并基本上提取一些COUNT。 [...]

这通常是一个坏主意。特别是“获得一些计数”是直接在SQL中执行的最容易的事情之一,并且通常没有理由在循环中执行SELECT查询。出于性能原因,请尽量避免使用。

在你的情况下,这是完全可以避免的(甚至使你的代码更简单),只需更改你的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>

您根本不需要其他查询,也不需要循环。

另一种表达方式是:

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

这只会在v_Account视图中点击一次。您必须自己确定什么是最有效的查询。