查询以获取总行数和不同行之间的差异

pra*_*y G 11 sql database oracle

我是SQL的新手,无法编写正确的SQL.给定一个表STATION,其中包含五个字段的数据,即ID,CITY,STATE,NORTHERN LATITUDE和WESTERN LONGITUDE.

+-------------+------------+
| Field       |   Type     |
+-------------+------------+
| ID          | INTEGER    |
| CITY        | VARCHAR(21)|
| STATE       | VARCHAR(2) |
| LAT_N       | NUMERIC    |
| LONG_W      | NUMERIC    |
+-------------+------------+
Run Code Online (Sandbox Code Playgroud)

NUM是城市的数量,NUMunique是唯一的城市数量,然后编写一个查询打印的价值NUM- NUMunique.

我试过了:

select (count(CITY)- distinct count(CITY)) from STATION; 
Run Code Online (Sandbox Code Playgroud)

sca*_*dge 35

您可以在计数内使用select distinct并尝试这种方式

select  (count(CITY)- count(distinct CITY)) from STATION; 
Run Code Online (Sandbox Code Playgroud)


小智 7

select (count(CITY)- count(distinct CITY)) from STATION;
Run Code Online (Sandbox Code Playgroud)

脚注:您可以查看以下 sql 函数计算数学值:count、avg、sum、+、-、*、%


小智 6

SELECT COUNT(*) - COUNT(DISTINCT CITY) FROM STATION
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以编写许多查询来回答这个问题,例如:当我考虑这个问题时,我们有名为 STATION 的表,我们现在必须找到表中 CITY 条目总数与表中不同 CITY 条目数之间的差异桌子

查询1:

select (count(city)-count(distinct city)) from station;
Run Code Online (Sandbox Code Playgroud)

查询2:

select ((select count(city) as ans1 from station)-(select count(distinct city)
       as ans2 from station));
Run Code Online (Sandbox Code Playgroud)

查询3:

select (( select count(city) from station )-( select count(distinct city) from station ))
       as ans;
Run Code Online (Sandbox Code Playgroud)

所有上述查询都将起作用。