如何使用单个查询查找表中没有记录,也没有重复项?

时间:2019-02-22 10:41:40

标签: mysql sql sql-server

我想知道的是,使用单个查询可以编写查询以查找没有记录和没有重复记录的查询  在这里,我知道下面没有记录是查询

StatMovingWindow <- ggproto("StatMovingWindow", Stat,
  compute_group = function(data, scales, wSize, wStep, fun) {
    moveWin(data, wSize = wSize, wStep = wStep, f = fun)
  },

  required_aes = c("x", "y")
)
stat_movingwindow <- function(mapping = NULL, data = NULL, 
  fun = function(d) quantile(d, probs = c(0.05, 0.50, 0.95), na.rm = TRUE),
  wStep = 0.1, wSize = 1,
  geom = "smooth", position = "identity", show.legend = NA, inherit.aes = TRUE,
  ...
){
  layer(
    stat = StatMovingWindow, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(wStep = wStep, wSize = wSize, fun = fun, ...)
  )
}

ggplot(data = d) + aes(x = x, y = y) +
  stat_movingwindow(wStep = 0.1, wSize = 1) + 
  geom_point() + scale_x_continuous(breaks = 1:10)

,没有重复的查询是

 select count(*) from table

这里国家是表中的一列名称。但是当我尝试这样写时,我需要一个查询来显示表中记录的总数以及国家列中的重复记录。

select count(country) from table group by country having count(country)>1

但是我没有锻炼 我认为这是我写的最糟糕的查询,但是请原谅我..有人可以帮我找这个吗?

谢谢

4 个答案:

答案 0 :(得分:1)

您可以使用subquery

SELECT COUNT(country) AS duplicate_country, 
      (SELECT COUNT(*) FROM table t1) AS Total_count
FROM table t
GROUP BY country
HAVING COUNT(country) > 1;

答案 1 :(得分:0)

如果您想复制一个国家/地区,可以使用

    select country, count(*) 
    from table 
    group by country 
    having count(country)>1

答案 2 :(得分:0)

您可以在下面尝试-

SELECT COUNT(*)  as total_counts, 
      (SELECT COUNT(country) FROM table tablename A GROUP BY country
              HAVING COUNT(country) > 1) as duplicates
from tablename;

答案 3 :(得分:0)

如果您只想要数字,那么我认为您想要:

select count(*) as num_records,
       count(*) - count(distinct country) as num_duplicates
from t;