如何根据合并列的值过滤分组查询?

发布于 2024-11-05 04:34:02 字数 915 浏览 0 评论 0原文

这是我的查询不起作用,因为它显然违反了 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
)

问题:有什么方法可以做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-11-12 04:34:02

我怀疑您是否需要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    

I doubt if you need a having clause - try it in the where clause as below

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    
网白 2024-11-12 04:34:02
...
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. 哪里
  5. 分组依据
  6. 使用多维数据集或使用 ROLLUP
  7. HAVING(gbn:别名不可用)
  8. SELECT(gbn:此处生成的别名)
  9. 独特
  10. 排序依据
  11. 顶部
...
and COALESCE(Publisher.name, Affiliate.name) ='AdsMain LLC'
...

Column aliases are allowed only in the ORDER BY. You must use the expression (that generates the aliased column) in WHERE, GROUP BY, HAVING etc. You've already used the expression in the GROUP BY though as expected.

Some DBMS allow you to reference column aliases in other places but not SQL Server, which I think makes it more obvious what is going on.

Or you can use a derived table/CTE approach and SELECT from that (as per your self-answer)

As per SELECT on MSDN, "Logical Processing Order of the SELECT statement"

...Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. WITH CUBE or WITH ROLLUP
  7. HAVING (gbn: alias not available)
  8. SELECT (gbn: aliases generated here)
  9. DISTINCT
  10. ORDER BY
  11. TOP
著墨染雨君画夕 2024-11-12 04:34:02
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'
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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文