考虑我的表
+----+------+--------+----------+ | ID | Name | Salary | Month | +----+------+--------+----------+ | 1 | a | 5000 | Jan | | 2 | b | 5500 | Jan | | 3 | b | 5300 | Feb | | 4 | b | 5300 | Mar | | 5 | b | 5300 | Apr | | 6 | b | 5300 | May | | 7 | b | 5300 | June | | 8 | b | 5300 | July | +----+------+--------+----------+
我需要显示
+----+------+--------+----------+ | ID | Name | Salary | Month | +----+------+--------+----------+ | 1 | a | 5000 | Jan | | 2 | b | 5500 | Jan | | 3 | | 5300 | Feb | | 4 | | 5300 | Mar | | 5 | | 5300 | Apr | | 6 | | 5300 | May | | 7 | | 5300 | June | | 8 | | 5300 | July | +----+------+--------+----------+
可以帮助
答案 0 :(得分:0)
我假设结果按name
列排序(可能然后是工资?),您可以使用分析函数ROW_NUMBER
来挑出第一个,并且只打印出来一出。
你可能想稍微调整一下查询,但这是主要的想法:
SELECT id, CASE rn WHEN 1 THEN name ELSE null END, salary, month
FROM (SELECT id, name, salary, month,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY salary) AS rn
FROM some_table)
ORDER BY name, rn
答案 1 :(得分:0)
请尝试以下MS Sql server查询:
select
a.ID,
case when a.Name=b.Name then NULL else a.Name end Name,
a.Salary,
a.[Month]
from YourTable a left join YourTable b
on a.ID=b.ID+1