I have a tableA
with different values:
我有一个具有不同值的tableA:
data
------
10
15
20
40
40000
50000
60000
Also, I need to get some statistic information on that data (and I want to do it in one query), for example:
此外,我需要获取有关该数据的一些统计信息(我想在一个查询中执行此操作),例如:
select count(data) from tableA where data < 100
union all
select count(data) from tableA where data >= 100
As result, I receive
结果,我收到了
(No column name)
----------------
4
3
But I want to receive results in one row, like this:
但我希望在一行中收到结果,如下所示:
Small | Big
---------
4 | 3
How to do it? Is it possible?
怎么做?可能吗?
4 个解决方案
#1
10
select count(case when data < 100 then 1 end) as Small,
count(case when data >= 100 then 1 end) as Big
from TableA
With average it would look like this.
平均值看起来像这样。
select avg(case when data < 100 then data end) as Small,
avg(case when data >= 100 then data end) as Big
from TableA
#2
10
Try sub-query instead of UNION ALL
like this:
尝试子查询而不是UNION ALL,如下所示:
SELECT
(SELECT COUNT(data) FROM tableA WHERE data < 100) AS Small,
(SELECT COUNT(data) FROM tableA WHERE data >= 100) AS Big
看到这个SQLFiddle
#3
6
DECLARE @tst TABLE (
val INT
)
INSERT INTO @tst (val)
SELECT 10
UNION
SELEcT 15
UNION
SELECT 20
UNION
SELECT 40
UNION
SELECT 40000
UNION
SELECT 50000
UNION
SELECT 60000
;WITH Smalls AS (
SELECT COUNT(val) Small FROM @tst WHERE val < 100
), Bigs AS(
select count(val) Big from @tst where val >= 100
)
SELECT Small, Big
FROM Smalls, Bigs
#4
0
create table datatable
(
data int
)
insert into datatable values(10)
insert into datatable values(15)
insert into datatable values(20)
insert into datatable values(40)
insert into datatable values(40000)
insert into datatable values(50000)
insert into datatable values(60000)
create table outputtable
(
small int ,
big int
)
insert into outputtable
(
small,
big
)
select (select count(data) from datatable where data<100),
(select count(data) from datatable where data>=100)
select * from datatable
select * from outputtable
#1
10
select count(case when data < 100 then 1 end) as Small,
count(case when data >= 100 then 1 end) as Big
from TableA
With average it would look like this.
平均值看起来像这样。
select avg(case when data < 100 then data end) as Small,
avg(case when data >= 100 then data end) as Big
from TableA
#2
10
Try sub-query instead of UNION ALL
like this:
尝试子查询而不是UNION ALL,如下所示:
SELECT
(SELECT COUNT(data) FROM tableA WHERE data < 100) AS Small,
(SELECT COUNT(data) FROM tableA WHERE data >= 100) AS Big
看到这个SQLFiddle
#3
6
DECLARE @tst TABLE (
val INT
)
INSERT INTO @tst (val)
SELECT 10
UNION
SELEcT 15
UNION
SELECT 20
UNION
SELECT 40
UNION
SELECT 40000
UNION
SELECT 50000
UNION
SELECT 60000
;WITH Smalls AS (
SELECT COUNT(val) Small FROM @tst WHERE val < 100
), Bigs AS(
select count(val) Big from @tst where val >= 100
)
SELECT Small, Big
FROM Smalls, Bigs
#4
0
create table datatable
(
data int
)
insert into datatable values(10)
insert into datatable values(15)
insert into datatable values(20)
insert into datatable values(40)
insert into datatable values(40000)
insert into datatable values(50000)
insert into datatable values(60000)
create table outputtable
(
small int ,
big int
)
insert into outputtable
(
small,
big
)
select (select count(data) from datatable where data<100),
(select count(data) from datatable where data>=100)
select * from datatable
select * from outputtable