Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Not sure if I worded that correctly so here's an example of what I want to do.
STID	MARKS1 	MARKS2 	MARKS3	REMARK
01	    50		76		44		PROMOTED
02 	    80 		77		69		PROMOTED
03	    null	60		56		FAIL
04	    88		40		56		PROMOTED

This is the result I’m after
STID	MARKS   REMARK
01	    50		
01	    76
01	    44		PROMOTED
02 	    80
02	    77
02	    69		PROMOTED
03	    60
03	    56		FAIL
04	    88
04	    40
04	    56		PROMOTED


What I have tried:

I used this
SQL
SELECT STID, MARKS, REMARK
FROM
(
  SELECT STID, MARKS1, MARKS2, MARKS3, REMARK
  FROM dbo.student
) AS cp
UNPIVOT 
(
  MARKS FOR STUDENTMARKS IN (MARKS1, MARKS2, MARKS3)
  ) AS up

And got this as the result.
STID	MARKS   REMARK
01	    50		PROMOTED
01	    76		PROMOTED
01	    44		PROMOTED
02 	    80		PROMOTED
02	    77		PROMOTED
02	    69		PROMOTED
03	    60		FAIL
03	    56		FAIL
04	    88		PROMOTED
04	    40		PROMOTED
04	    56		PROMOTED
Posted
Updated 17-Jan-21 23:48pm
v2

1 solution

Assuming you only want the remark to show on the last row, try:
SQL
SELECT
    STID,
    MARKS1 As MARKS,
    CASE WHEN MARKS2 Is Null And MARKS3 Is Null THEN REMARK END As REMARK
FROM
    dbo.student
WHERE
    MARKS1 Is Not Null

UNION ALL

SELECT
    STID,
    MARKS2 As MARKS,
    CASE WHEN MARKS3 Is Null THEN REMARK END As REMARK
FROM
    dbo.student
WHERE
    MARKS2 Is Not Null

UNION ALL

SELECT
    STID,
    MARKS3 As MARKS,
    REMARK
FROM
    dbo.student
WHERE
    MARKS3 Is Not Null
;
If you want to preserve the output order:
SQL
WITH cte As
(
    SELECT
        STID,
        1 As SEQ,
        MARKS1 As MARKS,
        CASE WHEN MARKS2 Is Null And MARKS3 Is Null THEN REMARK END As REMARK
    FROM
        dbo.student
    WHERE
        MARKS1 Is Not Null
    
    UNION ALL
    
    SELECT
        STID,
        2 As SEQ,
        MARKS2 As MARKS,
        CASE WHEN MARKS3 Is Null THEN REMARK END As REMARK
    FROM
        dbo.student
    WHERE
        MARKS2 Is Not Null
    
    UNION ALL
    
    SELECT
        STID,
        3 As SEQ,
        MARKS3 As MARKS,
        REMARK
    FROM
        dbo.student
    WHERE
        MARKS3 Is Not Null
)
SELECT
    STID,
    MARKS,
    REMARK
FROM
    cte
ORDER BY
    STID,
    SEQ
;
 
Share this answer
 
v2
Comments
Maciej Los 18-Jan-21 5:58am    
Richard, take a deep look at OP's input data. Marks1, Marks2, Marks3 is never null.
Richard Deeming 18-Jan-21 5:59am    
Take another look at the source data from the question:
STID	MARKS1 	MARKS2 	MARKS3	REMARK
03	    null	60		56		FAIL

:)
Maciej Los 18-Jan-21 6:24am    
Oppsss...

:)
experimentalzee 18-Jan-21 6:36am    
Hi Richard, I tried it out and I'm getting data in an unordered format. STIDs are getting all mixed up.
Richard Deeming 18-Jan-21 6:36am    
Just add an ORDER BY clause.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900