2

Here's my Query that does not work because it apparently violates the rules of the HAVING clause:

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
)

QUESTION: is there any way to do this?

4

3 回答 3

3

我怀疑你是否需要一个有子句 - 在下面的 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    
于 2011-05-05T05:56:00.080 回答
1
...
and COALESCE(Publisher.name, Affiliate.name) ='AdsMain LLC'
...

列别名只允许在 ORDER BY 中。您必须在 WHERE、GROUP BY、HAVING 等中使用表达式(生成别名列)。尽管如预期的那样,您已经在 GROUP BY 中使用了表达式。

一些 DBMS 允许您在其他地方引用列别名,但不允许在 SQL Server 中引用,我认为这让发生的事情更加明显。

或者您可以使用派生表/CTE 方法并从中选择(根据您的自我回答)

根据MSDN上的 SELECT ,“SELECT 语句的逻辑处理顺序”

...相反,因为 SELECT 子句是第 8 步,所以在该子句中定义的任何列别名或派生列都不能被前面的子句引用。但是,它们可以被后续子句引用,例如 ORDER BY 子句。请注意,语句的实际物理执行由查询处理器确定,并且顺序可能与此列表不同。

  1. 加入
  2. 在哪里
  3. 通过...分组
  4. WITH CUBE 或 WITH ROLLUP
  5. HAVING(gbn:别名不可用)
  6. SELECT(gbn:此处生成的别名)
  7. 清楚的
  8. 订购方式
  9. 最佳
于 2011-05-05T04:55:58.710 回答
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'
于 2011-05-05T04:31:23.067 回答