这里很难解释问题:)
我需要获得一个新列,我有大量的行 F.E.“RIT-17000263-T:1”将有一个额外的列“Total Sequences”:'9' “RIT-17000264-T:1”将是“总序列”:'2'
欢迎任何帮助!:
SELECT DISTINCT
CONCAT(R.[Trip No_],'-', CONCAT('T: ', LEFT(R.[Partial trip Line No_],1))) AS 'TRIP',
R.[File] AS 'FILE',
R.[Sequence No_] AS 'SEQUENCE IN PTRIP'
FROM
[Route] AS R
WHERE
(R.[Trip No_] = 'RIT-17000263'
OR R.[Trip No_] = 'RIT-17000264')
GROUP BY
CONCAT(R.[Trip No_], '-', CONCAT('T: ', LEFT(R.[Partial trip Line No_],1))),
R.[File], R.[Sequence No_]
答案 0 :(得分:2)
也许将它包装在CTE中......
with cte as(
SELECT DISTINCT
CONCAT(R.[Trip No_],'-', CONCAT('T: ', LEFT(R.[Partial trip Line No_],1))) AS 'TRIP',
R.[File] AS 'FILE',
R.[Sequence No_] AS 'SEQUENCE IN TRIP'
FROM
[Route] AS R
WHERE
(R.[Trip No_] = 'RIT-17000263'
OR R.[Trip No_] = 'RIT-17000264')
GROUP BY
CONCAT(R.[Trip No_], '-', CONCAT('T: ', LEFT(R.[Partial trip Line No_],1))),
R.[File], R.[Sequence No_])
select
c.TRIP,
c.[FILE],
c.[SEQUENCE IN TRIP],
c2.CT as [Total Sequences]
from
cte c
join (select TRIP, count(TRIP) as CT from cte group by TRIP) c2 on c2.TRIP = c.TRIP