我想要一个按照规则的 SQL 计算列
我希望能够在一列中存储十进制值,并在另一列中存储表示选项的 int (将解释):
- 应该是基数 -%
- 应该是基数 -absolute
- 应该是基数 +%
- 应该是基数+绝对
- 1& 2是折扣
- 3& 4是附加费
- 1& 3 按百分比减少/增加金额(即金额*金额/值)。
- 2& 4 减少/增加绝对金额(即金额+/-值)。
意思是我有一个包含 3 列的表:
- BasePrice MoneyAdditionalPricedecimalOptiontinyint
- 和
- 根据选项,
ComputedColumn
- (十进制?)
假设我们有一行 BasePrice 为 100,AdditionalPrice 为 0.20
计算的 col 应生成以下值:
- 80
- 99.80
- 120
- 100.20
我说清楚了吗? 我有什么办法可以实现这个目标吗?
I want to be able to store a decimal value in one column, and in the other column to store an int that represents the option (will explain):
- should be base -%
- should be base -absolute
- should be base +%
- should be base +absolute
- 1 & 2 is a discount
- 3 & 4 is an upcharge
- 1 & 3 reduces/raises the amount by percentage (i.e. amount * amount/value).
- 2 & 4 reduces/raises the amount absolutely (i.e. amount +/- value).
Meaning I have a table of 3 columns:
- BasePrice money
- AdditionalPrice decimal
- Option tinyint
and
- ComputedColumn (decimal?)
let's say we have a row that it's BasePrice is 100 and the AdditionalPrice is 0.20
According to the option the computed col should generate the following value:
- 80
- 99.80
- 120
- 100.20
Am I clear?
Is there any way I can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也会将计算列保留为金钱。
在内部,由于数据类型优先级,这将转换为十进制
I'd keep the computed column as money too.
Internally, this will cast to decimal because of Data Type Precedence