我是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 |
+-------------+------------+
让NUM
为城市数量,NUMunique
为唯一城市数量,然后撰写查询以打印NUM
- NUMunique
的值。
我试过了:
select (count(CITY)- distinct count(CITY)) from STATION;
答案 0 :(得分:30)
您可以在计数内使用select distinct 试试这种方式
select (count(CITY)- count(distinct CITY)) from STATION;
答案 1 :(得分:3)
select (count(CITY)- count(distinct CITY)) from STATION;
脚注:您可以查看以下sql函数 计算数学值:计数,平均,总和,+,-,*,%
答案 2 :(得分:3)
从车站选择COUNT(*)-COUNT(不同城市)
答案 3 :(得分:0)
foreach
别名为SELECT COUNT(CITY) - COUNT(DISTINCT CITY) AS N FROM STATION;
的查询结果。
答案 4 :(得分:0)
从STATION选择(count(CITY)-count(distinct CITY));
此查询对我有用!这是关于Hackerrank的问题。
答案 5 :(得分:0)
您可以编写许多查询来回答此问题,例如: 我正在考虑这个问题,因为我们有一个名为STATION的表,我们现在必须查找表中CITY条目的总数与表中不同CITY条目的数量之间的差异
查询1:
select (count(city)-count(distinct city)) from station;
查询2:
select ((select count(city) as ans1 from station)-(select count(distinct city)
as ans2 from station));
查询3:
select (( select count(city) from station )-( select count(distinct city) from station ))
as ans;
上述所有查询都可以使用。
答案 6 :(得分:-1)
SELECT (COUNT(CITY)- COUNT(DISTINCT(CITY))) AS diff
FROM STATION