SQL 视图中的表达式
我是 SQL 视图的新手,所以要温柔!
我有以下 SQL 视图:
SELECT dbo.product.name AS [Product Name],
ROUND(CASE [vat] WHEN 1 THEN [packcost] * 1.2 ELSE [packcost] END, 2) AS [Pack Cost],
ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Cost],
dbo.purchase.unitsaleprice * dbo.product.units AS [ Pack Sale Price], dbo.purchase.unitsaleprice AS [Unit Sale Price],
dbo.product.units * (dbo.purchase.unitsaleprice - ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2)) AS [Pack Profit],
dbo.purchase.unitsaleprice - ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Profit]
FROM dbo.product INNER JOIN
dbo.purchase ON dbo.product.id = dbo.purchase.productID
但它似乎效率低下,因为我正在重写很多内容。
例如,我想定义 [Pack Cost] 列:
ROUND(CASE [vat] WHEN 1 THEN [packcost] * 1.2 ELSE [packcost] END, 2) AS [Pack Cost]
在其他地方使用而不是重写它。
例如,这样我可以使用:
[Pack Cost] / [Units]
定义单位成本,而不是:
ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Cost]
不确定我是否得到了正确的结果,或者是否适合这样做。
I'm new to SQL Views so be gentle!
I have the following SQL view:
SELECT dbo.product.name AS [Product Name],
ROUND(CASE [vat] WHEN 1 THEN [packcost] * 1.2 ELSE [packcost] END, 2) AS [Pack Cost],
ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Cost],
dbo.purchase.unitsaleprice * dbo.product.units AS [ Pack Sale Price], dbo.purchase.unitsaleprice AS [Unit Sale Price],
dbo.product.units * (dbo.purchase.unitsaleprice - ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2)) AS [Pack Profit],
dbo.purchase.unitsaleprice - ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Profit]
FROM dbo.product INNER JOIN
dbo.purchase ON dbo.product.id = dbo.purchase.productID
But it seems inefficient as I am rewriting a lot of this.
For example I would like to define the [Pack Cost] column:
ROUND(CASE [vat] WHEN 1 THEN [packcost] * 1.2 ELSE [packcost] END, 2) AS [Pack Cost]
to be used elswhere instead of rewriting it.
e.g so I could use:
[Pack Cost] / [Units]
To define unit cost instead of:
ROUND(CASE [vat] WHEN 1 THEN ([packcost] * 1.2) / [units] ELSE [packcost] / [units] END, 2) AS [Unit Cost]
Not sure if I'm getting the right end of the stick though, or whether it is appropriate to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
创建一个新的用户定义标量值函数,如下所示:
在查询中,您将选择如下:
Create a new User-Defined Scalar-valued Function as follows:
In your query, you would select is as follows:
您可以创建一个标量函数来为您执行该逻辑。然后你就可以调用你视图中的函数了。
You could create a scalar function that did that logic for you. Then you can just call the function in your view.
使用计算逻辑创建中间视图。称之为(例如)ProductEx。该视图可以为您计算并命名 PackCost 列。然后根据视图 ProductEx 而不是表 Product 编写所有其他视图。
Create an intermediate view with the calculation logic. Call it (for example) ProductEx. This view can have the PackCost column calculated and named for you. Then write all your other views against the view ProductEx instead of the table Product.
您可以在子查询中定义它
You can define it in a subquery
我的经验是,与执行简单计算的成本相比,获取行的“成本”是如此之大,以至于我从不关心它。
我想说可读性和/或抽象性更值得关注。
另外,您的 dbms 可能已经在幕后执行了这些优化,但您应该测量这两种方式以确保确定。
My experience is that the "cost" of aquiring the rows is so large compared to the cost of performing simple calculations that I just never care about it.
I'd say that readability and/or abstraction is of greater concern.
Plus, it is likely that your dbms already perform those optimizations under the hood, but you should measure both ways to be sure.
或者,如果您要经常进行计算,您可以将一个计算字段添加到名为 PackCost 的原始表中。那么 caclis 仅在数据输入时完成,或者不会随每个查询而更改。
Alternatively, if you wil be doing the calculation often, you could add a calulated field to the orginal table that is called PackCost. then caclis only done on data entry or change not with every query.