我目前的查询...
SELECT datepart(hour,s.prodtime) ProdHr
, Min(distinct R.FinalProd) Prod
, MIN(r.chargeseq) StartSeq
, MAX(r.chargeseq) EndSeq
, MIN(S.RollRecID) RID
, (MIN(r.locseq) + COUNT(DISTINCT S.RollRecID) - 1) endd
FROM NYS1Reheat R INNER JOIN NYS1SawPieces S ON R.locseq = S.RollRecID
where s.ShiftIdent = '06/03/15154B' and r.Location = 'HISTORY'
GROUP BY datepart(hour,s.prodtime)
order by RID
它返回......
17 S18X70 14 17 542999660 542999663
18 S18X70 18 41 542999700 542999720
19 S18X54.7 40 61 542999920 542999940
20 S18X54.7 62 79 543000140 543000157
21 S18X54.7 80 105 543000320 543000345
22 S18X54.7 106 133 543000580 543000606
23 S18X54.7 134 171 543000860 543000897
1 W16X100 1 5 543001250 543001254
2 W16X100 6 17 543001300 543001311
3 W16X100 18 32 543001420 543001434
4 W16X77 33 42 543001570 543001578
5 W16X77 41 51 543001650 543001659
使用最后两列(9位数字),我需要从第二列中减去第一列并创建另一列/每个行的差异。 有什么帮助吗?
答案 0 :(得分:1)
你可以内联:
SELECT datepart(hour,s.prodtime) ProdHr
, Min(distinct R.FinalProd) Prod
, MIN(r.chargeseq) StartSeq
, MAX(r.chargeseq) EndSeq
, MIN(S.RollRecID) RID
, (MIN(r.locseq) + COUNT(DISTINCT S.RollRecID) - 1) endd
, (MIN(r.locseq) + COUNT(DISTINCT S.RollRecID) - 1) - MIN(S.RollRecID) ColumnName
FROM NYS1Reheat R INNER JOIN NYS1SawPieces S ON R.locseq = S.RollRecID
where s.ShiftIdent = '06/03/15154B' and r.Location = 'HISTORY'
GROUP BY datepart(hour,s.prodtime)
order by RID
或者将这些全部放入子查询中并按名称调用它:
SELECT tbl.*, tbl.endd - tbl.RID as ColumnName
FROM
(
SELECT datepart(hour,s.prodtime) ProdHr
, Min(distinct R.FinalProd) Prod
, MIN(r.chargeseq) StartSeq
, MAX(r.chargeseq) EndSeq
, MIN(S.RollRecID) RID
, (MIN(r.locseq) + COUNT(DISTINCT S.RollRecID) - 1) endd
FROM NYS1Reheat R INNER JOIN NYS1SawPieces S ON R.locseq = S.RollRecID
where s.ShiftIdent = '06/03/15154B' and r.Location = 'HISTORY'
GROUP BY datepart(hour,s.prodtime)
order by RID
) as tbl