0

ROW_NUMBER()Null が原因で、SQL Server 2005 で使用するテーブルの 2 つの異なる期間のデータを比較する際に問題が発生します。

どなたかお力添えを頂ければ幸いです。

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  
4

1 に答える 1

1

最後にそれを見た。

の行には、とID = 1367の両方の日付範囲の日付値が含まれています。したがって、それらは内部結合の両側で選択され、それに応じて結合が行われます。01/03/2011..01/09/201101/04/2010..01/10/2010

他の 2 つは、どちらか一方の日付範囲を持っています。したがって、副選択の 1 つは行を返しません。また、これは INNER 結合であるため、最終的にそれらの ID の行は取得されません。

于 2011-02-17T08:12:10.097 に答える