T-SQL 按计算列排序 [英] T-SQL sorting by a calculated column

查看:41
本文介绍了T-SQL 按计算列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下列的表格:ID、Price、IsMonthlyPrice

I have a table with these columns: ID, Price, IsMonthlyPrice

我如何选择全部并按每周价格排序,同时考虑到:

How can I select all and sort by weekly price, taking into account that:

if (IsMonthlyPrice = false) then Price is weekly

我需要按每周价格排序

每月价格的每周价格为:价格*12/52

weekly price of a monthly price would be: Price*12/52

推荐答案

您不需要包含两次计算.您可以按列号订购

You don't need to include the calculation twice. You can order on the column number

SELECT 
    ID, 
    Price, 
    IsMonthlyPrice, 
    CASE IsMonthlyPrice
    WHEN 1 THEN Price * 12 / 52
    ELSE price
    END
FROM [TABLE]
    order by 
        4

这篇关于T-SQL 按计算列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆