去掉重覆記錄問題

2021-04-12 15:23:46 字數 3313 閱讀 6959

原記錄為: 

a    b    c

-----------------

1    1     a

1    2     b

1    3     c

2    4     d

1    5     e

2    6     f

2    7     g

3    8     h

要求結果為:

a    b    c

-----------------

1    1     a

2     b

3     c

5     d

2    4     e

6     f

7     g

3    8     h

答案1:

declare @t table(a int,b int,c varchar(20))

insert @t

select 1,    1,     'a' union all

select 1,    2,     'b' union all

select 1,    3,     'c' union all

select 2,    4,     'd' union all

select 1,    5,     'e' union all

select 2,    6,     'f' union all

select 2,    7,     'g' union all

select 3,    8,     'h'

----查詢

select

a = case

when b = (select top 1 b from @t where a = x.a order by b)

then cast(a as varchar(10))

else '' end,b,c

from @t as x

order by a*1,b

---結果

a          b           c                   

---------- ----------- --------------------

1          1           a

2           b

3           c

5           e

2          4           d

6           f

7           g

3          8           h

(所影響的行數為 8 行)

如果order by a,b

sql的優化器會認為按照結果的第乙個字段排序,這樣所有空的都跑到最前面去了

order by a*1,b

就是指明先按a欄位排序,加了運算(*1)表示必須選擇字段

也可以:

declare @t table(a int,b int,c varchar(20))

insert @t

select 1,    1,     'a' union all

select 1,    2,     'b' union all

select 1,    3,     'c' union all

select 2,    4,     'd' union all

select 1,    5,     'e' union all

select 2,    6,     'f' union all

select 2,    7,     'g' union all

select 3,    8,     'h'

----查詢

select

a1 = case

when b = (select top 1 b from @t where a = x.a order by b)

then cast(a as varchar(10))

else '' end,b,c

from @t as x

order by a,b

不過前端也許就不方便了

答案2:

declare @t table(a int,b int,c varchar(20))

insert @t

select 1,    1,     'a' union all

select 1,    2,     'b' union all

select 1,    3,     'c' union all

select 2,    4,     'd' union all

select 1,    5,     'e' union all

select 2,    6,     'f' union all

select 2,    7,     'g' union all

select 3,    8,     'h'

----方法1

select

d = case   /*取個新列名,防止與a同名,以使a能用於排序*/

when b = (select top 1 b from @t where a = x.a order by b)

then cast(a as varchar(10))

else '' end,b,c

from @t as x

order by a,b     /*按照a列原值排序,再按b列原值排序*/

----方法2:使用排序後的子查詢作為查詢的資料來源(效率要低些)

select

a = case

when b = (select top 1 b from @t where a = x.a order by b)

then cast(a as varchar(10))

else '' end,b,c

from (select top 100 percent * from @t order by a,b) as x    /*子查詢表示對源資料進行排序*/

這樣的sql不是迴圈也不是巢狀,只是一種相關查詢,體現在下面這行**中:

when b = (select top 1 b from @t where a = x.a order by b)

即源表中每掃瞄一行都判斷"b = (select top 1 b from @t where a = x.a order by b)"這個條件是否成立,關聯之處在於子查詢中的a = x.a

sql去掉重覆記錄

第一種,資料全部重複,如下圖 需要得到以下的結果 刪除重複的記錄 重覆記錄保留1條 可以按以下方法刪除 1 seleet distinct into tmp from 表名23 drop table 表名45 select into 表名 from tmp67 drop table tmp 第二種,...

SQL去掉重覆記錄

第一種,資料全部重複,如下圖 需要得到以下的結果 刪除重複的記錄 重覆記錄保留1條 可以按以下方法刪除 1 seleet distinct into tmp from 表名23 drop table 表名45 select into 表名 from tmp67 drop table tmp 第二種,...

sql 重覆記錄和重覆記錄數

如果table1有兩個column adress和pepole,那麼下面的sql可以找出table1裡的重覆記錄和重覆記錄數 create table table1 adress nvarchar 10 pepole nvarchar 10 insert table1 select 寧波 張三 nb...