+ -
当前位置:首页 → 问答吧 → 求一条SQL语句

求一条SQL语句

时间:2011-12-12

来源:互联网

SQL code

--原数据
col1 col2  col3 col4  col5 col6 .......
AAA   BBB  CCC    1     0    1
col1 col2  col3 col4  col5 col6 .......
AAA   BBB  CCC    3     7    9
col1 col2  col3 col4  col5 col6 .......
AAA   EEE  CCC    5     3    2
col1 col2  col3 col4  col5 col6 .......
AAA   BBB  DDD    5     3    2



SQL code

--想要的结果
col1 col2  col3 col4  col5 col6 .......
AAA   BBB  CCC    1     0    1

                  3     7    9

AAA   EEE  CCC    5     3    2

AAA   BBB  DDD    5     3    2


要的结果就是前面三个字段值相同的只显示一行,后面字段的值也不合并就在原行里显示!


作者: custom1234   发布时间: 2011-12-12

SQL code
select col1=(case when no=1 then col1 else '' end),
       col2=(case when no=1 then col1 else '' end),
       col3=(case when no=1 then col1 else '' end),col4.....
from (select no=row_number() over(partition by col1,col2,col3 order by getdate()),* from tb) t

作者: ssp2009   发布时间: 2011-12-12

SQL code
select
   case px when 1 then col1 else '' end as col1,
   case px when 1 then col2 else '' end as col2,
   case px when 1 then col3 else '' end as col3,
   *
from
   (select px=row_number()over(partition by col1,col2,col3 order by getdate()),* from tb)t

作者: fredrickhu   发布时间: 2011-12-12

SQL code

--想要的结果
ID  col1 col2  col3 col4  col5 col6 .......
 1   AAA   BBB  CCC    1     0    1

                  3     7    9

  2  AAA   EEE  CCC    5     3    2

  3  AAA   BBB  DDD    5     3    2


在记录前加一个序列号呢?


作者: custom1234   发布时间: 2011-12-12

SQL code
select px,col1=(case when no=1 then col1 else '' end),
       col2=(case when no=1 then col1 else '' end),
       col3=(case when no=1 then col1 else '' end),col4.....
from (select no=row_number() over(partition by col1,col2,col3 order by getdate(),
  px=rank() over(order by col1,col2,col3),* from tb) t

作者: ssp2009   发布时间: 2011-12-12

SQL code
select
   case px when 1 then rankid else '' end as id,
   case px when 1 then col1 else '' end as col1,
   case px when 1 then col2 else '' end as col2,
   case px when 1 then col3 else '' end as col3,
   *
from
   (select
     rankid=DENSE_RANK()over(partition by col1,col2,col3 order by getdate()),
     px=row_number()over(partition by col1,col2,col3 order by getdate()),
    * 
     from tb)t

作者: fredrickhu   发布时间: 2011-12-12

引用 4 楼 ssp2009 的回复:

SQL code
select px,col1=(case when no=1 then col1 else '' end),
col2=(case when no=1 then col1 else '' end),
col3=(case when no=1 then col1 else '' end),col4.....
from (select no=row_n……


SQL code

--不好意思,两位!刚才说错了,是一个完整的序号流水号:因为我想用这个ID来排序!

ID  col1   col2  col3 col4  col5 col6 .......
1       AAA   BBB  CCC    1     0    1

2                         3     7    9

3       AAA   EEE  CCC    5     3    2

4       AAA   BBB  DDD    5     3    2


作者: custom1234   发布时间: 2011-12-12

SQL code
select
   id=row_number()over(order by getdate()),
   case px when 1 then rankid else '' end as id,
   case px when 1 then col1 else '' end as col1,
   case px when 1 then col2 else '' end as col2,
   case px when 1 then col3 else '' end as col3,
   *
from
   (select
     px=row_number()over(partition by col1,col2,col3 order by getdate()),* from tb)t

作者: fredrickhu   发布时间: 2011-12-12