oracle 查询以获取总行数和非重复行之间的差值

ctzwtxfj  于 2023-06-05  发布在  Oracle
关注(0)|答案(4)|浏览(213)

我是SQL新手,无法编写正确的SQL。给定一个表STATION,它包含五个字段的数据,即ID、CITY、STATE、北方LATITUDE和WESTERN LONGITUDE。

+-------------+------------+
| Field       |   Type     |
+-------------+------------+
| ID          | INTEGER    |
| CITY        | VARCHAR(21)|
| STATE       | VARCHAR(2) |
| LAT_N       | NUMERIC    |
| LONG_W      | NUMERIC    |
+-------------+------------+

NUM是城市的数量,NUMunique是唯一城市的数量,然后编写一个查询来打印NUM-NUMunique的值。
我试过:

select (count(CITY)- distinct count(CITY)) from STATION;
gopyfrb3

gopyfrb31#

您可以在count中使用select distinct并尝试这种方式

select  (count(CITY)- count(distinct CITY)) from STATION;
ojsjcaue

ojsjcaue2#

SELECT COUNT(*) - COUNT(DISTINCT CITY) FROM STATION
ars1skjm

ars1skjm3#

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

select (count(city)-count(distinct city)) from station;

查询二:

select ((select count(city) as ans1 from station)-(select count(distinct city)
       as ans2 from station));

查询三:

select (( select count(city) from station )-( select count(distinct city) from station ))
       as ans;

上面提到的所有查询都可以工作。

kr98yfug

kr98yfug4#

SQL服务器:-

SELECT ABS((SELECT COUNT(CITY) FROM STATION)-(SELECT COUNT(DISTINCT CITY) FROM STATION)) AS diff_num

相关问题