2011-05-05 29 views
2

這裏是我的查詢不起作用,因爲它顯然違反了HAVING子句的規則:如何過濾合併列的值的分組查詢?

SELECT 
     COALESCE(Publisher.name, Affiliate.name) AS Publisher 
    ,dbo.SumKeys(Item.id) AS ItemIDs 
FROM 
    Item 
    INNER JOIN ItemAccountingStatus ON Item.item_accounting_status_id = ItemAccountingStatus.id 
    INNER JOIN Affiliate ON Item.affid = Affiliate.affid 
    INNER JOIN Currency AffiliateCurrency ON Affiliate.currency_id = AffiliateCurrency.id 
    INNER JOIN Campaign ON Item.pid = Campaign.pid 
    INNER JOIN CampaignStatus ON Campaign.campaign_status_id = CampaignStatus.id 
    LEFT OUTER JOIN Publisher ON Affiliate.affid = Publisher.affid 
GROUP BY 
    COALESCE(Publisher.name, Affiliate.name) 
    ,ItemAccountingStatus.name 
    ,CampaignStatus.name  
HAVING (
    ItemAccountingStatus.name='default' 
    and CampaignStatus.name='Verified' 
    and Publisher='AdsMain LLC' -- THIS BREAKS THE QUERY 
) 

問:有沒有辦法做到這一點?

+0

回答我自己的問題 - 身價離開這個向上或我應該刪除嗎? – 2011-05-05 04:32:08

+0

我會放棄它 - 如果有人遇到同樣的問題,他們可能會很高興找到答案! – 2011-05-05 04:52:17

+0

@marc_s:答案已添加。這是「爲什麼我不能在WHERE子句中訪問我的列別名」的變體「 – gbn 2011-05-05 04:59:04

回答

0
SELECT * FROM 
(
    SELECT 
      COALESCE(Publisher.name, Affiliate.name) AS Publisher 
     ,dbo.SumKeys(Item.id) AS ItemIDs 
    FROM 
     Item 
     INNER JOIN ItemAccountingStatus ON Item.item_accounting_status_id = ItemAccountingStatus.id 
     INNER JOIN Affiliate ON Item.affid = Affiliate.affid 
     INNER JOIN Currency AffiliateCurrency ON Affiliate.currency_id = AffiliateCurrency.id 
     INNER JOIN Campaign ON Item.pid = Campaign.pid 
     INNER JOIN CampaignStatus ON Campaign.campaign_status_id = CampaignStatus.id 
     LEFT OUTER JOIN Publisher ON Affiliate.affid = Publisher.affid 
    GROUP BY 
     COALESCE(Publisher.name, Affiliate.name) 
     ,ItemAccountingStatus.name 
     ,CampaignStatus.name  
    HAVING (
     ItemAccountingStatus.name='default' 
     and CampaignStatus.name='Verified' 
    ) 
) A 
WHERE A.Publisher='AdsMain LLC' 
+0

」或者您可以簡單地將'和COALESCE(Publisher.name,Affiliate.name)='AdsMain LLC''添加到Having子句中,而不是使用派生表。 – Thomas 2011-05-05 06:07:42

+0

更好的辦法是把它放在Where子句中,就像josephj1989 – Thomas 2011-05-05 06:09:23

1
... 
and COALESCE(Publisher.name, Affiliate.name) ='AdsMain LLC' 
... 

列別名只允許在ORDER BY。您必須在WHERE,GROUP BY,HAVING等中使用表達式(用於生成別名列),但您已經按照預期在GROUP BY中使用了表達式。

某些DBMS允許您在其他地方引用列別名,但不引用SQL Server,我認爲它更明顯地發生了什麼。

或者你可以使用派生表/ CTE方法,並從選擇(根據你的自我的答案)

SELECT on MSDN

「SELECT語句的邏輯處理順序」。 ..相反,因爲SELECT子句是第8步,所以在該子句中定義的任何列別名或派生列都不能被前面的子句引用。但是,它們可以被後續子句(如ORDER BY子句)引用。請注意,該語句的實際物理執行由查詢處理器決定,並且訂單可能與此列表不同。

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY WITH CUBE或WITH ROLLUP
  6. HAVING
  7. (GBN:別名不可用)
  8. SELECT(GBN:此處生成的別名)
  9. DISTINCT
  10. ORDER BY
  11. TOP
3

如果你需要一個having子句我對此表示懷疑 - 嘗試在where子句中,如下

SELECT 
     COALESCE(Publisher.name, Affiliate.name) AS Publisher 
    ,dbo.SumKeys(Item.id) AS ItemIDs 
FROM 
    Item 
    INNER JOIN ItemAccountingStatus ON Item.item_accounting_status_id = ItemAccountingStatus.id 
    INNER JOIN Affiliate ON Item.affid = Affiliate.affid 
    INNER JOIN Currency AffiliateCurrency ON Affiliate.currency_id = AffiliateCurrency.id 
    INNER JOIN Campaign ON Item.pid = Campaign.pid 
    INNER JOIN CampaignStatus ON Campaign.campaign_status_id = CampaignStatus.id 
    LEFT OUTER JOIN Publisher ON Affiliate.affid = Publisher.affid 
where 
    ItemAccountingStatus.name='default' 
    and CampaignStatus.name='Verified' 
    and Publisher='AdsMain LLC' 
GROUP BY 
    COALESCE(Publisher.name, Affiliate.name) 
    ,ItemAccountingStatus.name 
    ,CampaignStatus.name  
+0

提示的快速跟進 - 我實際上得到'無效的列名'Publisher''。 – 2011-05-10 20:04:06