2015-03-25 111 views
0

我有一個MS sql查詢,其中我計算了我的車的總公里數。爲了實現這一點,我創建了一個查詢,獲取最後的數據並從第一個數據中減去它。我可以每天實現這一目標,但現在我需要的是每天都有,但在日期範圍之內。這是我的代碼1日期過濾器。從ms sql中的第一個數據中減去最後一個數據,但是每天都這樣做

SELECT TOP 1 
(
    (
     (SELECT TOP 1 cast(kmrun as decimal(7,2)) as totkm 
      from [MARK_PASSIVE].[dbo].tblkmrun2 
       where PHILTIME BETWEEN '2015-03-02 00:00:00' AND '2015-03-02 23:59:00' 
        AND PLATENO = 'UUM572' ORDER BY PHILTIME DESC) 
        - 
     (SELECT TOP 1 cast(kmrun as decimal(7,2)) as totkm 
      from [MARK_PASSIVE].[dbo].tblkmrun2 
       where PHILTIME BETWEEN '2015-03-02 00:00:00' AND '2015-03-02 23:59:00' 
        AND PLATENO = 'UUM572' ORDER BY PHILTIME ASC) 
    ) 
) as finkm , 
(CONVERT(char(10), PHILTIME, 126)) as NEWDATE 
from [MARK_PASSIVE].[dbo].tblkmrun2 
where PHILTIME BETWEEN '2015-03-02 00:00:00' AND '2015-03-02 23:59:00' 

現在這裏是我的代碼日期範圍和結果以及。結果顯示相同的,但由於即時得到總公里運行的每一天不是針對整個日期範圍

SELECT DISTINCT (CONVERT(char(10), PHILTIME, 126)), 
( 
    (
     (
      SELECT TOP 1 cast(kmrun as decimal(7,2)) as totkm 
       from [MARK_PASSIVE].[dbo].tblkmrun2 
        where PHILTIME BETWEEN '2015-03-01 00:00:00' AND '2015-03-06 23:59:00' 
         AND PLATENO = 'UUM572' group by kmrun ORDER BY PHILTIME DESC 
     ) - 
     (
      SELECT TOP 1 cast(kmrun as decimal(7,2)) as totkm 
       from [MARK_PASSIVE].[dbo].tblkmrun2 
        where PHILTIME BETWEEN '2015-03-01 00:00:00' AND '2015-03-06 23:59:00' 
         AND PLATENO = 'UUM572' ORDER BY PHILTIME ASC 
     ) 
    ) 
) as finkm , 
(CONVERT(char(10), PHILTIME, 126)) as NEWDATE 
    from [MARK_PASSIVE].[dbo].tblkmrun2 
     where PHILTIME BETWEEN '2015-03-01 00:00:00' AND '2015-03-06 23:59:00' 
      ORDER BY NEWDATE DESC 

這裏的結果日期範圍

enter image description here

一定是不一樣的這裏是預期的結果

enter image description here

與我的查詢日期範圍內的問題是計算整個日期範圍,從1開始獲取第一個數據,從6開始獲取最後一個數據。

請幫助我爲日期範圍重新配置我的sql查詢。

由於

數據爲March1

enter image description here

數據爲MAR2

enter image description here

數據爲Mar3

enter image description here

數據爲3月4日

enter image description here

數據爲3月5日

enter image description here

數據爲3月6日

enter image description here

結果可能會發生變化SI我只限於5個數據。

+0

你能提供我們可以使用一些樣本數據? – 2015-03-25 06:23:36

+0

更新了我的帖子先生。 – Dodgeball 2015-03-25 06:32:37

回答

1

試試這個:

DECLARE @startDate DATE = '20150301' 
DECLARE @endDate DATE = '20150306' 

;WITH Cte AS(
    SELECT *, 
     FirstPhilTime = ROW_NUMBER() OVER(PARTITION BY CAST(PhilTime AS DATE) ORDER BY PhilTime ASC), 
     LastPhilTime = ROW_NUMBER() OVER(PARTITION BY CAST(PhilTime AS DATE) ORDER BY PhilTime DESC) 
    FROM tblkmrun2 
) 
SELECT 
    CAST(PhilTime AS DATE), 
    FinKm = MAX(CASE WHEN LastPhilTime = 1 THEN KmRun END) - MAX(CASE WHEN FirstPhilTime = 1 THEN KmRun END) 
FROM cte 
WHERE 
    PhilTime >= @STARTDATE 
    AND PhilTime < DATEADD(DAY, 1, @ENDDATE) 
GROUP BY CAST(PhilTime AS DATE) 
+0

它在某些日期返回否定答案。 – Dodgeball 2015-03-26 03:08:07

+1

@Dodgeball,是的,如果最後一個'PHILTIME'的'kmrun'小於第一個'PHILTIME'的'kmrun',它會。 – 2015-03-26 03:23:38

+0

調整了一下。感謝所有的幫助,併爲這個麻煩道歉。 – Dodgeball 2015-03-26 03:51:19

相關問題