SQL Count返回0

时间:2015-09-02 20:59:14

标签: mysql sql postgresql

我想搜索一个表的所有活动记录,并在第二个表中查找它们各自的数据,并且如果没有找到则返回0的计数

这是我的例子。

SELECT employee.id, count(jobs.id)
FROM employee LEFT OUTER JOIN jobs ON employee.id = jobs.employee_id
where employee.state ='active' and jobs.startdatetime >= date(now())
group by employee.id

如果我运行w / o startdatetime范围,它将返回正确的行数 如果我添加startdatetime范围,那么所有没有工作的员工都会被删除

2 个答案:

答案 0 :(得分:1)

正如已经评论过的那样,将条件## example library("ggplot2") library(grid) # Multiple plot function # # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) # - cols: Number of columns in layout # - layout: A matrix specifying the layout. If present, 'cols' is ignored. # # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), # then plot 1 will go in the upper left, 2 will go in the upper right, and # 3 will go all the way across the bottom. # multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # If layout is NULL, then use 'cols' to determine layout if (is.null(layout)) { # Make the panel # ncol: Number of columns of plots # nrow: Number of rows needed, calculated from # of cols layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols)) } if (numPlots==1) { print(plots[[1]]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for (i in 1:numPlots) { # Get the i,j matrix positions of the regions that contain this subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } example1 <- read.table(text="x height w wm color colorcode A 74.87091 0 0.8477582 PT E6F598FF B 75.7462 0.8477582 2.7575227 ES 51AAAEFF C 154.96351 2.7575227 5.0641208 IT 9DD7A4FF D 218.2934 5.0641208 5.582455 EL 3C93B8FF",header=TRUE) example1$colorcode <- paste("#",example1$colorcode,sep="") #example2 <- example1[-1,] example3 <- read.table(text=" x height w wm color colorcode E 23.31359 0 2.619406 BG B41947FF F 28.60724 2.619406 3.282477 HU FEE08BFF G 33.30486 3.282477 3.292582 CY DB474CFF H 34.40072 3.292582 24.22946 PL FDCC7AFF I 42.86401 24.22946 26.141007 RO 9E0142FF C 45.83487 26.141007 47.13058 IT 9DD7A4FF J 46.48877 47.13058 48.152381 SI FDB869FF K 47.74536 48.152381 51.518293 CZ FEEC9FFF L 50.12508 51.518293 278.176833 FR 66C2A5FF M 72.17257 278.176833 284.513883 DK 4C65ACFF N 73.16484 284.513883 285.837194 SK FBA15BFF O 75.94599 285.837194 314.661756 AT CEEB9CFF P 77.39849 314.661756 339.818609 BL 5E4FA2FF D 77.87686 339.818609 340.366524 EL 3C93B8FF Q 78.60072 340.366524 623.890373 DE 3A7CB7FF R 80.39367 623.890373 624.871331 EE F7874FFF S 83.13369 624.871331 626.13241 LV E75A47FF T 87.46645 626.13241 628.725496 LT F46D43FF U 89.35913 628.725496 629.62701 FI FAFDB7FF V 94.46328 629.62701 766.334365 NL B6E1A1FF W 106.02335 766.334365 871.746899 IR 81CCA4FF X 107.62544 871.746899 881.874454 SE F0F9A7FF B 112.60728 881.874454 892.735907 ES 51AAAEFF Y 127.36419 892.735907 991.546621 UK FEF8B4FF Z 342.85699 991.546621 997.847212 PT E6F598FF",header=TRUE) example3$colorcode <- paste("#",example3$colorcode,sep="") #example4 <- example3[-1,] example1[,c("wm","w","height")] <- example1[,c("wm","w","height")] + 10 #example2[,c("wm","w","height")] <- example2[,c("wm","w","height")] + 10 example3[,c("wm","w","height")] <- example3[,c("wm","w","height")] + 10 #example4[,c("wm","w","height")] <- example4[,c("wm","w","height")] + 10 example.data <- list(example3,example1) listplota <- list() listplotb <- list() AxisX_max <- 1100 AxisY_max <- 360 AxisX_min <- 0 AxisY_min <- 0 for(i in 1:2){ df.1 <- example.data[[i]] p5 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = x)) + geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = x, size=16, angle=90)) + theme_bw() + labs(x = "cummulated", y = "costs") + theme(axis.ticks = element_blank(), legend.position = "none") + #ylim(-150, 400) + xlim(-150, 1000) + ylim(max(-150,AxisY_min), min(400,AxisY_max)) + xlim(AxisX_min, AxisX_max) + scale_fill_manual(values=df.1$colorcode) + ggtitle(paste("type ",i,sep="- ") ) + theme(plot.title = element_text(lineheight=.8, face="bold")) listplota[[i]] <- p5 p55 <- ggplot(df.1, aes(xmin = wm, xmax = w, ymin = 0, ymax = height, fill = x)) + geom_rect(color="darkgrey")+ geom_text(aes(x = (wm+w)/2, y = 150 , label = x, size=16, angle=90)) + theme_bw() + labs(x = "cummulated", y = "costs") + theme(axis.ticks = element_blank(), legend.position = "none") + #ylim(-150, 400) + xlim(-150, 1000) + ylim(max(-150,AxisY_min), min(400,AxisY_max)) + xlim(AxisX_min, AxisX_max) + scale_fill_manual(values=df.1$colorcode) + ggtitle(paste("type ",i,sep="- ") ) + theme(plot.title = element_text(lineheight=.8, face="bold")) listplotb[[i]] <- p55 } multiplot(plotlist=c(listplota,listplotb),cols=2) 移动到您的jobs.startdatetime >= date(now())子句作为连接条件而非

JOIN

答案 1 :(得分:0)

只需使用

and (jobs.startdatetime >= date(now()) or jobs.startdatetime is null) 

它会起作用