在一个表上组合两个SQL查询
发布时间:2021-01-08 01:48:13 所属栏目:MsSql教程 来源:网络整理
导读:我有一个具有不同值的tableA: data------ 10 15 20 40 40000 50000 60000 此外,我需要获取有关该数据的一些统计信息(我想在一个查询中执行此操作),例如: select count(data) from tableA where data 100union allselect count(data) from tableA where d
我有一个具有不同值的tableA: data ------ 10 15 20 40 40000 50000 60000 此外,我需要获取有关该数据的一些统计信息(我想在一个查询中执行此操作),例如: select count(data) from tableA where data < 100 union all select count(data) from tableA where data >= 100 结果,我收到了 (No column name) ---------------- 4 3 但我希望在一行中收到结果,如下所示: Small | Big --------- 4 | 3 怎么做?可能吗? 解决方法select count(case when data < 100 then 1 end) as Small,count(case when data >= 100 then 1 end) as Big from TableA 平均值看起来像这样. select avg(case when data < 100 then data end) as Small,avg(case when data >= 100 then data end) as Big from TableA (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |