Sql server SQL Server-多事务ID';s-如果包含-仅选择最新版本
为不清楚的标题道歉 我试图用SQL创建一个时间验证报告,但遇到了麻烦 我面临的问题是,有时支持人员会在员工提交时间后进行更改,区分这些更改的唯一方法是通过事务ID。例如,如果John Doe提交了时间卡,则表中的交易ID为1234,但如果对该条目进行了更改,则表中的交易ID为1234-00,然后是1234-01,然后是1234-02,依此类推。我想做的是修改我的SQL查询,以便如果事务ID包含-它只选择最新的条目 请参见下面的示例代码:Sql server SQL Server-多事务ID';s-如果包含-仅选择最新版本,sql-server,tsql,Sql Server,Tsql,为不清楚的标题道歉 我试图用SQL创建一个时间验证报告,但遇到了麻烦 我面临的问题是,有时支持人员会在员工提交时间后进行更改,区分这些更改的唯一方法是通过事务ID。例如,如果John Doe提交了时间卡,则表中的交易ID为1234,但如果对该条目进行了更改,则表中的交易ID为1234-00,然后是1234-01,然后是1234-02,依此类推。我想做的是修改我的SQL查询,以便如果事务ID包含-它只选择最新的条目 请参见下面的示例代码: SELECT jb_id AS JobID,
SELECT
jb_id AS JobID,
MAX(jb_nme) AS JobName,
MAX(emplye_nme_frst) + ' ' + MAX(emplye_nme_lst) AS FullName,
jchstry_dte_effctve AS EffectiveDate,
jchstry_hrs,
pytype_id PayType,
jbcstctgry_id,
jbcstctgry_dscrptn,
jchstry_srce_trnsctn_id
FROM jb
JOIN jbbllngitm
ON jbbllngitm.jb_rn = jb.jb_rn
JOIN jbcstcde
ON jbcstcde.jb_rn = jb.jb_rn
JOIN jchstry
ON jbcstcde.jbcstcde_rn = jchstry.jbcstcde_rn
JOIN pytype
ON pytype.pytype_rn = jchstry.pytype_rn
JOIN jbcstctgry
ON jbcstctgry.jbcstctgry_rn = jchstry.jbcstctgry_rn
JOIN emplye
ON jchstry_rfrnce_rn = emplye.emplye_rn
WHERE jb_id = '1234'
AND jchstry_dte_effctve BETWEEN 'XXXX-04-01' AND 'XXXX-04-04'
GROUP BY jbcstcde_nme,
jb.jb_rn,
jchstry.jchstry_dte_effctve,
jb.jb_id,
pytype.pytype_id,
jbcstctgry.jbcstctgry_id,
jbcstctgry.jbcstctgry_dscrptn,
jchstry.jchstry_hrs,
jchstry.jchstry_srce_trnsctn_id
样本数据集
JobID JobName FullName EffectiveDate Hours PayType Source Transaction ID
1234 XXXX John Doe 4/1/XXXX 0.25 Straight Time 5678
1234 XXXX John Doe 4/1/XXXX 8 Straight Time 5678-01
1234 XXXX John Doe 4/4/XXXX 8 Straight Time 4567
例如,在上表中,因为5678有两个事务ID,所以我只想选择包含-01的事务ID。如果结果具有事务ID 5678-01、5678-02和5678-03,我只希望查询返回带有5678-03的那一行
如果有什么不清楚的地方,请告诉我。这比我预想的要难解释。如果没有唯一的ID列,也没有最后一个条目的时间戳,那么可以内部连接到它自己。以下是您提供的结果。内部联接是到max transactionID的。这里的关键是在联接中包含足够多的列以使其唯一,同时忽略将要编辑的列。不优雅,但两者都没有时间戳或唯一ID列 升降台#温度
若并没有唯一的ID列,并且并没有最后一个条目的时间戳,那个么可以内部连接到它自己。以下是您提供的结果。内部联接是到max transactionID的。这里的关键是在联接中包含足够多的列以使其唯一,同时忽略将要编辑的列。不优雅,但两者都没有时间戳或唯一ID列 升降台#温度
我根据您的要求在SSMS中进行测试。它工作得很好。 PS:我使用temp table#test作为具有给定结构和数据的表。请适当地进行修改。:)
——创建表结构
创建表#测试(
JobID int,
JobName varchar(10),
全名varchar(50),
生效日期,
小时钱,
PayType varchar(10),
源varchar(10),
TransactionID varchar(10)
)
--将数据插入临时表
插入#测试
选择1234,'XXXX','JohnDoe','4/1/1990',0.25','Straight','Time','5678'
联合所有
选择1234,'XXXX','John Doe','4/1/1990',8','Straight','Time','5678-01'
联合所有
选择1234,'XXXX','John Doe','4/4/1990',8','Straight','Time','4567'
联合所有
选择1234,'XXXX','John Doe','4/1/1990',9','Straight','Time','5678-02'
联合所有
选择1234,'XXXX','John Doe','4/6/1990',123,'Straight','Time','5678-03'
联合所有
选择1234,'XXXX','JohnDoe','4/12/1990',23','Straight','Time','5678-04'
插入#测试
选择2345,'XXXX','John Doe','4/12/1990',23,'Straight','Time','2342'
联合所有
选择2345,'XXXX','John Doe','4/12/1990',23','Straight','Time','2342-01'
--下面是这个问题的解决方案。
选择jobid、jobname、fullname、effectivedate、hours、paytype、source、transactionid
从…起
(
选择*,max(a.orderbyid\u in)over(partitionby a.partitionid)作为orderbyid\u out
从…起
(
选择*,
(charindex('-',TransactionID)0然后离开时的情况(TransactionID,charindex('-',TransactionID)-1)
else事务ID
结束)作为分区,
(charindex('-',TransactionID)为0时的大小写右(TransactionID,charindex('-',reverse(TransactionID))-1)
其他'00'
结束)作为中的orderbyid_
从#测试
)作为一个
)作为b
其中(transactionid=partitionid,orderbyid\u out='00')
或
(transactionid=partitionid+'-'+orderbyid\u out)
我会根据您的要求在SSMS中对其进行测试。它工作得很好。
PS:我使用temp table#test作为具有给定结构和数据的表。请适当地进行修改。:)
——创建表结构
创建表#测试(
JobID int,
JobName varchar(10),
全名varchar(50),
生效日期,
小时钱,
PayType varchar(10),
源varchar(10),
TransactionID varchar(10)
)
--将数据插入临时表
插入#测试
选择1234,'XXXX','JohnDoe','4/1/1990',0.25','Straight','Time','5678'
联合所有
选择1234,'XXXX','John Doe','4/1/1990',8','Straight','Time','5678-01'
联合所有
选择1234,'XXXX','John Doe','4/4/1990',8','Straight','Time','4567'
联合所有
选择1234,'XXXX','John Doe','4/1/1990',9','Straight','Time','5678-02'
联合所有
选择1234,'XXXX','John Doe','4/6/1990',123,'Straight','Time','5678-03'
联合所有
选择1234,'XXXX','JohnDoe','4/12/1990',23','Straight','Time','5678-04'
插入#测试
选择2345,'XXXX','John Doe','4/12/1990',23,'Straight','Time','2342'
联合所有
选择2345,'XXXX','John Doe','4/12/1990',23',Str
SELECT
1234 as JobID,'XXXX' as JobName,'John Doe' as FullName,'4/1/2016' as EffectiveDate,0.25 as Hours,'Straight' as PayType,'Time' as Source,'5678' as TransactionID
INTO #temp
UNION ALL
SELECT
1234,'XXXX','John Doe','4/1/2016',8,'Straight','Time','5678-01'
UNION ALL
SELECT
1234,'XXXX','John Doe','4/4/2016',8,'Straight','Time','4567'
SELECT * FROM #temp
SELECT
t.JobID, t.JobName, t.FullName, t.EffectiveDate, t.Hours, t.PayType, t.Source, t.TransactionID
FROM
#temp t
INNER JOIN
(SELECT JobID, JobName, FullName, EffectiveDate, PayType, Source, MAX(TransactionID) TransactionID
FROM #temp
GROUP BY JobID, JobName, FullName, EffectiveDate, PayType, Source) t2 on
t2.JobID = t.JobID and
t2.JobName = t.JobName and
t2.FullName = t.FullName and
t2.EffectiveDate = t.EffectiveDate and
t2.PayType = t.PayType and
t2.Source = t.Source and
t2.TransactionID = t.TransactionID
-- Create table structure
create table #test(
JobID int,
JobName varchar(10),
FullName varchar(50),
EffectiveDate date,
Hours money,
PayType varchar(10),
Source varchar(10),
TransactionID varchar(10)
)
--Insert Data into the temp table
insert #test
select 1234, 'XXXX', 'John Doe', '4/1/1990', 0.25, 'Straight', 'Time', '5678'
union all
select 1234, 'XXXX', 'John Doe', '4/1/1990', 8, 'Straight', 'Time', '5678-01'
union all
select 1234, 'XXXX', 'John Doe', '4/4/1990', 8, 'Straight', 'Time', '4567'
union all
select 1234, 'XXXX', 'John Doe', '4/1/1990', 9, 'Straight', 'Time', '5678-02'
union all
select 1234, 'XXXX', 'John Doe', '4/6/1990', 123, 'Straight', 'Time', '5678-03'
union all
select 1234, 'XXXX', 'John Doe', '4/12/1990', 23, 'Straight', 'Time', '5678-04'
insert #test
select 2345, 'XXXX', 'John Doe', '4/12/1990', 23, 'Straight', 'Time', '2342'
union all
select 2345, 'XXXX', 'John Doe', '4/12/1990', 23, 'Straight', 'Time', '2342-01'
--Below is the solution for this question.
select jobid,jobname,fullname,effectivedate,hours,paytype,source,transactionid
from
(
select *,max(a.orderbyid_in)over(partition by a.partitionid) as orderbyid_out
from
(
select *,
(case when charindex('-',TransactionID) <> 0 then left(TransactionID,charindex('-',TransactionID)-1)
else TransactionID
end) as partitionid,
(case when charindex('-',TransactionID) <> 0 then right(TransactionID,charindex('-',reverse(TransactionID))-1)
else '00'
end) as orderbyid_in
from #test
) as a
) as b
where (transactionid = partitionid and orderbyid_out = '00')
or
(transactionid = partitionid +'-'+orderbyid_out)
create table #test
(
JobID int,
JobName varchar(10),
FullName varchar(50),
EffectiveDate date,
Hours money,
PayType varchar(10),
Source varchar(10),
TransactionID varchar(10),
jchstry_id int,
jchstry_pstd_tme datetime
)
delete #test
--Insert Data into the temp table
insert #test
select 1234, 'XXXX', 'John Doe', '4/1/2016', 0.25, 'Straight', 'Time', '5678' , 1, '4/1/2016 01:00:00'
union all
select 1234, 'XXXX', 'John Doe', '4/1/2016', 8, 'Straight', 'Time', '5678-01' , 2, '4/1/2016 02:00:00'
union all
select 1234, 'XXXX', 'John Doe', '4/4/2016', 8, 'Straight', 'Time', '4567' , 1 , '4/4/2016 03:00:00'
union all
select 1234, 'XXXX', 'John Doe', '4/1/2016', 9, 'Straight', 'Time', '5678-02' , 3 , '4/1/2016 03:00:00'
union all
select 1234, 'XXXX', 'John Doe', '4/6/2016', 123, 'Straight', 'Time', '5678-03' , 4, '4/6/2016 04:00:00'
union all
select 1234, 'XXXX', 'John Doe', '4/12/2016', 23, 'Straight', 'Time', '5678-04' , 5, '4/12/2016 05:00:00'
union all
select 2345, 'XXXX', 'Mike Smith', '4/12/2016', 23, 'Straight', 'Time', '2342' , 1 , '4/12/2016 03:00:00'
union all
select 2345, 'XXXX', 'Mike Smith', '4/12/2016', 23, 'Straight', 'Time', '2342-01' , 2 , '4/12/2016 04:00:00'
Select * From
(
select *,
Left(TransactionID+'-', CHARINDEX('-',TransactionID+'-') ) Lft,
Rank() Over ( Partition by FullName, Left(TransactionID+'-', CHARINDEX('-',TransactionID+'-') ) Order By jchstry_id desc , jchstry_pstd_tme desc) as Rnk
from #test
) A Where Rnk =1
With TimeVerifcation as (
select
jb_id as JobID,
max(jb_nme) as JobName,
emplye_id as EmployeeID,
max(emplye_nme_frst) + ' ' + max(emplye_nme_lst) as FullName,
jbbllngitm_dscrptn as JobBillingItem,
jchstry_dte_effctve as EffectiveDate,
jchstry_hrs as Hours,
max(pytype_id) PayType,
jbcstctgry_id as CostCategoryID,
jbcstctgry_dscrptn as CostCategoryDescription,
row_number() over (partition by jbbllngitm_dscrptn,jchstry_dte_effctve,emplye_id,pytype_id order by jchstry_pstd_dte + ' ' + jchstry_pstd_tme desc) as RN
from jb
join jbcstcde on jbcstcde.jb_rn = jb.jb_rn
join jchstry on jbcstcde.jbcstcde_rn = jchstry.jbcstcde_rn
join pytype on pytype.pytype_rn = jchstry.pytype_rn
join jbcstctgry on jbcstctgry.jbcstctgry_rn = jchstry.jbcstctgry_rn
join emplye on jchstry_rfrnce_rn = emplye.emplye_rn
join jbbllngitm on jbbllngitm.jbbllngitm_rn = jchstry.jbbllngitm_rn
where jb_id = '72681-00'
and emplye_nme_lst = 'Cortez'
and jchstry_dte_effctve between '2016-04-01' and '2016-04-04'
group by jchstry.jchstry_dte_effctve,jb.jb_id,jbcstctgry.jbcstctgry_id,jbcstctgry.jbcstctgry_dscrptn,jchstry.jchstry_hrs,jbbllngitm.jbbllngitm_dscrptn,emplye.emplye_id,pytype.pytype_id,jchstry.jchstry_pstd_dte,jchstry.jchstry_pstd_tme
)
Select *
from TimeVerifcation
where RN=1
order by EffectiveDate