2010-12-02 44 views
0

我有一個LIST(查詢輸出),它具有來自表「Account」的不同ID。 使用該LIST我正在LOOPING SELECT查詢並基本上提取一些COUNT。 我無法單獨獲取COUNT值。無法使用列表獲取所需的輸出,<cfloop>和<cfquery>

表「帳戶」具有字段 1)contract_ID,
2)vAccountID(主鍵)和 3)STATUS_ID(值=」」,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> 

查詢=「qryGetNiceAccounts」在列表中的「Valid_AcntCount」甚至是不同的「C_ID」只返回一個值。

例如,如果「客戶」表中有基本上我有麻煩了各自的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 

附註: - 當我做了轉儲輸入「contract_ID」被顯示爲123,123 123,而不是123124125

+1

如果你做一個,你看到了什麼? – charliegriefer 2010-12-02 16:24:49

+1

附註:當你只能循環查詢本身時,你爲什麼要從查詢中遍歷一個`ValueList()`? – Tomalak 2010-12-02 16:37:07

回答

2

[...]使用該列表我正在LOOPING 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視圖只有一次。您必須自己確定什麼是最有效的查詢。

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 

如果我正確解密您的查詢,應該做你所追求的。