无法定义条件

时间:2011-10-26 08:22:33

标签: sql-server coldfusion

我试图在sql代码中定义条件SR.PRODUCT_STOCK2 > 0,因为它是sum条件的AS,这里是sql代码

    SELECT P.PRODUCT_ID,P.PRODUCT_NAME,PS.MONEY,PR.PRICE,P.BRAND_ID,PS.PRICE,GSL.PRODUCT_STOCK,GSL.PURCHASE_ORDER_STOCK,GSL.SALEABLE_STOCK,P.PRODUCT_DETAIL2,P.BARCOD
<cfif isdefined('attributes.department_id') and len(attributes.department_id)>
    ,(SUM(SR.STOCK_IN)-SUM(SR.STOCK_OUT)) AS PRODUCT_STOCK2
</cfif>
FROM PRODUCT P
    JOIN PRICE_STANDART PS ON P.PRODUCT_ID = PS.PRODUCT_ID
    JOIN PRICE PR ON P.PRODUCT_ID = PR.PRODUCT_ID
    JOIN #DSN2_ALIAS#.GET_STOCK_LAST GSL ON P.PRODUCT_ID = GSL.PRODUCT_ID
    <cfif isdefined('attributes.department_id') and len(attributes.department_id)>
        JOIN #DSN2_ALIAS#.STOCKS_ROW SR ON P.PRODUCT_ID=SR.PRODUCT_ID
    </cfif>
WHERE PS.PURCHASESALES=1 AND PS.PRICESTANDART_STATUS=1 AND P.IS_SALES=1 AND P.IS_PURCHASE=1 AND P.IS_INTERNET=1 AND P.IS_EXTRANET=1
<cfif isdefined('attributes.department_id') and len(attributes.department_id)>
    AND
        (
        <cfloop list="#attributes.department_id#" delimiters="," index="dept_i">
        (SR.STORE = #listfirst(dept_i,'-')# AND SR.STORE_LOCATION = #listlast(dept_i,'-')#)
        <cfif dept_i neq listlast(attributes.department_id,',') and listlen(attributes.department_id,',') gte 1> OR</cfif>
        </cfloop>  
        )
        <cfif isdefined('attributes.is_stock') and attributes.is_stock is 1>
            AND SR.PRODUCT_STOCK2 > 0
        </cfif>
    </cfif>
GROUP BY P.PRODUCT_ID,PR.PRICE,P.PRODUCT_NAME,PS.MONEY,P.BRAND_ID,PS.PRICE,GSL.PRODUCT_STOCK,GSL.PURCHASE_ORDER_STOCK,GSL.SALEABLE_STOCK,P.PRODUCT_DETAIL2,P.BARCOD

错误日志说:无效的列名PRODUCT_STOCK2我知道我错误地定义了条件,我只是不知道如何正确定义它,需要帮助!谢谢大家的帮助!

1 个答案:

答案 0 :(得分:2)

你将不得不使用如下的having子句:

HAVING (SUM(SR.STOCK_IN)-SUM(SR.STOCK_OUT)) > 0