SQL Server - group by 中的用例语句

发布于 2024-11-27 20:51:02 字数 515 浏览 1 评论 0原文

我试图创建一个视图,如下所示:

CREATE VIEW vw1 AS SELECT     
  Town
, case when (Spend > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       else null
       end as AvgSpend
, case when (Margin > 0 and NbOrders > 0)
       then sum(Margin)/sum(NbOrders)
       else null
       end as AvgMargin
FROM Table
group by Town

但是我收到错误,因为“case when...”语句中使用的字段不包含在聚合函数中。

我无法使用“where”子句,因为平均字段取决于大于 0 的不同字段,并且我不知道在聚合字段上工作时如何证明这一点?

我有什么想法可以实现这一点 - 最好是在一个声明中?

I am trying to create a view as follows:

CREATE VIEW vw1 AS SELECT     
  Town
, case when (Spend > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       else null
       end as AvgSpend
, case when (Margin > 0 and NbOrders > 0)
       then sum(Margin)/sum(NbOrders)
       else null
       end as AvgMargin
FROM Table
group by Town

But I get an error because the fields used in my 'case when...' statement aren't included in an aggregate function.

I can't use a 'where' clause because the average fields depend on different fields being >0 and I can't see how I could make the case when work on an aggregated field?

Any ideas how I can achieve this - preferably in a single statement?

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

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

发布评论

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

评论(4

老子叫无熙 2024-12-04 20:51:02
CREATE VIEW vw1 AS
SELECT
  Town
, SUM(CASE WHEN Spend > 0 THEN Spend END)
  / SUM(CASE WHEN NbOrders > 0 THEN NbOrders END) AS AvgSpend
, SUM(CASE WHEN Margin > 0 THEN Margin END)
  / SUM(CASE WHEN NbOrders > 0 THEN NbOrders END) AS AvgMargin
FROM Table
GROUP BY Town
CREATE VIEW vw1 AS
SELECT
  Town
, SUM(CASE WHEN Spend > 0 THEN Spend END)
  / SUM(CASE WHEN NbOrders > 0 THEN NbOrders END) AS AvgSpend
, SUM(CASE WHEN Margin > 0 THEN Margin END)
  / SUM(CASE WHEN NbOrders > 0 THEN NbOrders END) AS AvgMargin
FROM Table
GROUP BY Town
脱离于你 2024-12-04 20:51:02

更新了...感谢@Andriy M

SELECT town

,SUM(CASE WHEN Spend > 0 and NbOrders > 0 THEN Spend END)
/SUM(CASE WHEN Spend > 0 and NbOrders > 0 THEN NbOrders END)  As AvgSpend

,SUM(CASE WHEN Margin > 0 and NbOrders > 0 THEN Margin END)
/ SUM(CASE WHEN Margin > 0 and NbOrders > 0 THEN NbOrders END) As AvgMargin

FROM table
GROUP BY town

Updated... thanks to @Andriy M

SELECT town

,SUM(CASE WHEN Spend > 0 and NbOrders > 0 THEN Spend END)
/SUM(CASE WHEN Spend > 0 and NbOrders > 0 THEN NbOrders END)  As AvgSpend

,SUM(CASE WHEN Margin > 0 and NbOrders > 0 THEN Margin END)
/ SUM(CASE WHEN Margin > 0 and NbOrders > 0 THEN NbOrders END) As AvgMargin

FROM table
GROUP BY town
日裸衫吸 2024-12-04 20:51:02

您只需要在每个“案例”中包含 group by 即可。

像下面这样:

CREATE VIEW vw1 AS SELECT     
  Town
, case when (Spend > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       group by Town
       else null
       end as AvgSpend
, case when (Margin > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       group by Town
       else null
       end as AvgMargin
FROM Table

you just need to include the group by in each "case".

something like the below:

CREATE VIEW vw1 AS SELECT     
  Town
, case when (Spend > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       group by Town
       else null
       end as AvgSpend
, case when (Margin > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       group by Town
       else null
       end as AvgMargin
FROM Table
清欢 2024-12-04 20:51:02
CREATE VIEW vw1 AS SELECT     
  Town
, case when (Spend > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       else null
       end as AvgSpend
, case when (Margin > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       else null
       end as AvgMargin
FROM Table
group by Town, AvgSpend, AvgMargin 
CREATE VIEW vw1 AS SELECT     
  Town
, case when (Spend > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       else null
       end as AvgSpend
, case when (Margin > 0 and NbOrders > 0)
       then sum(Spend)/sum(NbOrders)
       else null
       end as AvgMargin
FROM Table
group by Town, AvgSpend, AvgMargin 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文