我正在尝试复制此表:
列出制造商工厂少于2个的城市的所有名称。我试图通过使用HAVING(*)并将它与INNER JOIN混合来实现这些方法,但我不确定我是否在正确的轨道上。
我试图通过查看堆栈上的其他问题以两种不同的方式编写代码:
1
library(tidyverse)
base = c(1.84,3.92,1.67,1.12,1.63,.62,.59)
e1 = c(.61,1.47,1.68,1.95,1.64,.61,.72)
e2 = c(.64,7.08,1.67,1.12,1.44,.46,.76)
e3 = c(.64,4.47,1.68,2.04,1.45,.4,1.35)
e4 = c(.78,1.61,1.62,1.09,1.46,.66,.76)
e5 = c(.78,.99,1.62,2.32,1.46,.73,.52)
df = data.frame(base, e1, e2, e3, e4, e5)
# calculate colwise differences
df %>%
map_df( ~ (. - base)) %>%
select(-base) %>%
# create id for each number
mutate(id = row_number()) %>%
# convert to long format
gather(key = "model", value = "diff", -id) -> df_dif
# plot the differences
ggplot(df_dif, aes(x = id, y = diff)) +
geom_col(aes(fill = model), position = "dodge") +
facet_grid(~ model) +
theme_classic()
结果不准确但如果我将(ManID)更改为小于2,则不会有任何结果。
2
SELECT c.CityName as "City Name", m.CityID as "Number of Manufacturers"
FROM CITY4787 c
INNER JOIN MANUFACTURER4787 m
ON c.CityID = m.CityID
WHERE m.ManName IN (SELECT m.ManName FROM MANUFACTURER4787
GROUP BY m.ManID HAVING COUNT (ManID) > 2);
结果设法计算出现少于2但仍然不对的城市人。
我真的不明白如何合并这两个或如果我正确使用这些sql语句。这是ERD:ERD
答案 0 :(得分:2)
根据您的架构,我认为这应该有效:
SELECT c.CityName as "City Name", COUNT(m.ManName) AS "Number of Manufacturers"
FROM City c
LEFT JOIN Manufacturer m
ON m.CityID = c.CityID
GROUP BY c.CityID
HAVING COUNT(m.ManName) < 2
答案 1 :(得分:1)
如果我说错了,你可以尝试一下。
如果您没有预料到这一点,您能否提供一些示例数据我会编辑我的答案。
SELECT c.CityName as "City Name", COUNT(m.ManID) as "Number of Manufacturers"
FROM CITY4787 c
LEFT JOIN MANUFACTURER4787 m ON c.CityID = m.CityID
GROUP BY c.CityName
HAVING COUNT(m.ManID) < 2