select case rn when 1 then [姓名] else '' end as 姓名,[去过的地方],b.去的时间 from( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from table_name )a join tb2 b on a.单号 = b.单号
--这个!select case rn when 1 then [姓名] else '' end as 姓名,[去过的地方],b.去的时间 from( select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn from table_name )a join tb2 b on a.姓名 = b.姓名
with cte as ( select row_number() over(partition by 姓名 order by getdate()) no,a.姓名,a.去过的地方, b.去的时间 from tb1 a,tb2 b where a.姓名=b.姓名 )select 姓名=(case when no=1 then 姓名 else '' end),去过的地方 from cte
select (case when 去过的地方=(select min(去过的地方) from tb1 a where a.姓名=tb1.姓名) then 姓名 else '' end ) as 姓名, tb1.去过的地方,tb2.去的时间 from tb1,tb2 where tb1.姓名=tb.姓名 order by tb1.姓名
--加上时间 with cte as ( select row_number() over(partition by 姓名 order by getdate()) no,a.姓名,a.去过的地方, b.去的时间 from tb1 a,tb2 b where a.姓名=b.姓名 ) select 姓名=(case when no=1 then 姓名 else '' end),去过的地方,去的时间 from cte
select (case when 去过的地方=(select min(去过的地方) from tb1 a where a.姓名=tb1.姓名) then 姓名 else '' end ) as 姓名, tb1.去过的地方,tb2.去的时间 from tb1,tb2 where tb1.姓名=tb.姓名 order by tb1.姓名
Create table TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int) Insert TB1 select N'曹操',N'北京',1 union all select N'曹操',N'上海',1 union all select N'刘备',N'中山',2 union all select N'刘备',N'上海',2 union all select N'刘备',N'日本',2 union all select N'曹操',N'日本',1Create table TB2([姓名] nvarchar(2),[去的时间] int) Insert TB2 select N'曹操','1980' union all select N'刘备','2006'select (case when 去过的地方=(select min(去过的地方) from tb1 a where a.姓名=tb1.姓名) then tb1.姓名 else '' end ) as 姓名, tb1.去过的地方,tb2.去的时间 from tb1,tb2 where tb1.姓名=tb2.姓名/* 姓名 去过的地方 去的时间 ---- ----- ----------- 曹操 北京 1980 上海 1980 中山 2006 上海 2006 刘备 日本 2006 日本 1980 */
select (case when address=(select min(address) from tb1 a where a.name=tb1.name) then tb1.name else ' ' end ), tb1.address,tb2.time from tb1,tb2 where tb1.name = tb2.name group by tb1.name,tb1.address,tb2.time曹操 北京 1980 日本 1980 上海 1980 刘备 日本 2006 上海 2006 中山 2006
Create table TB2([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int) Insert TB2 select N'曹操',N'北京',1 union all select N'曹操',N'上海',1 union all select N'刘备',N'中山',2 union all select N'刘备',N'上海',2 union all select N'刘备',N'日本',2 union all select N'曹操',N'日本',1Create table TB3([姓名] nvarchar(2),[去的时间] int) Insert TB3 select N'曹操','1980' union all select N'刘备','2006'WITH CTE AS ( SELECT ROW_NUMBER()OVER(PARTITION BY A.姓名 ORDER BY A.去过的地方) AS ROW,A.姓名,A.去过的地方,B.去的时间 FROM TB2 AS A INNER JOIN TB3 AS B ON A.姓名=B.姓名 ) SELECT CASE WHEN ROW=1 THEN 姓名 ELSE '' END AS 姓名,去过的地方,去的时间 FROM CTE ---------------------------- 姓名 去过的地方 去的时间 曹操 北京 1980 日本 1980 上海 1980 刘备 日本 2006 上海 2006 中山 2006
select (case when address=(select min(address) from tb1 a where a.name=tb1.name) then tb1.name else ' ' end ), tb1.address,tb2.time from tb1,tb2 where tb1.name = tb2.name group by tb1.name,tb1.address,tb2.time
SQL2008上 执行OKWITH CTE AS ( SELECT ROW_NUMBER()OVER(PARTITION BY A.姓名 ORDER BY A.去过的地方) AS ROW,A.姓名,A.去过的地方,B.去的时间 FROM TB2 AS A INNER JOIN TB3 AS B ON A.姓名=B.姓名 ) SELECT CASE WHEN ROW=1 THEN 姓名 ELSE '' END AS 姓名,去过的地方,去的时间 FROM CTE
select (case rn when 1 then a.name else '' end ),a.loca,a.time from (select #TB1.[姓名] as 'name',#TB1.[去过的地方] as 'loca',#TB2.[去的时间] as 'time',row_number() over ( partition by #TB1.[姓名] order by #TB1.[去过的地方]) rn from #TB1 ,#TB2 where #TB1.[姓名]=#TB2.[姓名] ) a
CREATE TABLE TB1(姓名 nvarchar(10),去过的地方 nvarchar(10)) insert into tb1 select '曹操','北京' insert into tb1 select '曹操','上海' insert into tb1 select '刘备','中山' insert into tb1 select '刘备','上海' insert into tb1 select '刘备','日本' insert into tb1 select '曹操','日本' create table TB2(姓名 nvarchar(10),去的时间 varchar(10)) insert into tb2 select '曹操','1980' insert into tb2 select '刘备','2006' go ;with cte as( select row_number()over(partition by 姓名 order by 去过的地方)rn,* from tb1 ) select case when b.rn=1 then b.姓名 else '' end 姓名,b.去过的地方,a.去的时间 from tb2 a inner join cte b on a.姓名=b.姓名 order by 3 /* 姓名 去过的地方 去的时间 ---------- ---------- ---------- 曹操 北京 1980 日本 1980 上海 1980 刘备 日本 2006 上海 2006 中山 2006(6 行受影响) */ go drop table tb1,tb2
Create table TB1([姓名] nvarchar(2),[去过的地方] nvarchar(2),[单号] int) Insert TB1 select N'曹操',N'北京',1 union all select N'曹操',N'上海',1 union all select N'刘备',N'中山',2 union all select N'刘备',N'上海',2 union all select N'刘备',N'日本',2 union all select N'曹操',N'日本',1Create table TB2([姓名] nvarchar(2),[去的时间] int) Insert TB2 select N'曹操','1980' union all select N'刘备','2006'select (case when 去过的地方=(select min(去过的地方) from tb1 a where a.姓名=tb1.姓名) then tb1.姓名 else '' end ) as 姓名, tb1.去过的地方,tb2.去的时间 from tb1,tb2 where tb1.姓名=tb2.姓名/* 姓名 去过的地方 去的时间 ---- ----- ----------- 曹操 北京 1980 上海 1980 中山 2006 上海 2006 刘备 日本 2006 日本 1980 */
select case rn when 1 then [姓名] else '' end as 姓名,[去过的地方],b.去的时间
from(
select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn
from table_name
)a join tb2 b on a.单号 = b.单号
--这个!select case rn when 1 then [姓名] else '' end as 姓名,[去过的地方],b.去的时间
from(
select 姓名,去过的地方,单号,row_number() over (partition by 姓名 order by 去过的地方) rn
from table_name
)a join tb2 b on a.姓名 = b.姓名
(
select row_number() over(partition by 姓名 order by getdate()) no,a.姓名,a.去过的地方, b.去的时间
from tb1 a,tb2 b where a.姓名=b.姓名
)select 姓名=(case when no=1 then 姓名 else '' end),去过的地方 from cte
from tb1 a where a.姓名=tb1.姓名)
then 姓名 else '' end ) as 姓名,
tb1.去过的地方,tb2.去的时间 from tb1,tb2
where tb1.姓名=tb.姓名
order by tb1.姓名
--加上时间
with cte as
(
select row_number() over(partition by 姓名 order by getdate()) no,a.姓名,a.去过的地方, b.去的时间
from tb1 a,tb2 b where a.姓名=b.姓名
)
select 姓名=(case when no=1 then 姓名 else '' end),去过的地方,去的时间 from cte
from tb1 a where a.姓名=tb1.姓名)
then 姓名 else '' end ) as 姓名,
tb1.去过的地方,tb2.去的时间 from tb1,tb2
where tb1.姓名=tb.姓名
order by tb1.姓名
Insert TB1
select N'曹操',N'北京',1 union all
select N'曹操',N'上海',1 union all
select N'刘备',N'中山',2 union all
select N'刘备',N'上海',2 union all
select N'刘备',N'日本',2 union all
select N'曹操',N'日本',1Create table TB2([姓名] nvarchar(2),[去的时间] int)
Insert TB2
select N'曹操','1980' union all
select N'刘备','2006'select (case when 去过的地方=(select min(去过的地方)
from tb1 a where a.姓名=tb1.姓名)
then tb1.姓名 else '' end ) as 姓名,
tb1.去过的地方,tb2.去的时间
from tb1,tb2
where tb1.姓名=tb2.姓名/*
姓名 去过的地方 去的时间
---- ----- -----------
曹操 北京 1980
上海 1980
中山 2006
上海 2006
刘备 日本 2006
日本 1980
*/
(case when address=(select min(address)
from tb1 a where a.name=tb1.name)
then tb1.name else ' ' end ), tb1.address,tb2.time from tb1,tb2 where tb1.name = tb2.name group by tb1.name,tb1.address,tb2.time曹操 北京 1980
日本 1980
上海 1980
刘备 日本 2006
上海 2006
中山 2006
上海 1980
日本 1980
刘备 中山 2006
上海 2006
日本 2006
其实你一定要这个显示应该是显示输出在页面上吧?而不是SQL查询出来就是这样吧?
按道理应该是在页面代码里做相应处理,而不是靠SQL强行特殊处理成这样。
Insert TB2
select N'曹操',N'北京',1 union all
select N'曹操',N'上海',1 union all
select N'刘备',N'中山',2 union all
select N'刘备',N'上海',2 union all
select N'刘备',N'日本',2 union all
select N'曹操',N'日本',1Create table TB3([姓名] nvarchar(2),[去的时间] int)
Insert TB3
select N'曹操','1980' union all
select N'刘备','2006'WITH CTE AS (
SELECT ROW_NUMBER()OVER(PARTITION BY A.姓名 ORDER BY A.去过的地方) AS ROW,A.姓名,A.去过的地方,B.去的时间
FROM TB2 AS A INNER JOIN TB3 AS B ON A.姓名=B.姓名
)
SELECT CASE WHEN ROW=1 THEN 姓名 ELSE '' END AS 姓名,去过的地方,去的时间 FROM CTE
----------------------------
姓名 去过的地方 去的时间
曹操 北京 1980
日本 1980
上海 1980
刘备 日本 2006
上海 2006
中山 2006
(case when address=(select min(address)
from tb1 a where a.name=tb1.name)
then tb1.name else ' ' end ), tb1.address,tb2.time from tb1,tb2 where tb1.name = tb2.name group by tb1.name,tb1.address,tb2.time
SQL2008上 执行OKWITH CTE AS (
SELECT ROW_NUMBER()OVER(PARTITION BY A.姓名 ORDER BY A.去过的地方) AS ROW,A.姓名,A.去过的地方,B.去的时间
FROM TB2 AS A INNER JOIN TB3 AS B ON A.姓名=B.姓名
)
SELECT CASE WHEN ROW=1 THEN 姓名 ELSE '' END AS 姓名,去过的地方,去的时间 FROM CTE
如果同一个人在tb2里有两条数据会发生什么事?
例如
数据库TB1
姓名 去过的地方 曹操 北京
曹操 上海
刘备 中山
刘备 上海
刘备 日本
曹操 日本 数据库TB2姓名 去的时间
曹操 1980
刘备 2006
曹操 1990
不要问sql了,就是你本人恐怕也不会知道应该怎么输出了对不对
) a
insert into tb1 select '曹操','北京'
insert into tb1 select '曹操','上海'
insert into tb1 select '刘备','中山'
insert into tb1 select '刘备','上海'
insert into tb1 select '刘备','日本'
insert into tb1 select '曹操','日本'
create table TB2(姓名 nvarchar(10),去的时间 varchar(10))
insert into tb2 select '曹操','1980'
insert into tb2 select '刘备','2006'
go
;with cte as(
select row_number()over(partition by 姓名 order by 去过的地方)rn,* from tb1
)
select case when b.rn=1 then b.姓名 else '' end 姓名,b.去过的地方,a.去的时间 from tb2 a inner join cte b on a.姓名=b.姓名 order by 3
/*
姓名 去过的地方 去的时间
---------- ---------- ----------
曹操 北京 1980
日本 1980
上海 1980
刘备 日本 2006
上海 2006
中山 2006(6 行受影响)
*/
go
drop table tb1,tb2
Insert TB1
select N'曹操',N'北京',1 union all
select N'曹操',N'上海',1 union all
select N'刘备',N'中山',2 union all
select N'刘备',N'上海',2 union all
select N'刘备',N'日本',2 union all
select N'曹操',N'日本',1Create table TB2([姓名] nvarchar(2),[去的时间] int)
Insert TB2
select N'曹操','1980' union all
select N'刘备','2006'select (case when 去过的地方=(select min(去过的地方)
from tb1 a where a.姓名=tb1.姓名)
then tb1.姓名 else '' end ) as 姓名,
tb1.去过的地方,tb2.去的时间
from tb1,tb2
where tb1.姓名=tb2.姓名/*
姓名 去过的地方 去的时间
---- ----- -----------
曹操 北京 1980
上海 1980
中山 2006
上海 2006
刘备 日本 2006
日本 1980
*/