TSQL Order BY 有时无法正确排序 [英] TSQL Order BY on occasion doesn't order correctly

查看:30
本文介绍了TSQL Order BY 有时无法正确排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TSQL MSSQL 2008r2

TSQL MSSQL 2008r2

我正在重新编写问题,试图弄清楚我要解释的问题是什么.

I'm re-writing the question to try and make it clear what the issue is that I'm trying to explain.

我有一个带有 3 个参数的存储过程.VehicleKey、StartDate 和 EndDateTime.我正在查询数据仓库数据库.所以数据不应该改变.

I've got a stored proc that takes 3 parameters. VehicleKey, StartDate and EndDateTime. I'm querying a Data Warehouse db. So the data shouldn't change.

当使用相同的参数调用 proc 时,大多数时候结果会如预期的那样,但在某些随机情况下,使用相同的参数,结果会有所不同.我正在查询数据 WH,因此数据不会更改.

When the proc is called with the same parameters then most of the time the results will be as expected but on some random occasions, with those same parameters, the results differ. I'm querying a Data WH so the data doesn't change.

问题在于动态派生列Island".

The problem is with the dynamic derived column "Island".

这完全是随机的.proc 可以执行 20 次并给出预期的结果,然后接下来的 2 次将给出错误的结果.

It's completely random. The proc can be executed 20 times and give the expected results and then the next 2 will give incorrect results.

在给定日期范围内可以有 1 个或多个 VehicleKey/DriverKey 组合.

There can be 1 or more VehicleKey/DriverKey combinations in a given date range.

这是问题查询

SELECT
     A.VehicleKey
    ,A.NodeId
    ,A.DriverKey
    ,MIN(A.StartTrip)   'StartTrip'
    ,MAX(A.EndTrip)     'EndTrip'
    ,SUM(A.PrivOdo)     'Private'
    ,SUM(A.BusOdo)      'Business'
    ,SUM(A.TravOdo)     'Travel'
    ,SUM(A.PrivOdo + A.BusOdo + A.TravOdo )'Total'
FROM
(
    SELECT
         Island = ( ROW_NUMBER() OVER (PARTITION BY T.VehicleKey ORDER BY MONTH(StartTrip)) ) - ( ROW_NUMBER() OVER (PARTITION BY T.VehicleKey, T.DriverKey ORDER BY T.StartTrip) )
        ,NodeId
        ,VehicleKey
        ,DriverKey
        ,StartTrip
        ,EndTrip
        ,BusOdo
        ,PrivOdo
        ,TravOdo
    FROM 
        #xYTD_BPTotals T
) AS A
GROUP BY
     A.Island
    ,A.VehicleKey
    ,A.NodeId
    ,A.DriverKey
ORDER BY 
     A.VehicleKey
    ,MIN(A.StartTrip); 

我的理解是 ORDER BY 应该在派生表的外部才能生效.

I am of the understanding that the ORDER BY should be on the outside of the derived table for it to take effect.

我想我已经将问题缩小到只有当车辆具有 2 个或更多 DriverKey 组合时才会出现的问题.

I think I've narrowed it down to the issue presenting itself only when a Vehicle has 2 or more DriverKey combinations.

例如,Parameters VehicleKey 4865, StartDateTime = '2016-01-01', EndDateTime = '2016-10-31'这是正确的结果 - 包括 Island 列

for example, Parameters VehicleKey 4865, StartDateTime = '2016-01-01', EndDateTime = '2016-10-31' This is the correct result - including Island column

VehicleKey    NodeId    DriverKey    Island    StartTrip              EndTrip               Private    Business    Travel    Total_
4865          458       0            0         2016-09-06 14:06:08    2016-09-28 17:02:08   54.75      737.83      0         792.58
4865          458       1202         134       2016-09-29 11:10:04    2016-09-30 17:25:51   0          211.32      0         211.32
4865          458       0            27        2016-10-03 07:39:25    2016-10-14 17:00:15   0          579.81      0         579.81

这是错误的时候.参数 VehicleKey 4865, StartDateTime = '2016-01-01', EndDateTime = '2016-10-31'- 包括岛列这里的前两行应该合并.

and this is when it's wrong. Parameters VehicleKey 4865, StartDateTime = '2016-01-01', EndDateTime = '2016-10-31' - including Island column The first two rows here should be combined.

VehicleKey    NodeId    DriverKey    Island    StartTrip              EndTrip               Private    Business    Travel    Total_
4865          458       0            98        2016-09-06 14:06:08    2016-09-21 09:15:49   0          313.87      0         313.87
4865          458       0            -63       2016-09-21 09:21:10    2016-09-28 17:02:08   54.75      423.96      0         478.71
4865          458       1202         71        2016-09-29 11:10:04    2016-09-30 17:25:51   0          211.32      0         211.32
4865          458       0            27        2016-10-03 07:39:25    2016-10-14 17:00:15   0          579.81      0         579.81

如果我显示派生表的前几行,我已经分解了岛"列

If I show the first few rows from the derived table, I've broken down the "Island" column

SELECT
     Island = ( ROW_NUMBER() OVER (PARTITION BY T.VehicleKey ORDER BY MONTH(StartTrip)) ) - ( ROW_NUMBER() OVER (PARTITION BY T.VehicleKey, T.DriverKey ORDER BY T.StartTrip) )
    ,Island_x =( ROW_NUMBER() OVER (PARTITION BY T.VehicleKey ORDER BY MONTH(StartTrip)) )
    ,Island_y = ( ROW_NUMBER() OVER (PARTITION BY T.VehicleKey, T.DriverKey ORDER BY T.StartTrip) )
    ,NodeId
    ,VehicleKey
    ,DriverKey
    ,StartTrip
    ,EndTrip
    ,BusOdo
    ,PrivOdo
    ,TravOdo
FROM 
    #xYTD_BPTotals T

正确的结果应该是

Island  Island_x    Island_y    NodeId  VehicleKey  DriverKey   StartTrip   EndTrip BusOdo  PrivOdo TravOdo
0   1   1   24901   4865    0   2016-09-06 14:06:08 2016-09-06 14:08:50 0   0   0
0   2   2   24901   4865    0   2016-09-06 15:39:14 2016-09-06 15:40:53 114 0   0
0   3   3   24901   4865    0   2016-09-08 11:06:43 2016-09-08 11:07:23 0   0   0
0   4   4   24901   4865    0   2016-09-08 11:12:03 2016-09-08 11:12:26 20  0   0
0   5   5   24901   4865    0   2016-09-08 11:19:20 2016-09-08 11:19:52 1   0   0
0   6   6   24901   4865    0   2016-09-08 11:26:58 2016-09-08 11:27:56 88  0   0
0   7   7   24901   4865    0   2016-09-08 11:33:40 2016-09-08 11:35:02 1   0   0
0   8   8   24901   4865    0   2016-09-12 09:08:53 2016-09-12 09:10:42 34  0   0

但有时我会用相同的输入参数得到这个.

but I sometimes get this with the same input paramaters.

Island  Island_x    Island_y    NodeId  VehicleKey  DriverKey   StartTrip   EndTrip BusOdo  PrivOdo TravOdo
98  1   1   24901   4865    0   2016-09-06 14:06:08 2016-09-06 14:08:50 0   0   0
98  2   2   24901   4865    0   2016-09-06 15:39:14 2016-09-06 15:40:53 114 0   0
98  3   3   24901   4865    0   2016-09-08 11:06:43 2016-09-08 11:07:23 0   0   0
98  4   4   24901   4865    0   2016-09-08 11:12:03 2016-09-08 11:12:26 20  0   0
98  5   5   24901   4865    0   2016-09-08 11:19:20 2016-09-08 11:19:52 1   0   0
98  6   6   24901   4865    0   2016-09-08 11:26:58 2016-09-08 11:27:56 88  0   0
98  7   7   24901   4865    0   2016-09-08 11:33:40 2016-09-08 11:35:02 1   0   0
98  8   8   24901   4865    0   2016-09-12 09:08:53 2016-09-12 09:10:42 34  0   0

为什么Island"计算列错误?1-1 = 0 不是 98.

Why is the "Island" calculated column wrong? 1-1 = 0 not 98.

我哪里出错了?

推荐答案

编辑 - @YourData 现在看起来像你的原始表

Declare @YourTable table (VehicleKey int,NodeId int,DriverKey int,StartTrip datetime,EndTrip datetime,PrivOdo decimal(10,2),BusOdo decimal(10,2), TravOdo decimal(10,2))
Insert Into @YourTable values
(4865,458,0   ,'2016-09-06 14:06:08','2016-09-21 09:15:49',0    ,313.87,0),
(4865,458,0   ,'2016-09-21 09:21:10','2016-09-28 17:02:08',54.75,423.96,0),
(4865,458,1202,'2016-09-29 11:10:04','2016-09-30 17:25:51',0    ,211.32,0),
(4865,458,0   ,'2016-10-03 07:39:25','2016-10-14 17:00:15',0    ,579.81,0)

Select VehicleKey
      ,NodeID
      ,VehicleKey
      ,DriverKey
      ,StartTrip = min(StartTrip)
      ,EndTrip   = max(EndTrip)
      ,Private   = sum(PrivOdo)
      ,Business  = sum(BusOdo)
      ,Travel    = sum(TravOdo)
      ,Total     = sum(PrivOdo + BusOdo + TravOdo )
 From (
 Select  Island = ( ROW_NUMBER() OVER (PARTITION BY VehicleKey ORDER BY MONTH(StartTrip)) ) - ( ROW_NUMBER() OVER (PARTITION BY VehicleKey, DriverKey ORDER BY StartTrip) )
        ,*
 From @YourTable 
) A
Group By Island,VehicleKey,NodeID,VehicleKey,DriverKey
Order By min(StartTrip)

退货

仅供参考 - 子查询产生

FYI - The sub-query produces

这篇关于TSQL Order BY 有时无法正确排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆