Select data from the right

  • Hello can some one help me select data from the following:

    I have data:
    1001
    1001
    1002
    1002
    1003
    1003
    1004
    1004
    1005
    1005
    1006
    1006
    1007
    1007
    1008
    1008
    1009
    1009
    1010
    1010
    1011
    1011
    1012
    1012
    1013
    1013
    1014
    1014
    1015
    1015
    1016
    1016
    1017
    1017
    1018
    1018
    1019
    1019
    1020
    1020
    1021
    1021
    1022
    I need to have the results in another column
    if column1 = 1001 then column 2 =ydt_1
    if column1 = 1002 then column 2 =ydt_2
    if column1 = 1012 then column 2 =ydt_12
    if column1 = 1025 then column 2 =ydt_25
    how can I do it thank you

  • ;WITH myTable AS
    (
      SELECT * FROM (VALUES(1001), (1001), (1002), (1002), (1003), (1003), (1004), (1004), (1005), (1005),
        (1006), (1006), (1007), (1007), (1008), (1008), (1009), (1009), (1010), (1010), (1011), (1011), (1012), (1012),
        (1013), (1013), (1014), (1014), (1015), (1015), (1016), (1016), (1017), (1017), (1018), (1018), (1019), (1019),
        (1020), (1020), (1021), (1021), (1022)) T(column1)
    )
    SELECT t.column1,
            CASE t.column1
                WHEN 1001 THEN 'ytd_1'
                WHEN 1002 THEN 'ytd_2'
                WHEN 1012 THEN 'ytd_12'
                WHEN 1025 THEN 'ytd_25'
                ELSE ''
            END [column 2]
       FROM myTable t

  • Did you try using a CASE statement?

  • Instead of using a CASE expression:

    column2 = concat('ytd_', t.column1 - 1000)

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply