1

I have the data in this format

      NAME1 AMOUNT1 Internal
      NAME1 AMOUNT1 External
      NAME2 AMOUNT2 Internal
      NAME2 AMOUNT2 External

but now i need to extract the data in the below format using SQL-Server

    NAME1 AMOUNT1 Internal
                  External
    NAME2 AMOUNT2 Internal
                  External

Please suggest.

ughai
  • 9,830
  • 3
  • 29
  • 47
susmitha
  • 21
  • 6

1 Answers1

1

You can achieve It by using ROW_NUMBER() and CASE in following:

SAMPLE DATA:

CREATE TABLE #t
(
   Name NVARCHAR(40),
   Amount NVARCHAR(40),
   Types NVARCHAR(40)
)
INSERT INTO #t VALUES
   ('NAME1', 'AMOUNT1', 'Internal'),
   ('NAME1', 'AMOUNT1', 'External'),
   ('NAME2', 'AMOUNT2', 'Internal'),
   ('NAME2', 'AMOUNT2', 'External')

QUERY

SELECT CASE WHEN rn > 1 THEN '' ELSE Name END AS Name,
       CASE WHEN rn > 1 THEN '' ELSE Amount END AS Amount,
       Types
FROM(
    SELECT Name, 
           Amount, 
           Types, 
           ROW_NUMBER() OVER (PARTITION BY Name, Amount ORDER BY Name) rn
    FROM #t
    ) x

OUTPUT

Name    Amount      Types
NAME1   AMOUNT1     Internal
                    External
NAME2   AMOUNT2     Internal
                    External

DEMO

You can test it: SQL FIDDLE