如何组合这两个查询

时间:2018-04-10 18:37:57

标签: sql-server tsql

好的,我有2个查询,但我不确定如何合并它们,这是第一个:

SELECT
    e.EmplName,
    CAST(SUM(t.ManHrs) AS REAL) AS [Hrs Logged]
FROM EmplCode e 
    LEFT JOIN TimeTicketDet t ON e.EmplCode = t.EmplCode
WHERE CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)
    AND t.WorkCntr <> 50
    AND e.DeptNum LIKE 'PROD %'
    AND e.Active = 'Y'
GROUP BY e.EmplName
HAVING CAST(SUM(t.ManHrs) AS REAL) < 6

基本上,我想要完成的是编制一个每天6小时登录的员工列表。问题是,我无法捕获根本没有登录的员工。 EmplCode表的LEFT JOIN不起作用,因为

WHERE CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)

基本上将LEFT JOIN变为INNER JOIN。我列出所有员工的查询是这样的:

SELECT
    e.EmplName
FROM EmplCode e
WHERE e.DeptNum LIKE 'PROD %'
    AND e.Active = 'Y'
GROUP BY e.EmplName

但是有了这个故障辩论是我很难解决的问题。如何获得今天所有员工及其登录时间的清单,同时还包括今天没有时间门票的人员?我尝试做一个子查询,但是当我仅过滤今天的票时,我无法绕过它,而没有完全消除空值

2 个答案:

答案 0 :(得分:3)

将此:CAST(t.TicketDate AS DATE) = CAST(GETDATE() AS DATE)移动到ON子句而不是WHERE子句。

这也是:AND t.WorkCntr <> 50

可能还需要修改HAVING子句,以包括没有ManHrs的员工。将OR SUM(t.ManHrs) IS NULL添加到最后可能会这样做,但我没有对此进行测试。

答案 1 :(得分:0)

library(shiny) library(shinydashboard) library(data.table) library(DT) ui <- dashboardPage(title = "Dashboard Title", dashboardHeader(title = "My Dashboard"), dashboardSidebar( sidebarMenu(id = "menu", menuItem(text = "Data", icon = icon("database"), tabName = "inputData", fileInput(inputId = "file", label = "Choose CSV File", multiple = TRUE, accept = ".csv")), menuItem(text = "My Items", tabName = "items", icon = icon("book"), menuSubItem(text = "Item 1", tabName = "item01"), menuSubItem(text = "Item 2", tabName = "item02"), menuSubItem(text = "Item 3", tabName = "item03") ) ) ), dashboardBody( tabItems( tabItem(tabName = "inputData", class = "active", h1(textOutput("heading")), dataTableOutput("loaded.data")), tabItem(tabName = "items", h1(textOutput("heading0")), class = "active", tabItem(tabName = "item01", class = "active", h1(textOutput("heading1")), dataTableOutput("table1")), tabItem(tabName = "item02", class = "active", h1(textOutput("heading2")), dataTableOutput("table2")), tabItem(tabName = "item03", class = "active", h1(textOutput("heading3")), dataTableOutput("table3")) ) ) ) ) server <- function(input, output) { # Load the data and assign it to a reactive object df <- reactive({ inFile <- input$file if(is.null(inFile)) { return(NULL) } else { tbl <- fread(input$file$datapath, sep = ",", quote = '"', stringsAsFactors = TRUE) return(tbl) } }) output$heading <- renderText({ if(is.null(df())) { return(NULL) } else { return("Data loaded") } }) output$loaded.data <- renderDT({ if(is.null(df())) { return(NULL) } else { df() } }) output$heading0 <- renderText({ if(is.null(df())) { return(NULL) } else { return("In the sub-menus below you will find the tables") } }) output$heading1 <- renderText({ if(is.null(df())) { return(NULL) } else { return("Heading item 1") } }) output$table1 <- renderDT({ if(is.null(df())) { return(NULL) } else { return(df()[ , c("VAR1", "VAR2")]) } }) output$heading2 <- renderText({ if(is.null(df())) { return(NULL) } else { return("Heading item 2") } }) output$table2 <- renderDT({ if(is.null(df())) { return(NULL) } else { return(df()[ , c("VAR2", "VAR3")]) } }) output$heading3 <- renderText({ if(is.null(df())) { return(NULL) } else { return("Heading item 3") } }) output$table3 <- renderDT({ if(is.null(df())) { return(NULL) } else { return(df()[ , c("VAR2", "VAR3")]) } }) } shinyApp(ui, server) int杀死e

的位置
left