I am working on SQL Server 2008 R2. I have below 2 tables:
CREATE TABLE TRK_REQUEST(
REQUEST_ID int,
APPROVER_ID int CONSTRAINT app_ID_fk FOREIGN KEY REFERENCES TRK_APPROVER(APPROVER_ID),
APPROVER_LEVEL int)
CREATE TABLE TRK_APPROVER(APPROVER_ID int IDENTITY(1,1) PRIMARY KEY,
NAME Varchar(10))
INSERT INTO TRK_APPROVER VALUES('Approver_1'),('Approver_2'),('Approver_3')
INSERT INTO TRK_REQUEST VALUES(1, 1, 0),(1, 2, 1), (1, 3, 2)
What I want is a version of group_concat()
BUT the values should be in separate columns (not comma separated)
I have tried joining the table,
SELECT REQ.REQUEST_ID, APP.NAME FROM TRK_REQUEST REQ
JOIN TRK_APPROVER APP
ON REQ.APPROVER_ID = APP.APPROVER_ID
But it gives me 3 different rows.
The desired output is like this,
| Request_ID | APPROVER_NAME1 | APPROVER_NAME2 | APPROVER_NAME3
+-------------+----------------------+----------------------+--------------------+
| 1 | Approver_1 | Approver_2 | Approver_3
I found some matching examples here on SO, but not what i expected. Requesting your help.