首页 新闻 会员 周边 捐助

SQL列转行问题

0
悬赏园豆:10 [已关闭问题]
<table class="mframe" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="lf" rowspan="2">&nbsp;</td> <td class="rw"> <div class="fbart"><em>楼主</em>发表于:2010-03-26 10:32:53</div> <table class="mtxt" cellspacing="0" cellpadding="0"> <tbody> <tr> <td id="body"><!-- google_ad_section_start --> <div class="msgfont">表A <br />tid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;湖北 &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 <br />&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 002 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 003 <br />&nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 002 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 003 <br />&nbsp; 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 002 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null &nbsp; &nbsp; &nbsp; &nbsp; 003 <br />&nbsp; 5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; na &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 003 &nbsp; <br />.... <br />表B <br />tid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;address &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 状态 <br />&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br />&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br /><br />&nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; na <br />&nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br /><br />&nbsp; 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br />&nbsp; 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br />&nbsp; 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br /><br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br /><br />&nbsp; 5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br />&nbsp; 5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test <br />&nbsp; 5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; na <br /><br /><br />下面要求表C <br /><br />表C: <br />tid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; address &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 状态 <br />&nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; na <br />&nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖南 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 湖北 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null <br />&nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 广东 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; na <br /><br />逻辑:表A和表B中TID是关联字段,现在要求 &nbsp; 表A中列有值(na或者null都是无值的)表B中TID所对应的行中状态列没有值的数据 <br />表A中的列对应表B中的行 &nbsp; 表A中每一列代表表B中一行 <br /><br />求SQL &nbsp; 列转行 &nbsp; 通过A,B表求C表 <br /><br />逻辑有点复杂,有不明白的,我再补充说明。 <br />&nbsp; &nbsp; &nbsp; </div> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table>
火柴人的主页 火柴人 | 初学一级 | 园豆:190
提问于:2010-03-26 15:26
< >
分享
其他回答(2)
0

代码
1 CREATE TABLE #TEST_A (TID CHAR(2),ADDRESS CHAR(10),STATUS CHAR(10))
2  INSERT INTO #TEST_A
3 SELECT TID, '湖北', 湖北 FROM A
4 INSERT INTO #TEST_A
5 SELECT TID, '湖南', 湖南 FROM A
6 INSERT INTO #TEST_A
7 SELECT TID, '广东', 广东 FROM A
8 --如果数据太多,可以使用动态的。
9
10 SELECT B.TID,B.ADDRESS,B.状态
11 FROM B
12 INNER JOIN #TEST_A
13 ON #TEST_A.TID=B.TID
14 AND #TEST_A.ADDRESS = B.ADDRESS
15 AND (#TEST_A.STATUS<>'NA' OR #TEST_A.STATUS<>NULL)
16 WHERE (B.状态=NULL OR B.状态='NA')

 

huayifu | 园豆:249 (菜鸟二级) | 2010-03-26 16:23
15 AND (#TEST_A.STATUS<>'NA' OR #TEST_A.STATUS<>NULL) 16 WHERE (B.状态=NULL OR B.状态='NA') 改为 15 AND (#TEST_A.STATUS<>'NA' OR #TEST_A.STATUS IS NOT NULL) 16 WHERE (B.状态 IS NULL OR B.状态='NA')
支持(0) 反对(0) huayifu | 园豆:249 (菜鸟二级) | 2010-03-26 16:52
0

不一定要用sql呢,hehe

好像sql里面有个行列掉转的语句呢

LittlePeng | 园豆:3445 (老鸟四级) | 2010-03-27 19:08
0

select * from B where 状态 is null or 状态='na'

这样就可以了吧。

清海扬波 | 园豆:825 (小虾三级) | 2010-03-29 23:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册