sql不顯示重複列

來源:互聯網
上載者:User

在報表裡,基本上都可以把重複的資料不顯示,在SQL裡怎麼才能做到如下情況呢?

a10a20b30b40b50
顯示為:

a1020b304050

SQL 如下:

create table #a (part varchar(10),price int)goinsert into #a values('a',10)insert into #a values('a',20)insert into #a values('b',30)insert into #a values('b',40)insert into #a values('b',50)goselect * from #ago select part ,MIN(price) price into #b from #a group by part goselect * from #a select * from #bgo   select  case when price in (select price from #b) then part else '' end ,price  from #a  go  

參考: http://bbs.csdn.net/topics/310112824

主要內容:

方案一:

if object_id('[tab]') is not null drop table [tab]create table [tab]([單位] varchar(6),[姓名] varchar(4),[學曆] varchar(4))insert [tab]select '一車間','張三','本科' union allselect '一車間','李四','本科' union allselect '一車間','王五','本科' union allselect '二車間','王中','專科' union allselect '二車間','劉一','專科'select 單位,姓名,學曆 from (select 單位,姓名=(select top 1 姓名 from tab where ta.單位=單位 and ta.學曆=學曆),學曆,s1=0,s2=單位,s3=0 from tab ta group by 單位,學曆 union allselect '   '  ,姓名,'',s1=0,s2=單位,s3=1 from  tab tb where  姓名<>(select top 1 姓名 from tab where tb.單位=單位 and tb.學曆=學曆))torder by s1,s2,s3/*單位     姓名   學曆   ------ ---- ---- 二車間    王中   專科       劉一   一車間    張三   本科       李四          王五   (所影響的行數為 5 行)*/

方案二:

select 姓名 into #temp from (select [單位],max(姓名)as 姓名 from [tab]group by [單位])Tselect case when 姓名 in (select * from #temp ) then [單位] else '' end,姓名,case when 姓名 in (select * from #temp ) then [學曆] else '' endfrom [tab]




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.