如何实现每个列数据不同的表?

时间:2011-07-06 08:58:42

标签: sql-server

我想我的例子不清楚。 表是:  

 
id - name   -    work
-------------------------

1 - sina    -     programmer
2 - ali - programmer 3 - jack - graphist 4 - sina - graphist


我想得到以下结果,每个列数据是distinct.Suppose结果表中的列是独立的:


id - name   -    work
------------------------- 1 - sina - graphist
2 - ali - programmer
3 - jack -


坦克

2 个答案:

答案 0 :(得分:0)

你可以这样做:

WITH names_CTE (Name, RowNum)
                AS
                (
                    SELECT DISTINCT Person.Name,
                    ROW_NUMBER() over (ORDER BY Name) RowNum
                    FROM Person
                )
                SELECT * FROM JBAccount_CTE cte

这样每个名字都会有不同的数字。

答案 1 :(得分:0)

hi guys
this solution is my answer :

With CTE1 as  
(select *,ROW_NUMBER() over (partition by [name] order by id) as rn1 from @TAB )
,CTE2 as
(select *,ROW_NUMBER() over (partition by [work] order by id desc) as rn2 from @TAB )
select C1.Id,C1.[name],ISNULL(C2.[work],'') from CTE1 C1 left join CTE2 C2
on C1.rn1=1 and C2.rn2=1 and C1.[name]=C2.[name]
where C1.rn1=1
order by C1.id
---------OUPUT------------
--1 sina graphist
--2 ali programmer
--3 jack