我有一个名为“压力”的列,它的文本值(高,低,正常)在表中针对不同的个人重复了几次。现在,我想分别对高,低和正常值进行计数,并在三列中分别显示,如下所示。
pressure high low Normal
?? ?? ??
请建议...
致谢
答案 0 :(得分:0)
假设您有这样的查询:
SELECT
SampleDateTime, -- datetimeoffset(7) NOT NULL
Celsius -- float NOT NULL
FROM
Temperatures
ORDER BY
SampleDateTime
然后您可以执行以下操作:
SELECT
MAX( Celsius ) AS [High],
MIN( Celsius ) AS [Low],
AVG( Celsius ) AS [Mean],
PERCENTILE_CONT( 0.5 ) WITHIN GROUP ( ORDER BY Celsius ) OVER () AS [Median]
FROM
Temperatures
如果要使用模态值,则要涉及更多。由于这可能是连续数据,因此您需要ROUND
以获得离散值
SELECT
TOP 1
ROUND( Celsius, 0 ) AS [Mode]
FROM
Temperatures
GROUP BY
Celsius
ORDER BY
COUNT(*) DESC
...可以使用CROSS JOIN
进行连接,因为每个子查询仅返回单行,所以可以:
SELECT
[High],
[Low],
[Mean],
[Median],
[Mode]
FROM
(
SELECT
MAX( Celsius ) AS [High],
MIN( Celsius ) AS [Low],
AVG( Celsius ) AS [Mean],
PERCENTILE_CONT( 0.5 ) WITHIN GROUP ( ORDER BY Celsius ) OVER () AS [Median]
FROM
Temperatures
) AS t1
CROSS JOIN
(
SELECT
TOP 1
ROUND( Celsius, 0 ) AS [Mode]
FROM
Temperatures
GROUP BY
Celsius
ORDER BY
COUNT(*) DESC
) AS t2
答案 1 :(得分:0)
具有这样的表模式(包括示例数据):
create table pressure(name varchar(10), pressure varchar(10));
insert into pressure(name, pressure) values("john", "high");
insert into pressure(name, pressure) values("john", "low");
insert into pressure(name, pressure) values("john", "normal");
insert into pressure(name, pressure) values("jacob", "high");
insert into pressure(name, pressure) values("smith", "normal");
一种方法是查询如下内容:
select 'pressure' as data, (select count(*) from pressure where pressure = 'high') as high, (select count(*) low from pressure where pressure = 'low') as low, (select count(*) normal from pressure where pressure = 'normal') as normal from dual;
这应该导致如下结果:
| data | high | low | normal |
| pressure | 2 | 1 | 2 |
免责声明:这可能不适用于大量数据
答案 2 :(得分:0)
我认为您需要遵循这些原则
select 'pressure' as data,
sum(case when pressure='high' then 1 else 0 end) as high,
sum(case when pressure='low' then 1 else 0 end) as low,
sum(case when pressure='normal' then 1 else 0 end) as normal
from your_table