加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在一个表上组合两个SQL查询

发布时间:2021-01-08 03:06:33 所属栏目:MsSql教程 来源:网络整理
导读:我有一个具有不同值的tableA: data------ 10 15 20 40 40000 50000 60000 此外,我需要获取有关该数据的一些统计信息(我想在一个查询中执行此操作),例如: select count(data) from tableA where data 100union allselect count(data) from tableA where data

我有一个具有不同值的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

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读