我可以使用别名'例如语句
SELECT
id
, quantity
, unit_price
, (antity * unit_price) as price
CASE
WHEN quantity * unit_price > 5000 THEN (quantity * unit_price) * 0.2
WHEN quantity * unit_price > 3000 THEN (quantity * unit_price) * 0.15
ELSE null
END
AS discount
FROM OrderDetails;
我尝试在case
中使用Alias Price
,但它不起作用。 在上面的代码中,我重复了数量 * unit_price
5次。 是否有更好的方法来实现此代码? 有没有方法可以避免在案例语句中重复?
SELECT
id
, quantity
, unit_price
, (antity * unit_price) as price
CASE
WHEN quantity * unit_price > 5000 THEN (quantity * unit_price) * 0.2
WHEN quantity * unit_price > 3000 THEN (quantity * unit_price) * 0.15
ELSE null
END
AS discount
FROM OrderDetails;
I tried to use the alias price
in CASE
but it doesn't work.
In the above code, I repeated quantity * unit_price
5 times.
Do exist any better way to realize this code?
Is there way to avoid repetition in the CASE statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用CTE
You could use a CTE