求SQL语句, SQL高手请进!

h2high 2003-12-02 10:07:28
数据结构

vbelv vbeln vbtyp_n stufe
-------------------- -------------------- ------- -----------
0006037412 C 0
0006037412 0085044500 J 1
0085044500 0000103308 X 2
0085044500 0091055720 M 2
0085044500 0000103305 X 2
0085044500 0000103304 X 2
0006037412 0085044547 J 1
0085044547 0000103462 X 2
0085044547 0091055479 M 2
0006037412 0085047259 J 1
0085047259 0000109336 X 2
0085047259 0091058872 M 2
0085044500 0000103309 X 2
0006037412 0085047260 J 1
0085047260 0000109762 X 2
0085047260 0091058848 M 2
... ...

vbeln为父级的ID
vbelv为子级的ID
stufe为关联级别 0顶层,1一层,2二层
vbtyp_n为列表类型 以CJXM顺序排序, 数据中XM都是在第二层,按XM排序

C下面可能有J(一个或多个)
J下面可能有X(一个或多个)
J下面可能有M(一个) 同时需要有X才有M

M对应有X(一个或多个)
M上肯定有J(一个)
X上肯定有J(一个)


最终输出结果:

vbel vbtyp_n stufe
----------------------------------
0006037412 C 0

.0085044500 J 1

..0000103304 X 2
..0000103305 X 2
..0000103308 X 2
..0091055720 M 2

.0085044547 J 1

..0000103462 X 2
..0091055479 M 2

.0085047259 J 1

..0000103309 X 2
..0000109336 X 2
..0091058872 M 2

.0085047260 J 1
..0000109762 X 2
..0091058848 M 2

... ...

不想ASP中用Recorder进行LOOP查询,能用一条或几条SQL语句得到结果吗?
存储过程也可以
...全文
37 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
h2high 2003-12-15
  • 打赏
  • 举报
回复
谢谢大家
h2high 2003-12-15
  • 打赏
  • 举报
回复
To: nghengdizhen(转瞬即逝)

就用你的啦, 谢谢 ^_^
suhuoqiang 2003-12-03
  • 打赏
  • 举报
回复
??
h2high 2003-12-03
  • 打赏
  • 举报
回复
TO: yonghengdizhen(转瞬即逝)

谢谢提醒

举例来说:
第一行的stufe为0,是第一级. 子ID是0006037412
同时,0006037412也作为其他行的父ID,例如,
0006037412 0085044500 J 1
0006037412 0085044547 J 1
.....................
这样,子ID0085044500/0085044547....等等则是第二级
但是0085044500/0085044547....同样可以成为其他行的父ID,例如:
0085044500 0000103308 X 2
0085044500 0091055720 M 2
....................
这样,子ID0000103308/0091055720........则作为第三级显示
最后的结果就是将子ID按照父ID的关系分级显示。

不知道我上面的说明清不清楚
azh9703 2003-12-03
  • 打赏
  • 举报
回复
我觉得思路都是一样的。用pb做过。一般用递归调用。sql语句循环添加节点的问题,如果用树控件,要了解它的属性方法。
<!--#include file="conn.asp"-->

<html>

<head>
<style TYPE="text/css">
/* Outline Style Sheet */
UL UL {display: none;
margin-left: 20pt}
</style>
<link rel="stylesheet" href="font.css">
<title>tree</title>
<script LANGUAGE="JavaScript">
function checkParent(src, dest) {
// Search for a specific parent of the current element
while (src!=null) {
if (src.tagName == dest) return src;
src = src.parentElement;
}
return null;
}
function outline() {
// Expand or collapse if a list item is clicked.
var open = event.srcElement;
// Make sure clicked inside an LI. This test allows rich HTML inside lists.
var el = checkParent(open, "LI");
if (null!=el) {
var pos = 0;
// Search for a nested list
for (var pos=0; pos<el.children.length; pos++) {
if ("UL"==el.children[pos].tagName) break;
}
if (pos==el.children.length) return;
} else return;
el = el.children[pos];
if ("UL"==el.tagName) {
// Expand or Collapse nested list
if (""==el.style.display) {
el.style.display = "block";
el.style.listStyleImage = "url(images/folder1.gif)";
el.parentElement.style.listStyleImage = "url(images/ofolder3.gif)";
}
else
{
el.style.display = "";
el.parentElement.style.listStyleImage = "url(images/folder3.gif)";
}
}
event.cancelBubble = true;
}
document.onclick = outline;
</script>
<base target="right">

</head>
<body bgColor=PaleTurquoise>
<%
set rs1=server.createobject("adodb.recordset")
'sql="select id,att_filename,level1,link from document where parent=0 and type='folder' "
' rs1.open sql,conn,1,3
sql="select bj_name,bj_code from sparepart_code where bj_class=1"
rs1.open sql,conn,1,3
Response.write "<UL>"
do while not rs1.EOF
Response.write "<LI STYLE='list-style-image: url(images/folder3.gif)'><a href='"&rs1("bj_code")&"'>"&rs1("bj_name")&"</a></LI><br>"
Call ShowFolderList(rs1("bj_code"))
rs1.MoveNext
loop
Response.write "</UL>"
rs1.Close
set rs1=nothing
Sub ShowFolderList(parentid)

set rs=server.createobject("adodb.recordset")
'sql="select id,att_filename,level1,link from document where parent="&cstr(parentid)&" and type='folder'"
'rs.open sql,conn,1,3
sql="select bj_name,bj_code from sparepart_code where bj_parentcode='"& cstr(parentid)&"'"

rs.open sql,conn,1,3
if ( not rs.EOF) then
Response.write "<UL>"
do while not rs.EOF
Response.write "<li STYLE='list-style-image: url(images/folder3.gif)'>"
Response.write "<a href='"&rs("bj_code")&"'>"&rs("bj_name")&"</a></LI><br>"
Response.write "</a>"
Call ShowFolderList(rs("bj_code"))
rs.MoveNext
loop
rs.Close
set rs=nothing
Response.write "</UL>"
end if

end sub

%>
网上找的,我修改了一下用着呢。仅供参考
h2high 2003-12-03
  • 打赏
  • 举报
回复
TO yonghengdizhen(转瞬即逝) :

谢谢, 不过现在这个vbel有三列了呢?

XML我不太熟悉, 不知道怎么弄.
monkeys 2003-12-03
  • 打赏
  • 举报
回复
up
yonghengdizhen 2003-12-03
  • 打赏
  • 举报
回复
深度是确定的吗?
yonghengdizhen 2003-12-03
  • 打赏
  • 举报
回复
用for xml查询可能更简单
yonghengdizhen 2003-12-03
  • 打赏
  • 举报
回复
(select vbeln AS column1,null as column2,null column3,
(case vbtyp_n when 'C' then 0
when 'J' then 1
when 'X' then 2
when 'M' then 3
end) AS Sequence
from tablename where stufe=0)
union
(select vbelv,vbeln,null,
(case vbtyp_n when 'C' then 0
when 'J' then 1
when 'X' then 2
when 'M' then 3
end) AS Sequence
from tablename where stufe=1)
union
(select b.vbelv,a.vbelv,a.vbeln,
(case a.vbtyp_n when 'C' then 0
when 'J' then 1
when 'X' then 2
when 'M' then 3
end) AS Sequence
from tablename a join tablename b
on a.stufe=2 and b.stufe=1 and a.vbelv=b.vbeln
)
order by column1,column2,column3,Sequence
yonghengdizhen 2003-12-03
  • 打赏
  • 举报
回复
(select vbeln AS column1,null as column2,null column3,
(case vbtyp_n when 'C' then 0
when 'J' then 1
when 'X' then 2
when 'M' then 3
end) AS Sequence
from tablename where stufe=0)
union
(select vbelv,vbeln,null,
(case vbtyp_n when 'C' then 0
when 'J' then 1
when 'X' then 2
when 'M' then 3
end) AS Sequence
from tablename where stufe=1)
(select b.vbelv,a.vbelv,a.vbeln,
(case a.vbtyp_n when 'C' then 0
when 'J' then 1
when 'X' then 2
when 'M' then 3
end) AS Sequence
from tablename a join tablename b
on a.stufe=2 and b.stufe=1 and a.vbelv=b.vbeln
)
order by column1,column2,column3,Sequence

andy2001p 2003-12-03
  • 打赏
  • 举报
回复
在我的系统里,做树型结构根本不要用到 vbtyp_n 和 stufe 这两个字段,至于SQL查询好象以前看过,具体还不是很清楚!

但我觉得意义不大,你即使得到了结果,到显示树的时候还是得想办法用LOOP来显示!
h2high 2003-12-03
  • 打赏
  • 举报
回复
可是我的怎么循环呢?

再帮我顶一下哦 :)
h2high 2003-12-03
  • 打赏
  • 举报
回复
TO superdullwolf(超级大笨狼):

怎么没有见到
--------------------------------------------------------
wmlf(凌枫) ( ) 信誉:98 2003-11-29 16:28:01 得分:0



大家看看 这个存储过程。。。
... ...
--------------------------------------------------------
先学习一下, 谢谢 superdullwolf(超级大笨狼)
h2high 2003-12-03
  • 打赏
  • 举报
回复
TO superdullwolf(超级大笨狼):

SQL存储过程里面怎么递归呢?
这个网址去不了?
超级大笨狼 2003-12-03
  • 打赏
  • 举报
回复
wmlf(凌枫) ( ) 信誉:98 2003-11-29 16:28:01 得分:0



大家看看 这个存储过程。。。
create proc jk777
@id int
as
declare @re table(id int)

insert into @re select id
from clientxx where upid=@id
while @@rowcount>0
insert into @re select a.id
from clientxx a inner join @re b on a.upid=b.id
where a.id not in(select id from @re)
select a.* from @re b , clientxx a where a.id=b.id



超级大笨狼 2003-12-03
  • 打赏
  • 举报
回复
www.adr.gov.cn/dpwnload/deeptree.rar
阿信的树,里面有递归的精彩算法。
超级大笨狼 2003-12-03
  • 打赏
  • 举报
回复
要在程序里做递归或者在sql做存储过程里面也有递归。
h2high 2003-12-03
  • 打赏
  • 举报
回复
TO yonghengdizhen(转瞬即逝):

深度是两层, 加顶层的话是三层

stufe值是0,1,2

请大家帮忙.

自己最后顶一下!
h2high 2003-12-03
  • 打赏
  • 举报
回复
TO azh9703() :
我知道这样可以实现这样的功能,用递归

但数据多的时候, 一个页里有N个RecordSet Loop时要花很长的时间,
所以才会提出这样的问题来
加载更多回复(5)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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