三个表的合并

风一样的大叔 2012-11-19 02:00:33
table1
Id LocationCode
1 A02b
2 A02b

table2
detail_Id Tallydata
1 1
1 2
1 3
1 4
1 5
2 6
2 7
2 8
2 9
2 10
table1和table2中Id与detail_Id关联,

table3
LocationCode CodeDetail
A02b 字段1
A02b 字段2
A02b 字段3
A02b 字段4
A02b 字段5

table1和table3中LocationCode关联

我想得到如下表,怎么弄?
LocationCode 字段1 字段2 字段3 字段4 字段5
A02b 1 2 3 4 5
A02b 6 7 8 9 10
...全文
160 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 4 楼 TravyLee 的回复:
引用 2 楼 qiujialongjjj 的回复:引用 1 楼 DBA_Huangzj 的回复:先3表联合,然后行转列怎么联合啊,我用左连接会出现冗余数据 A02b 字段1 1 A02b 字段1 2 A02b 字段1 3 A02b 字段1 4 A02b 字段1 5 这个设计的不好
毛线 我早说了这设计的有问题 怎么没人看
开启时代 2012-11-19
  • 打赏
  • 举报
回复
表有问题,table2和table3之间没有关系 导致数据混乱。
發糞塗牆 2012-11-19
  • 打赏
  • 举报
回复
cte里面不能用groupby,即也不能用聚合函数
风一样的大叔 2012-11-19
  • 打赏
  • 举报
回复
引用 8 楼 lixzhong 的回复:
with TB as ( select detail_id,a.locationcode,c.codedetail,b.Tallydata from table1 as a inner join table2 as b on a.id=b.detail_id inner join table3 as c on a.locationcode=c.locationcode……
是不是这里不能用max啊?
风一样的大叔 2012-11-19
  • 打赏
  • 举报
回复
引用 9 楼 DBA_Huangzj 的回复:
搞个建表和查数据额语句出来。那么多数据,懒得给你整
风一样的大叔 2012-11-19
  • 打赏
  • 举报
回复
引用 8 楼 lixzhong 的回复:
with TB as ( select detail_id,a.locationcode,c.codedetail,b.Tallydata from table1 as a inner join table2 as b on a.id=b.detail_id inner join table3 as c on a.locationcode=c.locationcode……
变成这样了 1 A02b 6 6 6 6 6 6 2 A02b 6 6 6 6 6 6
發糞塗牆 2012-11-19
  • 打赏
  • 举报
回复
搞个建表和查数据额语句出来。那么多数据,懒得给你整
开启时代 2012-11-19
  • 打赏
  • 举报
回复
with TB as ( select detail_id,a.locationcode,c.codedetail,b.Tallydata from table1 as a inner join table2 as b on a.id=b.detail_id inner join table3 as c on a.locationcode=c.locationcode) select * from TB pivot(max(Tallydata) for codedetail in ([字段1],[字段2],[字段3],[字段4],[字段5])) as X 加个字段就好了
风一样的大叔 2012-11-19
  • 打赏
  • 举报
回复
引用 3 楼 lixzhong 的回复:
with TB as ( select a.locationcode,c.codedetail,b.Tallydata from table1 as a inner join table2 as b on a.id=b.detail_id inner join table3 as c on a.locationcode=c.locationcode) selec……
只出来了最大的一条,还有一条米出来 A02b 1 2 3 4 5 米出来
Alessandro_ 2012-11-19
  • 打赏
  • 举报
回复
發糞塗牆 2012-11-19
  • 打赏
  • 举报
回复
引用 2 楼 qiujialongjjj 的回复:
引用 1 楼 DBA_Huangzj 的回复: 先3表联合,然后行转列怎么联合啊,我用左连接会出现冗余数据 A02b 字段1 1 A02b 字段1 2 A02b 字段1 3 A02b 字段1 4 A02b 字段1 5
那是你连接的问题,冗余的话一般要么就是没用全部用上主键,导致无法唯一识别每一行,要么就是数据问题。
  • 打赏
  • 举报
回复
引用 2 楼 qiujialongjjj 的回复:
引用 1 楼 DBA_Huangzj 的回复:先3表联合,然后行转列怎么联合啊,我用左连接会出现冗余数据 A02b 字段1 1 A02b 字段1 2 A02b 字段1 3 A02b 字段1 4 A02b 字段1 5
这个设计的不好
开启时代 2012-11-19
  • 打赏
  • 举报
回复
with TB as ( select a.locationcode,c.codedetail,b.Tallydata from table1 as a inner join table2 as b on a.id=b.detail_id inner join table3 as c on a.locationcode=c.locationcode) select * from TB pivot(max(Tallydata) for codedetail in ([字段1],[字段2],[字段3],[字段4],[字段5])) as X
风一样的大叔 2012-11-19
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
先3表联合,然后行转列
怎么联合啊,我用左连接会出现冗余数据 A02b 字段1 1 A02b 字段1 2 A02b 字段1 3 A02b 字段1 4 A02b 字段1 5
發糞塗牆 2012-11-19
  • 打赏
  • 举报
回复
先3表联合,然后行转列

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧