永发信息网

求助按要求写个sql查询语句

答案:1  悬赏:70  手机版
解决时间 2021-03-28 00:55
  • 提问者网友:寂寞梧桐
  • 2021-03-27 08:53
求助按要求写个sql查询语句
最佳答案
  • 五星知识达人网友:duile
  • 2021-03-27 09:43
sqlserver数据库
首先你这个是动态列,同一个id下有几个人就会出现几个客户列,这个就难了,我假设你同一个id下最多2个人(多的话可以加)。
首先给同一个id编号,建一个视图
create view vw_person as
(
select *,row_number() over(partition by id order by [号码] desc ) as idx from person
)
--选出同一个id下的第一条数据,然后leftjoin第二条,以ID来匹配。需要多列的话不停地left --join就行
select * from 
(select id,idx,[客户] as [客户1],[号码] as [号码1] from  vw_person   where idx=1) a
left join
(select id,idx,[客户] as [客户2],[号码] as [号码2] from  vw_person   where idx=2) b
on a.id=b.id
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯