首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

三个表的合龙

2012-12-21 
三个表的合并table1IdLocationCode1A02b2A02btable2detail_IdTallydata111213141526272829210table1和tabl

三个表的合并
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 
[最优解释]
搞个建表和查数据额语句出来。那么多数据,懒得给你整
[其他解释]
先3表联合,然后行转列
[其他解释]
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

[其他解释]

引用:
引用:先3表联合,然后行转列怎么联合啊,我用左连接会出现冗余数据
A02b    字段11
A02b    字段12
A02b    字段13
A02b    字段14
A02b    字段15


这个设计的不好
[其他解释]
引用:
引用:
先3表联合,然后行转列怎么联合啊,我用左连接会出现冗余数据
A02b    字段1 1
A02b    字段1 2
A02b    字段1 3
A02b    字段1 4
A02b    字段1 5

那是你连接的问题,冗余的话一般要么就是没用全部用上主键,导致无法唯一识别每一行,要么就是数据问题。
[其他解释]
引用:
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
米出来
[其他解释]
表有问题,table2和table3之间没有关系 导致数据混乱。
[其他解释]
引用:
引用:引用:先3表联合,然后行转列怎么联合啊,我用左连接会出现冗余数据
A02b    字段11
A02b    字段12
A02b    字段13
A02b    字段14
A02b    字段15

这个设计的不好


毛线  我早说了这设计的有问题  怎么没人看
[其他解释]
引用:
先3表联合,然后行转列
怎么联合啊,我用左连接会出现冗余数据
A02b    字段11
A02b    字段12
A02b    字段13
A02b    字段14
A02b    字段15

[其他解释]
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
加个字段就好了
[其他解释]
引用:
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……
变成这样了

1A02b666666
2A02b666666
[其他解释]
引用:
搞个建表和查数据额语句出来。那么多数据,懒得给你整

[其他解释]
引用:
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啊?
[其他解释]
cte里面不能用groupby,即也不能用聚合函数
[其他解释]
null

热点排行