在这种情况下如何有效地编写这个SQL查询

时间:2014-05-08 16:43:50

标签: sql-server-2008 tsql join

这里我有一个类似下面的查询,我循环子ID并将其传递给此查询并获得适当的结果。这里我有33个子ID,因此此查询的结果包含33行

DECLARE @DATETIMENOW DATETIME  
SET @DATETIMENOW = GETDATE()-7 

SELECT COUNT(1) FROM  BKA.CHILDEVENTS CHE JOIN BKA.CHILDEVENTPROPERITIES CHEP ON CHEP.EVENTID = CHE.EVENTID  
                      WHERE (CHE.TYPE = 'ACCIDENT' OR (CHE.TYPE = 'BREAK' AND CHEP.PROPERTY = 'SUCCESS' AND CHEP.PROPERTYVALUE = 'FALSE'))  
                      AND CHE.CHILDID = @CHILDID AND CHE.ADDDATE BETWEEN DATEADD(DD, -(DATEPART(DW, @DATETIMENOW-7)-1), @DATETIMENOW-7) AND  
                      DATEADD(DD, 7-(DATEPART(DW, @DATETIMENOW-7)), @DATETIMENOW-7) AS PREVIOUSWEEKACCIDENTS

这里没有循环我已尝试在此查询中使用join的childid,而是将结果放在一行中。我在这个

做错了什么
DECLARE @DATETIMENOW DATETIME  
SET @DATETIMENOW = GETDATE()-7 
SELECT COUNT(1) FROM  BKA.CHILDEVENTS CHE JOIN BKA.CHILDEVENTPROPERITIES CHEP 
                      ON CHEP.EVENTID = CHE.EVENTID 
                      JOIN  BKA.CHILDINFORMATION CHINFO
                      ON CHE.CHILDID = CHINFO.CHILDID
                      WHERE (CHE.TYPE = 'ACCIDENT' OR (CHE.TYPE = 'BREAK' AND CHEP.PROPERTY = 'SUCCESS' AND CHEP.PROPERTYVALUE = 'FALSE'))  
                      AND CHE.ADDDATE 
                      BETWEEN DATEADD(DD, -(DATEPART(DW, @DATETIMENOW-7)-1), @DATETIMENOW-7) AND  
                      DATEADD(DD, 7-(DATEPART(DW, @DATETIMENOW-7)), @DATETIMENOW-7)

有什么建议吗?

3 个答案:

答案 0 :(得分:3)

您需要添加GROUP BY子句:

SELECT  CHINFO.CHILDID
      , COUNT(1)
FROM    BKA.CHILDEVENTS CHE
        JOIN BKA.CHILDEVENTPROPERITIES CHEP ON CHEP.EVENTID = CHE.EVENTID
        JOIN BKA.CHILDINFORMATION CHINFO ON CHE.CHILDID = CHINFO.CHILDID
WHERE   ( CHE.TYPE = 'ACCIDENT'
          OR ( CHE.TYPE = 'BREAK'
               AND CHEP.PROPERTY = 'SUCCESS'
               AND CHEP.PROPERTYVALUE = 'FALSE'
             )
        )
        AND CHE.ADDDATE BETWEEN DATEADD(DD,
                                        -( DATEPART(DW, @DATETIMENOW - 7) - 1 ),
                                        @DATETIMENOW - 7)
                        AND     DATEADD(DD,
                                        7 - ( DATEPART(DW, @DATETIMENOW - 7) ),
                                        @DATETIMENOW - 7)
GROUP BY CHINFO.CHILDID

答案 1 :(得分:1)

where中的值将使外连接无效

SELECT CHE.CHILDID
     , COUNT(1)
  FROM BKA.CHILDEVENTS CHE
  LEFT JOIN BKA.CHILDEVENTPROPERITIES CHEP 
    ON CHEP.EVENTID = CHE.EVENTID
   AND ( CHE.TYPE = 'ACCIDENT'
          OR ( CHE.TYPE = 'BREAK'
               AND CHEP.PROPERTY = 'SUCCESS'
               AND CHEP.PROPERTYVALUE = 'FALSE'
             )
       )
   AND CHE.ADDDATE BETWEEN DATEADD(DD,
                                        -( DATEPART(DW, @DATETIMENOW - 7) - 1 ),
                                        @DATETIMENOW - 7)
                       AND DATEADD(DD,
                                        7 - ( DATEPART(DW, @DATETIMENOW - 7) ),
                                        @DATETIMENOW - 7)    
GROUP BY CHE.CHILDID

答案 2 :(得分:0)

          DECLARE @DATETIMENOW DATETIME  
          SET @DATETIMENOW = GETDATE()

              SELECT  B.WEEK FROM BKA.CHILDINFORMATION CI LEFT OUTER JOIN 

              (SELECT Distinct CHINFO.CHILDID,COUNT(*)  as week FROM  BKA.CHILDINFORMATION CHINFO               
               JOIN  BKA.CHILDEVENTS CHE 
                            ON CHE.CHILDID = CHINFO.CHILDID                                                       
               JOIN BKA.CHILDEVENTPROPERITIES CHEP 
                            ON  CHE.EVENTID = CHEP.EVENTID                         
              WHERE 
                          (CHE.TYPE = 'ACCIDENT' OR (CHE.TYPE = 'POTTYBREAK' AND CHEP.PROPERTY = 'SUCCESS' 
                          AND CHEP.PROPERTYVALUE = 'FALSE'))  
                            AND 
                            CHE.ADDDATE 
                            BETWEEN DATEADD(DD, -(DATEPART(DW, @DATETIMENOW-14)-1), @DATETIMENOW-14) AND  
                            DATEADD(DD, 7-(DATEPART(DW, @DATETIMENOW-14)), @DATETIMENOW-14)      group by CHINFO.CHILDID) b
                            on CI.ChildID = b.ChildID