sql查詢中怎麼將兩條內容相同的記錄顯示成一條

時間 2021-10-14 22:23:54

1樓:匿名使用者

sql查詢中兩條內容相同的記錄顯示成一條可以用group by語句或distinct語句來實現。

如,test表中有如下資料:

group by的方法:

select id,name from test group by id,name;

查詢結果:

distinct的方法:

select distinct id,name from test;

查詢結果:

2樓:匿名使用者

select * from sy where (swrq is null)

and fnbm in(select fnbm from sy a where not exists(select 1 from sy b where b.id

這裡id是一個自動增長的主鍵,id最大,小孩越小。你條件裡面沒有給出,但我想應該有一個類似的欄位。

3樓:飛出大氣層

按該內容的欄位進行排序,用group by 欄位名,即可。

4樓:匿名使用者

select fnbm,znhc,max(zncsrq),znxb from sy

where swrq is null

group by fnbm,znhc,znxb

5樓:匿名使用者

select * from table_name a where a.zncsrq=(select min(zncsrq) from tsble_nme b where a.fnbm=b.

fnbm group by b.fnbm);

6樓:匿名使用者

select * from (

select row_number()over(partition by fnbm order by zncsrq) as row,fnbm,znhc,zncsrq,znxb,swrq from sy) as a where a.row=1

7樓:匿名使用者

distinct 去重

sql 如何將一個表中的兩條或多條擁有相同id的記錄合併為一條?

8樓:

這個恐怕要用存貯過程或程式設計實現, 提個思路:

1) 建立一個同欄位結構的新表table22) 按col1排序

3) 迴圈每條記錄, 記錄每個欄位值到臨時變數中, 名為vcol1, vcol2...

if (col1 == 前一條記錄vcol1)累加vcol2, vcol3...(如果是字串則相連)else

將vcol1, vcol2...各欄位插入table2中4)最終table2應該是你想要的結果

9樓:幸運的小李菲刀

一、建立表:

create table stuunion(sid int identity primary key,cid int,

id varchar(500)

)二、新增資料:

insert into stuunion

elect 1,'a' union

select 1,'b' union

select 2,'c' union

select 2,'d' union

select 3,'e' union

select 3,'f' union

select 3,'g'

三、用標量函式查詢:

建立標量函式:

create function b(@cid int)returns varchar(500)

asbegin

declare @s varchar(500)select @s=isnull(@s+'','')+rtrim(id)+',' from stuunion where cid=@cid

return @s

end;

用標量函式查詢:

select cid,dbo.b(cid) as id from stuunion group by cid

用sqlserver的xml:

select cid,id=stuff((select ' '+rtrim(id)+',' from stuunion where st.cid=cid order by id for xml path('')),1,1,'') from stuunion st group by cid

10樓:枯枝淚

你好,如果是查詢出來顯示的話 直接 分組就行了

如果你要是 把上面的資料生成新的資料插入到表中的話...就直接插入操作.

希望能幫到你吧!

11樓:匿名使用者

不好弄,具體資料具體分析

12樓:秋色豔陽

select distinct * into temp_table from table_name

godelete from table_namegoinsert into table_name select * fromgo

13樓:匿名使用者

不清楚你的資料會不會有兩筆同時存在,但不同值的資料

如果只是上面的這種資料可以這樣來實現

select col1,max(col2) as col2,max(col3) as col3,max(col4) as col4 from table group by col1

sql 如何將一個表中的兩條或多條擁有相同id的記錄合併為一條?

14樓:幸運的小李菲刀

一、建立表:

create table stuunion(sid int identity primary key,cid int,

id varchar(500)

)二、新增資料:

insert into stuunion

elect 1,'a' union

select 1,'b' union

select 2,'c' union

select 2,'d' union

select 3,'e' union

select 3,'f' union

select 3,'g'

三、用標量函式查詢:

建立標量函式:

create function b(@cid int)returns varchar(500)

asbegin

declare @s varchar(500)select @s=isnull(@s+'','')+rtrim(id)+',' from stuunion where cid=@cid

return @s

end;

用標量函式查詢:

select cid,dbo.b(cid) as id from stuunion group by cid

用sqlserver的xml:

select cid,id=stuff((select ' '+rtrim(id)+',' from stuunion where st.cid=cid order by id for xml path('')),1,1,'') from stuunion st group by cid

15樓:塔莞彌陶然

恐怕要用存貯

程或程式設計實現,

提思路:

1)建立

同欄位結構

新表table2

2)按col1排序

3)迴圈每條記錄,

記錄每欄位值臨變數

,名vcol1,

vcol2...

if(col1==前

條記錄vcol1)

累加vcol2,

vcol3...(

字串則相連)

else

vcol1,

vcol2...各欄位插入table2

4)終table2應該想要結

sql查詢滿足兩個條件的重複記錄只顯示2條記錄的方法

16樓:匿名使用者

首先,需要符合兩個條件,即where a=b and c=d;

其次,需要合併重複的資料,即group by a ;

最後,只顯示2條記錄,即top 2;

整條sql就是:

select top 2 * from table where a=b and c=d group by a;

上面是a欄位有重複的情況,若多個欄位有重複,則:

select top 2 * from table where a=b and c=d group by a,b,c;

17樓:ぷ親伱メ尐嘴

只以用遊標來做,單純的sql語句無法實現。

sql中怎樣把同一張表中相同欄位的內容合併為一條記錄?並統計數量

18樓:666挺喜歡他

把表名tt換下就可以了購買產品id是字串型別的話用這個select a.客戶id,stuff((select ','+購買產品id from tt b  wherea.客戶id=b.

客戶id  for xml path('') ),1,1,'') gg from tt a  group by a.客戶id

如果購買產品id是整型的話,用這個select a.客戶id,stuff((select ','+cast(購買產品id as nvarchar(max)) from tt b  wherea.客戶id=b.

客戶id  for xml path('') ),1,1,'') gg from tt a  group by a.客戶id

19樓:匿名使用者

--不用那麼麻煩 用這個 把表名tt換下就可以了

--你的購買產品id是字串型別的話用這個

select a.客戶id,stuff((select ','+購買產品id from tt b where

a.客戶id=b.客戶id for xml path('') ),1,1,'') gg from tt a

group by a.客戶id

--如果購買產品id是整型的話,用這個

select a.客戶id,stuff((select ','+cast(購買產品id as nvarchar(max)) from tt b where

a.客戶id=b.客戶id for xml path('') ),1,1,'') gg from tt a

group by a.客戶id

oracle中刪除兩條相同記錄中的一條該怎麼操作

1.不含大欄位 clob等 的 例子 create table test a number,b number 方法一 通過group by rowid,效率低 delete from test t where t.rowid not in select min rowid from test gro...

sql語句查詢,某一記錄上下相鄰的兩條記錄。怎麼寫

select from news a where news id 12345 and not exists select 1 from news where news id 12345 and news id a.id or news id 12345 and not exists select 1...

SQL如何聯合查詢兩張表中不相同的部分

小丁創業 聯合查詢兩張表中不相同的部分的操作方法和步驟如下 1 第一步,在計算機桌面上單擊 management studio 圖示,如下圖所示,然後進入下一步。2 其次,完成上述步驟後,在介面中單擊 新建查詢 選項,如下圖所示,然後進入下一步。3 接著,完成上述步驟後,在此介面的兩個表中繼續輸入用...