1

Hard to explain problem here :)

I need to get a new column I have the amount of lines F.E. "RIT-17000263-T: 1" would have an extra column "Total Sequences": '9' "RIT-17000264-T: 1" would be "Total Sequences": '2'

Any help is welcome!:

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_]

Screenshot

2
  • Which RDBMS is this for? Please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely. Commented Jan 25, 2017 at 16:28
  • Sorry!, SQL-SERVER Commented Jan 25, 2017 at 16:34

1 Answer 1

2

Perhaps wrap it in a 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
Sign up to request clarification or add additional context in comments.

3 Comments

Approach looks great!, but i'm getting an error on the join? An expression of non-boolean type specified in a context where a condition is expected, near 'c2'
Sorry about that, fixed it @Gregory
Cheers Mate! @Gregory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.