查询以为行分配序列号,而无需分组在一起并且不更改行的顺序

问题描述

此表由将集装箱运送到另一个位置的卡车行程组成。卡车每次搬运集装箱时,都会存储记录。如果载有1个单位,则将“ 1个单位”存储在Unitcount字段下,并将TripCount分配为1。如果卡车载有2个单位,则Unitcount字段值将保存为“ 2个单位”,而TripCount则保存为0.5 >

下面是表的内容-这里Tid是主键:

我需要以下输出

也就是说,如果驱动程序携带2个单元,则“工作序列号”值应存储编号。他工作的旅行次数,并且必须为他携带2个单位的旅行保持恒定的价值。此输出的目的是,我将使用此“工作序列”列来计算其旅行的旅行费用。像是,他的第一次旅行获得20美元,第二次旅行获得20美元,第三次旅行获得25美元,第四次旅行获得30美元。我的问题是,但是我尝试过,我的查询计算为6次旅行,实际上他只执行了4次旅行。希望我能够解释我的要求。我了解,这是非常简单的查询;但不幸的是我无法解决

请注意,行的顺序不应更改。

很抱歉,您的解释不明确。我只指定了一个驱动程序名称和单个日期,这是我的错误。实际上,我是在计算特定星期和所有驾驶员的驾驶员出行成本。尽管我已经从表中复制了可能的值并粘贴到了这里。

enter image description here

使用以下脚本创建表并插入数据(用于解决方法

CREATE TABLE [dbo].[Test_Table](
[Tid] [bigint] NOT NULL,[DriverName] [nvarchar](50) NULL,[Cardgdate] [date] NULL,[Dircid] [int] NULL,[Load] [nvarchar](50) NULL,[UnitCount] [nchar](10) NULL,[Result] [int] NULL,CONSTRAINT [PK_Test_Table] PRIMARY KEY CLUSTERED 
(
    [Tid] ASC
)WITH (PAD_INDEX = OFF,STATISTICS_norECOmpuTE = OFF,IGnorE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [dbo].[Test_Table]([Tid],[DriverName],[Cardgdate],[Dircid],[Load],[UnitCount],[Result]) VALUES (253293,N'Naveed Khan','20200823 00:00:00.000',34,N'Terminal',N'1 Unit    ',0)
INSERT INTO [dbo].[Test_Table]([Tid],[Result]) VALUES (253320,N'Aas Muhammad',30,[Result]) VALUES (253358,N'Danish Imtiaz',[Result]) VALUES (253407,[Result]) VALUES (253434,[Result]) VALUES (253449,[Result]) VALUES (253492,[Result]) VALUES (253516,'20200824 00:00:00.000',N'Agent',[Result]) VALUES (253520,[Result]) VALUES (253525,[Result]) VALUES (253576,[Result]) VALUES (253592,[Result]) VALUES (253599,[Result]) VALUES (254647,'20200825 00:00:00.000',[Result]) VALUES (254659,[Result]) VALUES (254664,[Result]) VALUES (254711,[Result]) VALUES (254743,N'Chandra Shekar',[Result]) VALUES (254744,[Result]) VALUES (254745,[Result]) VALUES (254783,[Result]) VALUES (254785,[Result]) VALUES (254802,'20200826 00:00:00.000',39,N'2 Units   ',[Result]) VALUES (254803,[Result]) VALUES (254815,[Result]) VALUES (254833,[Result]) VALUES (254900,[Result]) VALUES (254904,[Result]) VALUES (254905,[Result]) VALUES (254927,[Result]) VALUES (254964,[Result]) VALUES (254986,[Result]) VALUES (254987,[Result]) VALUES (254992,0)

解决方法

我猜想tid确实是主键,代表了您关心的顺序。

您可以通过查看某些列相同的行来分配所需的值-我想它们是驱动程序名称,cardgdate,dircid,负载和单位计数。然后,您可以将此视为差距和孤岛问题。检查这些值何时更改并进行累加:

select t.*,sum(case when prev_tid_grp = prev_tid then 0  -- no change in the key columns
                else 1                               -- count the change!
           end) over (order by tid) as workserial
from (select t.*,lag(tid) over (order by tid) as prev_tid,lag(tid) over (partition by rivername,cardgdate,dircid,load,unitcount order by tid) as prev_tid_grp
      from t
     ) t;
,

据我所知,您希望索引计数每次单位计数更改时都增加。

如果是这样,则可以使用lag(),然后使用窗口count()。目前尚不清楚哪一列可用于对行进行排序,所以我假设使用id

select t.*,sum(case when unitcount = lag_unitcount then 0 else 1 end)
        over(partition by drivename order by id) worserial      
from (
    select t.*,lag(unitcount) over(partition by drivename order by id) lag_unitcount
    from mytable t
) t
,

即使您以1个单位来计算旅行,我还是有些困惑,但是如果我理解主要目的,那应该会有所帮助。

drop table if exists test_table;
create table test_table as
select 254802 as id,0.5 as TripCount,'Abdul' as DriverName,'2units' as units union
select 254803 as id,'2units' as units  union
select 254987 as id,1 as TripCount,'1units' as units  union
select 254910 as id,'2units' as units  union
select 254911 as id,'2units' as units  union
select 254912 as id,'1units' as units  ;

select id,TripCount,DriverName,unit_flg,sum (TripCount) over (partition by DriverName,unit_flg order by  id rows unbounded preceding )   cumulative_trips
from (select *,case when units='2units' then 1 else 0 end as unit_flg
from test_table ) a where a.unit_flg>0 group by 1,2,3,4;