-1

i have multiple rows and want to make dynamic column i tried below code with case it work fine but i need the column to be dynamic based on the count of max Code

EventId Code Date
1 A12 12/12/2020
1 A13 12/12/2020
2 A12 12/12/2020
2 A13 12/12/2020
3 A13 12/12/2020

i want it like below table

EventId Code1 Date1 Code2 Date2
1 A12 12/12/2020 A13 12/12/2020
2 A12 12/12/2020 A13 12/11/2020
3 A13 12/12/2021
 
WITH Ranked AS (
    SELECT
        EventID,
        Date,
        Code 
        rn = ROW_NUMBER() OVER (PARTITION BY  EventID ORDER BY Date)
    FROM TableEvent
)
SELECT
    EventID,
    MAX(CASE WHEN rn = 1 THEN Date END) AS Date1,
    MAX(CASE WHEN rn = 1 THEN Code END) AS Code1, 
    MAX(CASE WHEN rn = 2 THEN Date END) AS Date2,
    MAX(CASE WHEN rn = 2 THEN Code END) AS Code2, 
FROM Ranked
GROUP BY EventID;
2
  • Hi - for Event 3 - A13 is code2, not code1 - what is the business rule that determines this? Commented Jul 24, 2024 at 11:26
  • @Mr.Brownstone i have updated the question. Commented Jul 24, 2024 at 11:55

2 Answers 2

0

The standard way of doing pivot tables - is to do them in the client, outside of the SQL.

If you really want to do inside SQL Server (cannot imagine real need for it), you can use pivoting functionality (read here). But you would still need to know beforehand a number of columns in the final result.

As a variant, you can do it in a batch or stored procedure (not a single select). In it, you will first find how many columns do you need. Construct a string with a select statement, with needed number of columns (based on pivot). And finally, run this constructed statement as dynamic sql.

But it really is too much hustle with too little return. Much easier to do it on a client side.

0

If you only need 2 codes in a row

you can use a GAPS And ISLAND approach

Where you add another column for the GROUP BY, so that all pair will be grouped to gether.

this is achieved by using the modulo of 2 on the row_number, so that it will pair to gether, which is achieved i the second CTE.

CREATE TABLE TableEvent (
  EventId INTEGER,
  Code VARCHAR(3),
  Date date
);

INSERT INTO TableEvent
  (EventId, Code, Date)
VALUES
  ('1', 'A12', '12/12/2020'),
  ('1', 'A13', '12/12/2020'),
    ('1', 'A14', '12/12/2020'),
  ('1', 'A15', '12/13/2020'),
  ('2', 'A12', '12/12/2020'),
  ('2', 'A13', '12/12/2020'),
  ('3', 'A13', '12/12/2020');
WITH Ranked AS (
    SELECT
        EventID,
        Date,
        Code ,
        rn = ROW_NUMBER() OVER (PARTITION BY  EventID ORDER BY Date),
  CASE WHEN ROW_NUMBER() OVER (PARTITION BY  EventID ORDER BY Date) % 2 = 1 THEN 1 ELSE 0 END sum_
    FROM TableEvent
),
  CTE AS (
SELECT
          EventID,
        Date,
        Code ,
  rn,
  SUM(sum_) OVER(PARTITION BY EventID ORDER BY rn) sum_
  FROM ranked
  )
SELECT
    EventID,
    MAX(CASE WHEN rn % 2  = 1 THEN Date END) AS Date1,
    MAX(CASE WHEN rn % 2 = 1 THEN Code END) AS Code1, 
    MAX(CASE WHEN rn% 2  = 0 THEN Date END) AS Date2,
    MAX(CASE WHEN rn % 2  = 0 THEN Code END) AS Code2 
FROM CTE
GROUP BY EventID, sum_
 ORDER BY EventID,MAX(rn) ;
EventID Date1 Code1 Date2 Code2
1 2020-12-12 A12 2020-12-12 A13
1 2020-12-12 A14 2020-12-13 A15
2 2020-12-12 A12 2020-12-12 A13
3 2020-12-12 A13 null null
Warning: Null value is eliminated by an aggregate or other SET operation.

fiddle

1
  • the number of codes varies from user to user it can reach upto 30, I would like to identify the maximum row count and then create the necessary columns. Commented Jul 28, 2024 at 6:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.