SQL Server中的列错误无效

时间:2015-05-21 06:19:45

标签: sql sql-server sql-server-2008

我在SQL Server中有这个查询:

SELECT 
    cr.formno, cr.controlid as formid, 
    min(cr.series) as formstartseries,
    max(cr.series) as formendseries,
    (SELECT currentseries 
     FROM af_inventory 
     WHERE objid=cr.controlid) AS afi_currentseries, 
    (SELECT currentseries 
     FROM af_control 
     WHERE objid=cr.controlid) AS afc_currentseries 
FROM cashreceipt cr 
WHERE cr.collector_objid='USR409c49cc:1488078ce62:-7958' 
  AND cr.objid NOT IN (SELECT objid 
                       FROM remittance_cashreceipt 
                       WHERE objid=cr.objid) 
GROUP BY cr.formno, cr.controlid 
HAVING formstartseries <> afi_currentseries 

执行此查询后,会抛出错误:

  

列名formstartseriesafi_currentseries

无效

出了什么问题?

4 个答案:

答案 0 :(得分:2)

SELECT中分配的别名无法在HAVING子句中使用,因为稍后会执行SELECT阶段,请查看Logical Query Processing。 您可以使用cte然后包含WHERE这样的子句:

WITH cte
AS (
    SELECT cr.formno
        ,cr.controlid AS formid
        ,min(cr.series) AS formstartseries
        ,max(cr.series) AS formendseries
        ,(
            SELECT currentseries
            FROM af_inventory
            WHERE objid = cr.controlid
            ) AS afi_currentseries
        ,(
            SELECT currentseries
            FROM af_control
            WHERE objid = cr.controlid
            ) AS afc_currentseries
    FROM cashreceipt cr
    WHERE cr.collector_objid = 'USR409c49cc:1488078ce62:-7958'
        AND cr.objid NOT IN (
            SELECT objid
            FROM remittance_cashreceipt
            WHERE objid = cr.objid
            )
    GROUP BY cr.formno
        ,cr.controlid
    )
SELECT *
FROM cte
WHERE formstartseries <> afi_currentseries

答案 1 :(得分:1)

您不能在HAVING子句中使用ALIASed列名。请尝试以下方法:

SELECT
  cr.formno,
  cr.controlid AS formid,
  MIN(cr.series) AS formstartseries,
  MAX(cr.series) AS formendseries,
  (
    SELECT
      currentseries
    FROM
      af_inventory
    WHERE
      objid = cr.controlid
  ) AS afi_currentseries,
  (
    SELECT
      currentseries
    FROM
      af_control
    WHERE
      objid = cr.controlid
  ) AS afc_currentseries
FROM
  cashreceipt cr
WHERE
  cr.collector_objid = 'USR409c49cc:1488078ce62:-7958'
  AND cr.objid NOT IN ( SELECT
                          objid
                        FROM
                          remittance_cashreceipt
                        WHERE
                          objid = cr.objid )
GROUP BY
  cr.formno,
  cr.controlid
HAVING
  MIN(cr.series) 
<> 
(SELECT currentseries 
 FROM af_inventory 
 WHERE objid=cr.controlid)

答案 2 :(得分:1)

你不能在GroupBy中使用列的别名并且尝试下面的

select
cr.formno,
formid,
formstartseries,
formendseries,
afi_currentseries,
afc_currentseries
from
(select 
cr.formno, cr.controlid as formid, 
min(cr.series) as formstartseries,
max(cr.series) as formendseries,
(select currentseries from af_inventory where objid=cr.controlid) as  afi_currentseries, 
(select currentseries from af_control where objid=cr.controlid) as afc_currentseries 
from cashreceipt cr 
where cr.collector_objid='USR409c49cc:1488078ce62:-7958' 
and cr.objid not in (select objid from remittance_cashreceipt where objid=cr.objid))as T 
group by cr.formno, formid, 
having formstartseries <> afi_currentseries 

答案 3 :(得分:1)

只需更改上一个代码:

...
HAVING formstartseries <> afi_currentseries 

到这个

...
HAVING min(cr.series) <> (SELECT currentseries 
                          FROM af_inventory 
                          WHERE objid=cr.controlid)