sql - 如何按不同表中三列的总和排序?
问题描述
select username,
(select COUNT(OpenUser) from TIcket t with(nolock) where t.OpenUser = u.UserName and cast(t.CompletedDate as date) = cast(getdate() as date) and t.IssueClass = 58) as ServiceNote,
(select COUNT(AssignUser) from TIcket t with(nolock) where ((t.AssignUser = u.UserName and t.Status = 1) and t.IssueClass != 58)) as status1,
(select COUNT(PickUpUser) from TIcket t with(nolock) where ((t.PickUpUser = u.UserName and t.Status = 2) and t.IssueClass != 58)) as status2
from users u
where isinactive = 0
and UserLevel > -1 and First_Name != ''
and center_id = '100'
我查询了我的程序。我想按三个字段 ServiceNote、status1、status2 的总和进行排序。我尝试使用“Sum”进行订购,(例如按 (ServiceNote,status1,status2) 订购,但它不起作用。
解决方案
这不行吗?
order by (serviceNote + status1 + status2)
这适用于大多数数据库,但不适用于 SQL Server。为此,请使用 CTE 或子查询:
with cte as (
<your query here>
)
select cte.*
from cte
order by (serviceNote + status1 + status2);
推荐阅读
- wordpress - 从新订单 woocommerce 中删除“回复:地址”
- vue.js - 如何在带有 Vuetify 的 Combobox 组件中使用掩码?
- django - 从 ExtractQuarter 中获取价值
- vba - 如果在源 WB 中未找到,则删除目标 WB 中的 VB 组件
- android - 双向数据绑定错误:无法解析布尔属性的设置器
- python - Tensorflow:tf.estimator 不适用于大型 CSV 训练文件(~7GB)
- python - 了解 gensim.similarities.index 中 AnnoyIndexer 的 most_similar 方法
- arrays - Swift 4 Array 获取参考
- google-api - 是否可以自定义使用 Google Calendar API 发送的日历邀请通知?
- amazon-web-services - Aws Batch 未登录到 Cloudwatch