Sql 使用count查找带有where子句的值的出现次数
我有这张桌子(这是一个简短的版本) 如何使用SQL确定表中使用“unix”运行的“桌面”计算机的数量Sql 使用count查找带有where子句的值的出现次数,sql,oracle,count,where,Sql,Oracle,Count,Where,我有这张桌子(这是一个简短的版本) 如何使用SQL确定表中使用“unix”运行的“桌面”计算机的数量 Select count(1) from table where computer_type = 'DESKTOP' and operating_system = 'UNIX' 这假设计算机类型和操作系统列的类型为VARCHAR2(如果使用描述计算机;命令,可以看到该类型)。如果它们有typeCHAR,则它们将用空格字符右键填充,您可以使用: SELECT computer_type,
Select count(1)
from table
where computer_type = 'DESKTOP'
and operating_system = 'UNIX'
这假设计算机类型
和操作系统
列的类型为VARCHAR2
(如果使用描述计算机;
命令,可以看到该类型)。如果它们有typeCHAR
,则它们将用空格字符右键填充,您可以使用:
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE RTRIM( computer_type ) = 'DESKTOP'
AND RTRIM( operating_system ) = 'UNIX'
GROUP BY
computer_type,
operating_system;
或者,您可以将字符串文字右键填充到适当的长度:
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE computer_type = 'DESKTOP '
AND operating_system = 'UNIX '
GROUP BY
computer_type,
operating_system;
没有变得更简单,请显示您尝试了什么?从computer_type='DESKTOP'和operating_system='UNIX'组BYcomputer_type,operating_system的计算机中选择computer_type,operating_system,count(*);这是我认为最接近的,但我只得到了表中没有数据的列名,该查询看起来应该可以工作()。你确定你有这些数据吗?或者你期望的结果是什么?我100%拥有这些数据,因为如果我从计算机上运行SELECT*,我会得到表格。但是,当我运行我之前发布的查询时,我只得到了“computer\u type”“operating\u system”和“count(*)这两个列的名称,没有任何数据,正如我所期望的那样,其中有一个条目包含运行Unix的台式机数量
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE RTRIM( computer_type ) = 'DESKTOP'
AND RTRIM( operating_system ) = 'UNIX'
GROUP BY
computer_type,
operating_system;
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE computer_type = 'DESKTOP '
AND operating_system = 'UNIX '
GROUP BY
computer_type,
operating_system;