在 SQL 查询中使用计算字段

发布于 2024-10-25 12:25:26 字数 144 浏览 2 评论 0原文

我有一个 sql 查询,其中有一个计算字段,用于计算边际贡献。我让它显示出来并且数学工作正常。我遇到的问题是我只想显示贡献边际低于 0.25 的记录。我知道你不能在 where 子句中使用列别名。我想知道执行此操作的最佳方法是什么。我也为此使用 Visual Studio。

I have a sql query in which i have a calculated field which calculates the Contribution Margin. I get it to display and the math works fine. The problem i'm having is that i want to only display the records in which the Contribution Margin is lower than 0.25. I know you cant use column alias in the where clause. I was wondering what the best way to go about doing this would be. I'm also using Visual Studio for this.

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

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

发布评论

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

评论(4

混吃等死 2024-11-01 12:25:26
SELECT  *
FROM    (
        SELECT  m.*,
                compute_margin(field1, field2) AS margin
        FROM    mytable m
        ) q
WHERE   margin < 0.25
SELECT  *
FROM    (
        SELECT  m.*,
                compute_margin(field1, field2) AS margin
        FROM    mytable m
        ) q
WHERE   margin < 0.25
请远离我 2024-11-01 12:25:26

您不能使用列别名(除非您使用原始查询作为子查询),但您可以使用用于定义计算值的表达式。

例如,如果您现在的查询是这样的:

select
    contribution_amount,
    total_amount,
    contribution_amount / total_amount as contribution_margin

from records

您可以这样做:

select
    contribution_amount,
    total_amount,
    contribution_amount / total_amount as contribution_margin

from records

where contribution_amount / total_amount < 0.25

或者这样:(

select * from
(
    select
        contribution_amount,
        total_amount,
        contribution_amount / total_amount as contribution_margin

    from records
)
where contribution_margin < 0.25

我个人认为第一个版本更好,但两者可能执行相同的操作)

You can't use the column alias (unless you use your original query as a subquery), but you can use the expression that you're using to define the calculated value.

For example, if your query is this now:

select
    contribution_amount,
    total_amount,
    contribution_amount / total_amount as contribution_margin

from records

You could do this:

select
    contribution_amount,
    total_amount,
    contribution_amount / total_amount as contribution_margin

from records

where contribution_amount / total_amount < 0.25

Or this:

select * from
(
    select
        contribution_amount,
        total_amount,
        contribution_amount / total_amount as contribution_margin

    from records
)
where contribution_margin < 0.25

(Personally I find the first version to be preferable, but both will likely perform the same)

口干舌燥 2024-11-01 12:25:26

您可以

  • 在 where 子句中重复计算,
  • 将查询包装在表表达式(CTE 或派生表)中,并在 where 子句中使用别名,
  • 在交叉应用中分配别名。

举一个最后一种方法的例子

select doubled_schema_id,*
from sys.objects
cross apply (select schema_id*2 as doubled_schema_id) c
where doubled_schema_id= 2

You can either

  • repeat the calculation in the where clause
  • wrap the query in a table expression (CTE or derived table) and use the alias in the where clause
  • assign the alias in a cross apply.

To give an example of the last approach

select doubled_schema_id,*
from sys.objects
cross apply (select schema_id*2 as doubled_schema_id) c
where doubled_schema_id= 2
深者入戏 2024-11-01 12:25:26

有两种方法,Quassnoi 发布的解决方案(您也可以使用类似的 CTE)

或 WHEREcompute_margin(field1, field2) WHEREcompute_margin(field1, field2) 0.25

two ways, either the solution that Quassnoi posted(you can also use a CTE which is similar)

or WHERE compute_margin(field1, field2) < 0.25

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文