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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.