處理某分組列值有重複時,只顯示分組的首行列值

2021-05-22 12:48:52 字數 1058 閱讀 2157

if not object_id('[tb]') is null

drop table [tb]

gocreate table [tb]([chuanming] nvarchar(10),[huoming] nvarchar(10))

insert [tb]

select n'東方',n'乙烯' union all

select n'東方',n'笨' union all

select n'東方',n'甲烷' union all

select n'東方',n'碳' union all

select n'東方',n'石化' union all

select n'海欣',n'乙烯' union all

select n'海欣',n'笨' union all

select n'海欣',n'甲烷' union all

select n'海欣',n'碳' union all

select n'海欣',n'石化'

go--select * from [tb]

-->sql查詢如下:

--1.2000/2005通用

select case when [huoming]=(select top 1 [huoming] from tb where [chuanming]=t.[chuanming])

then [chuanming] else ''

end [chuanming],

[huoming]

from tb t

--2.2005以上用

select case row_number()over(partition by [chuanming] order by getdate()) when 1

then [chuanming] else ''

end [chuanming],

[huoming]

from tb t

chuanming  huoming

東方       乙烯笨甲烷

碳石化海欣       乙烯笨甲烷

碳石化(10 行受影響)

處理某分組列值有重複時,只顯示分組的首行列值

if not object id tb is null drop table tb gocreate table tb chuanming nvarchar 10 huoming nvarchar 10 insert tb select n 東方 n 乙烯 union all select n 東方...

獲取某行某列的值

獲取某行某列的值 迴圈方式 param row 行數 param col 列數 return number function getval row,col tmp arr for i 1 i row i else return tmp arr row col echo getval 6,2 獲取某行...

獲取某行某列的值

獲取某行某列的值 迴圈方式 param row 行數 param col 列數 return number function getval row,col tmp arr for i 1 i row i else return tmp arr row col echo getval 6,2 獲取某行...