当前位置: 代码迷 >> Sql Server >> 查找节点属性值并根据某列排序(内容详见贴内说明),该如何解决
  详细解决方案

查找节点属性值并根据某列排序(内容详见贴内说明),该如何解决

热度:51   发布时间:2016-04-27 17:58:39.0
查找节点属性值并根据某列排序(内容详见贴内说明)
问题解决啦,特别感谢htl258,给大家分享下
原贴1:http://topic.csdn.net/u/20100513/09/a7efd3d6-cd97-43ad-ba95-f564515bb6bd.html
原贴2:http://topic.csdn.net/u/20100513/12/04cb1bfa-044b-4512-8073-f60187f7b90c.html
原贴3:http://topic.csdn.net/u/20100514/15/4004cbca-0472-486b-9d65-110dfbf5bdbc.html?66980
需求是这样的:首先有3张表a,b,c
表a有三个字段
属性id prop_id,节点 cate_node_id,顺序 level_no
表b是一个树,用来找表a中属性节点和表c中属性节点之间的关系的
节点id,父节点id
表c是人和属性及该属性拥有的值
userid 属性id prop_id, 节点id cate_node_id
首先现在表c中找到各个属性的值是a中对应属性值的父级或平级
在找到很多人之后,按照表A中的属性顺序一个一个属性去对比人,直到只剩一个人或者属性对比完,代码如下:
SQL code
;with t as(    select b.* ,lvl=0  from b b,a a    where a.cate_node_id=b.[cate_node_id]            union all    select n.* ,lvl+1 from t m,b n where m.parent_id=n.cate_node_id),t1 as(    select c.[User_Id],c.Prop_id Prop_Id,--t.parent_id,        c.cate_node_id Cate_Node_Id,        t.lvl,cnt=count(1)over(partition by [User_Id])    from t         join c  c            on t.cate_node_id=c.cate_node_id            ),t2 as(    select         t1 .*,abs(t1.Cate_Node_Id-a.cate_node_id) 差值,    --    top 1 with ties [User_Id]        AProp_Id=a.Prop_id,ACate_Node_Id=a.cate_node_id,ALevel_No=a.Level_No    from t1         join a a            on t1.prop_id=a.Prop_id                 where cnt=(select count(1) from a                                 )),t3 as(    select *,px=(select ','+right(10000+lvl,4) from t2 x where [User_Id]=t2.[User_Id] order by ALevel_No,差值 for xml path(''))     from t2),t4 as(    select top 1 with ties [User_id]     from t3    order by px)select distinct * from t4



------解决方案--------------------
不客气
  相关解决方案