ROW_NUMBER()SQL 2005查询返回Null

时间:2011-02-16 11:13:22

标签: sql-server-2005 row-number

由于Nulls的原因,我在SQL Server 2005中使用ROW_NUMBER()来比较来自表的两个不同时段的数据时遇到了一些问题。

如果有人能给我一些帮助,我会很高兴。

CREATE TABLE MOV (ID INT,DATE_TIMEID DATETIME,AMOUNT MONEY)

INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2011-01-04 00:00:00',    1238.75)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2010-01-05 00:00:00',    1220.75)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2011-01-05 00:00:00',    1000.50)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2010-01-06 00:00:00',    1111.25)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2011-01-06 00:00:00',    1065.25)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2010-01-07 00:00:00',    939.75)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2011-01-07 00:00:00',    1606.25)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2010-01-08 00:00:00',    1364.25)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2011-01-08 00:00:00',    2379.75)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2010-01-09 00:00:00',    908.25)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2011-01-09 00:00:00',    35.25)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1367, '2010-01-10 00:00:00',    1.50)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-04 00:00:00',    1164.80)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-05 00:00:00',    1385.50)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-06 00:00:00',    1683.90)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-07 00:00:00',    1420.80)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-08 00:00:00',    173.70) 
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-09 00:00:00',    117.50)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1368, '2010-01-10 00:00:00',    1052.20)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-03 00:00:00',    1052.20)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-04 00:00:00',    1164.80)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-05 00:00:00',    1385.50)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-06 00:00:00',    1683.90)
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-07 00:00:00',    1420.80)    
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-08 00:00:00',    173.70) 
INSERT INTO MOV (ID,DATE_TIMEID,AMOUNT)VALUES(1404, '2011-01-09 00:00:00',    117.50) 

以下是查询:

SELECT MOV1.ROWID,
       MOV1.ID,
       MOV1.DATE_TIMEID,
       MOV1.AMOUNT,
       MOV2.ROWID                    ROWID2,
       MOV2.ID                       ID2,
       MOV2.DATE_TIMEID              Date_timeID2,
       MOV2.AMOUNT                   Amount2,
       ( MOV1.AMOUNT - MOV2.AMOUNT ) Diff,
       CASE
         WHEN MOV1.AMOUNT = 0 THEN 0
         ELSE ( ( ( MOV1.AMOUNT - MOV2.AMOUNT ) * 100 ) / MOV1.AMOUNT )
       END                           AmountToPercent
from   (select ROW_NUMBER() OVER (ORDER BY CASE WHEN MOV.ID IS NULL THEN 0 ELSE
               MOV.ID
                      END,
               CASE WHEN MOV.DATE_TIMEID IS NULL THEN '01/01/1900' ELSE
               MOV.DATE_TIMEID
                      END ASC
               )
               AS
                      'ROWID',
               CAST(MOV.ID as CHAR(4)) /*+ ' - ' + V_ParkingUserAccess.ParkingName*/
               ID,
               DATE_TIMEID,
               Sum(AMOUNT)
               AMOUNT
        from   dbo.MOV
        where  MOV.ID between 1367 and 1404
               and DATE_TIMEID >= '01/03/2011'
               and DATE_TIMEID <= '01/09/2011'
        group  by MOV.ID,
                  DATE_TIMEID) MOV1
       INNER JOIN (select ROW_NUMBER() OVER (ORDER BY MOV.ID, MOV.DATE_TIMEID
                          ASC) AS
                          'ROWID',
                          MOV.ID
                          ID,
                          DATE_TIMEID
                                                       DATE_TIMEID,
                          Sum(AMOUNT)
                                                       AMOUNT
                   from   dbo.MOV
                   where  MOV.ID between 1367 and 1404
                          and DATE_TIMEID >= '01/04/2010'
                          and DATE_TIMEID <= '01/10/2010'
                   group  by MOV.ID,
                             DATE_TIMEID) MOV2
         ON MOV1.ROWID = MOV2.ROWID
            and MOV1.ID = MOV2.ID  

1 个答案:

答案 0 :(得分:1)

终于看到了。

ID = 1367的行包含01/03/2011..01/09/201101/04/2010..01/10/2010两个日期范围的日期值。因此,它们在内部连接的两侧被选中,并且相应地进行连接。

其他两个有一个日期范围或另一个。因此,其中一个子选择不返回任何行。因为它是一个INNER连接,所以最终不会为这些ID获取任何行。