請教SQL可以把ID相同的兩行資料合併到一起嗎

2022-03-15 10:51:48 字數 4290 閱讀 5427

1樓:匿名使用者

如果每個id的資料都有兩行 ,可以使用下面的語句 sql server

select identity(int, 1,1) sx , a.id, a.name+','+b.name c into tmpdata from table1 a

inner join table1 b on (a.id=b.id and a.name<>b.name)

select * from tmpdata where sx % 2 <> 0

2樓:匿名使用者

只限定存在id相同的兩行資料:

select a.id,rtrim(a.name)+','+b.name as 組合 from t a left join t b on a.id=b.id

where a.name>b.name

3樓:

oracle :

select id,wm_concat(name) from tablename

group by id

4樓:匿名使用者

insert (select id from table1 while id_table1=id_table) into table2 (id field)

5樓:匿名使用者

可以,你是什麼資料庫?mysql最容易了

怎麼用sql語句將一張表中id相同的行的內容合併在一起

6樓:仁昌居士

工具/材料:management studio。

1、首先在桌面上,

點選「management studio」圖示。

2、之後在該介面中,點選左上角「新建查詢」選項。

3、接著在該介面中,輸入將一張表中id相同的行的內容合併在一起的sql語句「select sum(grade) from test1 group by id」。

4、然後在該介面中,點選左上方「執行」按鈕。

5、最後在該介面中,顯示錶中id相同的行的內容合併在一起。

7樓:匿名使用者

要達到你說的結果, 單靠sql語句難度很大(你的這個相同id的資料行數不確定吧?). 給你個思路供參考: 先迴圈讀出併合並相同id的資料, 然後再寫入一新表中.

8樓:

看你什麼資料庫了額,oracle如下,其他資料庫不好弄select id,wm_concat(text) as textfrom table_name

group by id

9樓:匿名使用者

select distinct b.id, (select ' '+a.text+' ' from table_2 a where a.

id=b.id for xml path('')) as text from table_2 b

效果圖如下

10樓:夕夜問情

for xml path 可以

**如下

create table tt(

id int,

text1 varchar(120)

)insert into tt select 1,'we are chinese' union select 1,' we like our country' union select 1,' thank you' union

select 1,'we are chinese' union select 2,' you ' union select 2,'are' union select 2,'ok'

select t.id,(select text1+' ' from tt where tt

.id=t.id for xml path('') )as text1 from tt as t group by id

不懂可以私信我

sql語句如何合併相同id下的多行資料在一行 例如: 30

11樓:it技術小店

select id,

max(case when rn = 1 then op end ) op_1,

max(case wehn rn = 1 then result end ) result_1,

max(case when rn = 1 then else end ) else_1,

max(case when rn = 2 then op end ) op_2,

max(case wehn rn = 2 then result end ) result_2,

max(case when rn = 2 then else end ) else_2

from (

select a.*,

row_number() over(partition by id order by else) rn

from a)

group by id;

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

12樓:

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

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

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

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

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

13樓:枯枝淚

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

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

希望能幫到你吧!

14樓:匿名使用者

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

15樓:秋色豔陽

select distinct * into temp_table from table_name

godelete from table_namegoinsert into table_name select * fromgo

16樓:匿名使用者

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

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

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

用sql或函式如何將id相同的行合併到一行

17樓:一騎當後

select *

from (select t.id,

lead(t.name, 0, '') over(partition by t.id order by t.id),

lead(t.name, 1, '') over(partition by t.id order by t.id),

lead(t.name, 2, '') over(partition by t.id order by t.id),

row_number() over(partition by t.id order by t.id) as rowc

from tac t)

where rowc = 1;

看看這個是不是你要的~

sql語句查出多行資料,如何將id相同的行併成一行,且不相同的欄位合成一個字串

18樓:遊刃香菸

相同id合併成一行,只需要用聯合查詢就可以了

不相同的欄位合成一個字串只需要連線符+然後 as一個別名就ok了~

19樓:興

我個人建議你把邏輯寫在**裡面

像這種資料庫操作很好資源的,嚴重影響效率

可以先取出一個list

listresultlist = 資料庫返回mapmap = new hashmap();

for(user user : resultlist)elsemap.put(user.getid().tostring(),val);

}//map裡面的東西就是你要的

20樓:獨唱丶年華

select distinct 欄位 from 表名

如何用SQL語句將表中兩個欄位相同的記錄取出

假設表1為table1 表2為table2 select a.col from select column name colfrom user tab columns where table name table1 a,select column name colfrom user tab colu...

SQL如何蔣同一表中的,某兩個欄位值相同的資料中的某欄位相加,並呈現所有且不重複的資料看圖

select ko,ks,sum wkg as total from tbl where zxltext 生產。group by ko,ks sql問題,如何在一個欄位中根據相同的值,把另一個欄位的值相加 1 選擇 檔案 新建 如下圖所示。2 新增一個asp.net空 3 右鍵 根目錄,選擇新增新項...

兩臺蘋果手機共用id號,怎麼把刪的號碼找回?怎麼讓兩個人

前提是兩個蘋果裝置 都開了icloud,在icloud裡開啟了同步通 1.首先開啟iphone上的設定。1 首先兩個公用一個id賬號,導致通訊錄混亂,是沒有辦法恢復的。兩臺蘋果手機共用一個id怎麼取消通訊錄同步 1 首bai先兩個公用一個id賬號,導du 致通訊錄混zhi亂,是沒有辦法恢dao 1 ...