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

求 sql语句 这个比较难 也比较急解决方法

2012-03-23 
求 sql语句这个比较难 也比较急SQL codecreate table tb(id int identity(1,1),uid int,--编号u_noint,--

求 sql语句 这个比较难 也比较急

SQL code
create table tb(    id int identity(1,1),    uid int,            --编号    u_no        int,    --项目    u_no_sub    int,    --子项    uname varchar(100)    --名称)insert into tb(uid,u_no,u_no_sub,uname)select 1,1,1,'dell'unionselect 1,1,2,'机箱'union  select 1,2,1,'HP'unionselect 1,3,1,'索尼'unionselect 2,1,1,'神舟'unionselect 2,2,1,'HP'union  select 2,2,2,'机箱'unionselect 2,3,1,'HP'unionselect 3,1,1,'宏基'unionselect 3,2,1,'HP'union  select 3,3,1,'联想'unionselect 3,3,2,'鼠标'unionselect 4,1,1,'宏基'unionselect 4,2,1,'HP'union  select 4,3,1,'联想'unionselect 4,3,2,'鼠标'unionselect 4,3,3,'键盘'unionselect 5,1,1,'宏基'unionselect 5,2,1,'长城'union  select 5,3,1,'HP'unionselect 5,3,2,'机箱'


--想要的结果

--提示 结果1结果2 当时没有id 为12以后的数据
--去除重复 重复只考虑uname
--结果1 及 只要有一个子项的
121HP
131索尼
211神舟
231HP
311宏基
321HP  

--结果2 根据结果1去除名称一样的 只保留一个
HP
索尼
神舟
宏基



--结果三
uid为5 u_no为3的 与 uid为2 u_no为2的一致 去掉重复 
 
6221HP
7222机箱
11331联想
12332鼠标
16432鼠标
17433键盘









[解决办法]
SQL code
--数据表中内容是这样的:id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- -----------1           1           1           1           dell2           1           1           2           机箱3           1           2           1           HP4           1           3           1           索尼5           2           1           1           神舟6           2           2           1           HP7           2           2           2           机箱8           2           3           1           HP9           3           1           1           宏基10          3           2           1           HP11          3           3           1           联想12          3           3           2           鼠标13          4           1           1           宏基14          4           2           1           HP15          4           3           1           联想16          4           3           2           鼠标17          4           3           3           键盘18          5           1           1           宏基19          5           2           1           长城20          5           3           1           HP21          5           3           2           机箱(21 row(s) affected)
[解决办法]
SQL code
--只有一个子项的select distinct t.unamefrom tb t join (select uid,u_no from tb group by uid,u_no having count(*)=1) e    on t.uid = e.uid and t.u_no = e.u_no
[解决办法]
SQL code
--6L是结果二--结果一如下--只有一个子项的select t.*from tb t join (select uid,u_no from tb group by uid,u_no having count(*)=1) e    on t.uid = e.uid and t.u_no = e.u_no
[解决办法]
SQL code
--只要有一个子项的select a.* from tb a right join (select uid,u_no from tb group by uid,u_no having (count(1)=1))b on a.uid=b.uid and a.u_no=b.u_no/*id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- --------3           1           2           1           HP4           1           3           1           索尼5           2           1           1           神舟8           2           3           1           HP9           3           1           1           宏基10          3           2           1           HP13          4           1           1           宏基14          4           2           1           HP18          5           1           1           宏基19          5           2           1           长城*/ 


[解决办法]

SQL code
--结果二select distinct a.uname from tb a right join (select uid,u_no from tb group by uid,u_no having (count(1)=1))b on a.uid=b.uid and a.u_no=b.u_no/*uname------HP长城宏基神舟索尼*/
[解决办法]
SQL code
--结果三我也看不明白,搞不出你给出的结果select * from tb where uid=5 or u_no=3  or uid=2 or u_no=2/*id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- -----------3           1           2           1           HP4           1           3           1           索尼5           2           1           1           神舟6           2           2           1           HP7           2           2           2           机箱8           2           3           1           HP10          3           2           1           HP11          3           3           1           联想12          3           3           2           鼠标14          4           2           1           HP15          4           3           1           联想16          4           3           2           鼠标17          4           3           3           键盘18          5           1           1           宏基19          5           2           1           长城20          5           3           1           HP21          5           3           2           机箱*/
[解决办法]
--子项个数大于1的结果是这样的呀?
SQL code
select a.* from tb a right join (select uid,u_no from tb group by uid,u_no having (count(1)>1))b on a.uid=b.uid and a.u_no=b.u_no/*id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- ---------1           1           1           1           dell2           1           1           2           机箱6           2           2           1           HP7           2           2           2           机箱11          3           3           1           联想12          3           3           2           鼠标15          4           3           1           联想16          4           3           2           鼠标17          4           3           3           键盘20          5           3           1           HP21          5           3           2           机箱*/
[解决办法]
SQL code
;with    maco          as ( select   a.*               from     tb a                        right join ( select uid ,                                            u_no                                     from   tb                                     group by uid ,                                            u_no                                     having ( count(1) > 1 )                                   ) b on a.uid = b.uid                                          and a.u_no = b.u_no             ),        maco1          as ( select   uid ,                        u_no ,                        uname = stuff(( select  ',' + uname                                        from    maco t                                        where   uid = maco.uid                                                and u_no = maco.u_no                                      for                                        xml path('')                                      ), 1, 1, '')               from     maco               group by uid ,                        u_no             ),        maco2          as ( select   a.* ,                        s = case when charindex(',' + a.uname + ',',                                                ',' + ( select                                                              uname                                                        from  maco1                                                        group by uname                                                        having                                                              ( count(1) > 1 )                                                      ) + ',') > 0 then 0                                 else 1                            end               from     tb a                        right join ( select uid ,                                            u_no                                     from   tb                                     group by uid ,                                            u_no                                     having ( count(1) > 1 )                                   ) b on a.uid = b.uid                                          and a.u_no = b.u_no             )    select  a.id ,            a.uid ,            a.u_no ,            a.u_no_sub ,            a.uname    from    maco2 a            left join ( select  uid ,                                u_no                        from    maco2                        group by uid ,                                u_no                        having  ( sum(s) = 0 )                      ) b on a.uid = b.uid                             and a.u_no = b.u_no    where   b.uid is NULL/*id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- -------------2           1           1           2           机箱11          3           3           1           联想12          3           3           2           鼠标15          4           3           1           联想16          4           3           2           鼠标17          4           3           3           键盘*/ 


[解决办法]

SQL code
;with    maco          as ( select   a.*               from     tb a                        right join ( select uid ,                                            u_no                                     from   tb                                     group by uid ,                                            u_no                                     having ( count(1) > 1 )                                   ) b on a.uid = b.uid                                          and a.u_no = b.u_no             )   ,        maco1          as ( select   uid ,                        u_no ,                        uname = stuff(( select  ',' + uname                                        from    maco t                                        where   uid = maco.uid                                                and u_no = maco.u_no                                      for                                        xml path('')                                      ), 1, 1, '')               from     maco               group by uid ,                        u_no             ),        maco2          as ( select   a.* ,                        s = case when charindex(',' + a.uname + ',',                                                ',' + ( select                                                              uname                                                        from  maco1                                                        group by uname                                                        having                                                              ( count(1) > 1 )                                                      ) + ',') > 0 then 0                                 else 1                            end               from     tb a                        right join ( select uid ,                                            u_no                                     from   tb                                     group by uid ,                                            u_no                                     having ( count(1) > 1 )                                   ) b on a.uid = b.uid                                          and a.u_no = b.u_no             )    select  a.id ,            a.uid ,            a.u_no ,            a.u_no_sub ,            a.uname    from    maco2 a            left join ( select  uid ,                                u_no                        from    maco2                        group by uid ,                                u_no                        having  ( sum(s) = 0 )                      ) b on a.uid = b.uid                             and a.u_no = b.u_no    where   b.uid is NULL/*id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- ---------1           1           1           1           dell2           1           1           2           机箱11          3           3           1           联想12          3           3           2           鼠标15          4           3           1           联想16          4           3           2           鼠标17          4           3           3           键盘*/
[解决办法]
SQL code
;with maco as (     select a.* from tb a right join     ( select uid,u_no from tb group by uid ,u_no having ( count(1) > 1 )    ) b on a.uid = b.uid and a.u_no = b.u_no),maco1 as (     select uid ,u_no ,uname =     stuff(( select  ',' + uname from maco t where uid = maco.uid and u_no = maco.u_no    order by uname for xml path('')), 1, 1, '') from maco group by uid ,u_no),maco2 as (     select * from ( select row_number() over ( partition by uname order by uid ) as rid ,*    from maco1) a where rid = 1)select  a.id ,a.uid ,a.u_no ,a.u_no_sub ,a.unamefrom  maco a left join maco2 b on a.uid = b.uidand a.u_no = b.u_no where b.uid is not NULL--重复的保留了一组,前后顺序不影响结果/*id          uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------- ---------1           1           1           1           dell2           1           1           2           机箱6           2           2           1           HP7           2           2           2           机箱11          3           3           1           联想12          3           3           2           鼠标15          4           3           1           联想16          4           3           2           鼠标17          4           3           3           键盘*/ 


[解决办法]

探讨

引用:
如果6、7 和 20、21 重复,是把6、7和20、21都删除了,还是只保留一组?


只保留一组的

[解决办法]
SQL code
--1.select uid,u_no,u_no_sub,unamefrom tb awhere id<13and not exists(select 1 from tb where uid=a.uid and u_no=a.u_no and u_no_sub<>a.u_no_sub)order by uid,u_no/*uid         u_no        u_no_sub    uname----------- ----------- ----------- ----------------------------------------------------------------1           2           1           HP1           3           1           索尼2           1           1           神舟2           3           1           HP3           1           1           宏基3           2           1           HP(6 行受影响)*/--2.select distinct uname from(select uid,u_no,u_no_sub,unamefrom tb awhere id<13and not exists(select 1 from tb where uid=a.uid and u_no=a.u_no and u_no_sub<>a.u_no_sub))t/*uname----------------------------------------------------------------HP宏基神舟索尼(4 行受影响)*/--3.看不懂楼主说的什么 

热点排行