SQL查找上次使用汽车和零件的时间以及工作时数

时间:2018-01-05 11:13:17

标签: sql sql-server-2012

大家好我正在尝试创建一个查询,它会让我上次使用汽车,还有一些零件的使用时间我用SELECT MAX(DateRef)尝试它但是它只给我一个数据汽车,我想最后一次使用每辆车和零件的小时。因为我不是数据库开发人员而是系统管理员,这对我目前的知识来说看起来有点困难。我希望有人能帮助我。

SELECT distinct  c.CarID
, DateRef
, StartHours
, a.SerialNumber
, c.CarComponentID
, c.StartHours + c.UsageHours as TTSN
from ComponentUsage c 
inner join Car A ON a.CarID = c.CarID
where c.DateRef <= '2017-12-31' and c.DateRef >= '2017-12-01' 
group by c.CarComponentID
    ,c.CarID
    , a.SerialNumber
    , starthours
    , c.dateref 
    ,c.StartHours + c.UsageHours

CarID   DateRef       StartHours SerialNumber   CarComponentID      TTSN
18  2017-12-01 03:40:00 3135.30    510-0185       71               3136.90
18  2017-12-01 03:40:00 3135.30    510-0185       72               3136.90
18  2017-12-01 03:40:00 3135.30    510-0185       73               3136.90
18  2017-12-01 03:40:00 0.00       510-0185       74               0.00
18  2017-12-01 03:40:00 3135.30    510-0185       75               3136.90
18  2017-12-01 05:56:00 3136.90    510-0185       71               3138.60
18  2017-12-01 05:56:00 3136.90    510-0185       72               3138.60
18  2017-12-01 05:56:00 3136.90    510-0185       73               3138.60
18  2017-12-01 05:56:00 0.00       510-0185       74               0.00
18  2017-12-01 05:56:00 3136.90    510-0185       75               3138.60
2   2017-12-01 06:45:00 6471.80    510-0065        1               6472.70
2   2017-12-01 06:45:00 6267.80    510-0065       15               6268.70
2   2017-12-01 06:45:00 6338.80    510-0065       29               6339.70
2   2017-12-01 06:45:00 1.20       510-0065       43               1.20
2   2017-12-01 06:45:00 6471.50    510-0065       66               6472.40
11  2017-12-01 07:10:00 4736.70    510-0196       10               4737.50
11  2017-12-01 07:10:00 4736.70    510-0196       24               4737.50
11  2017-12-01 07:10:00 4736.70    510-0196       38               4737.50
11  2017-12-01 07:10:00 0.30       510-0196       52               0.30

2 个答案:

答案 0 :(得分:0)

这里的一种方法是,不是直接加入Car表,而是加入一个子查询,我们可以从中识别每辆汽车的最新记录。下面的子查询为每组汽车记录引入一个行号,从1开始排序,从最近的记录开始。然后,我们通过仅为每辆车保留最新的汽车记录来限制连接。

SELECT c.CarID, DateRef, StartHours, a.SerialNumber, c.CarComponentID,
       c.StartHours + c.UsageHours AS TTSN
FROM ComponentUsage c 
INNER JOIN
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY CarID ORDER BY DateRef DESC) rn
    FROM Car
) a
    ON a.CarID = c.CarID AND a.rn = 1
WHERE
    c.DateRef BETWEEN '2017-12-01' AND '2017-12-31';

答案 1 :(得分:0)

  

大家好我正在尝试创建一个查询,它会让我上次使用汽车时还会使用一些部件的时间

如果这是任务,这可能会有所帮助:

create procedure dbo.get_Cars_Last_Usage_And_Component_Time_sp
as

declare @s nvarchar(max) = ''
declare @s1 as nvarchar(max) = ''
declare @s2 as nvarchar(max) = ''

select @s1 = @s1 + '[' + cast( CarComponentID as varchar(10)) + '],'
from 
(
    select distinct CarComponentID
    from ComponentUsage
) a

if len(@s1) > 0
select @s1 = substring(@s1, 1, len(@s1) - 1)


select @s2 = @s2 + 'sum(coalesce([' + cast( CarComponentID as varchar(10)) + '], 0)) [PartNo ' + cast( CarComponentID as varchar(10)) + '],'
from 
(
    select distinct CarComponentID
    from ComponentUsage
) a

if len(@s2) > 0
select @s2 = substring(@s2, 1, len(@s2) - 1)

set @s =
'
select CarID, max(DateRef) [last time a car were used], ' + @s2 + '
from
(
    select *
    from ComponentUsage
) AS SourceTable
pivot
(
    sum(TTSN)
    FOR CarComponentID in(' + @s1 + ')
) AS PivotTable
group by CarID
'

exec sp_executesql @s

会给你这样的输出:

enter image description here