我有这种格式的数据
NAME1 AMOUNT1 Internal
NAME1 AMOUNT1 External
NAME2 AMOUNT2 Internal
NAME2 AMOUNT2 External
但现在我需要使用 SQL-Server
以下面的格式提取数据 NAME1 AMOUNT1 Internal
External
NAME2 AMOUNT2 Internal
External
请建议。
答案 0 :(得分:1)
您可以通过以下方式使用ROW_NUMBER()和CASE来实现它:
示例数据:
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
<强>输出强>
Name Amount Types
NAME1 AMOUNT1 Internal
External
NAME2 AMOUNT2 Internal
External
<强>样本强>
您可以测试它: SQL FIDDLE