當前位置:首頁 » 編程語言 » sql多行數據合並
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql多行數據合並

發布時間: 2022-01-24 18:04:45

1. sql 多行數據合並成一行

group by 前面相同的欄位,後面不同的,用max取。
SELECT m.member_id,
prefix_desc 'Prefix:',
UPPER(first_name) 'First Name:',
UPPER(last_name) 'Last Name:',
UPPER(email) 'Email:',
LOWER(user_id) 'User ID:',
REPLICATE('*',LEN(password)) 'Password:',
('****'+SUBSTRING(license_no,5,4)) 'Drivers Licese Number:',
REPLACE(email_ind,'Y','Accept') 'Email notification:',
REPLACE(return_ind,'Y','Accept') 'HERIZ E-RETURN',
max(CASE WHEN tel_type= 'B' THEN tel_number END + ' '+REPLACE(pref_phone_ind,'P','PREF')) 'Business Phone',
max('('+ p.country_code + ')'+ CASE WHEN tel_type= 'M' THEN tel_number END) 'Mobile Number',
max(CASE WHEN tel_type= 'F' THEN tel_number END) 'Fax'
FROM MEMBER m, license l, phone_fax p
WHERE m.member_id = l.member_id
AND m.member_id = p.member_id
AND m.member_id = 'A000001'
group by
m.member_id,
prefix_desc 'Prefix:',
UPPER(first_name) 'First Name:',
UPPER(last_name) 'Last Name:',
UPPER(email) 'Email:',
LOWER(user_id) 'User ID:',
REPLICATE('*',LEN(password)) 'Password:',
('****'+SUBSTRING(license_no,5,4)) 'Drivers Licese Number:',
REPLACE(email_ind,'Y','Accept') 'Email notification:',
REPLACE(return_ind,'Y','Accept') 'HERIZ E-RETURN'

2. sql語句如何合並相同id下的多行數據在一行 例如:

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;

3. SQL怎麼把多條數據合並成一條數據

把多條數據合並成一條數據的代碼:

select sum(case when wgrp_id='2' then quota end) w2, sum(case when wgrp_id='3' ;then quota end) w3, mm;
from table;
group by mm。

SQL語言,是結構化查詢語言(Structured Query Language)的簡稱。SQL語言是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統;同時也是資料庫腳本文件的擴展名。

SQL語言是高級的非過程化編程語言,允許用戶在高層數據結構上工作。它不要求用戶指定對數據的存放方法,也不需要用戶了解具體的數據存放方式,所以具有完全不同底層結構的不同資料庫系統可以使用相同的結構化查詢語言作為數據輸入與管理的介面。SQL語言語句可以嵌套,這使他具有極大的靈活性和強大的功能。

應用信息:

結構化查詢語言SQL(STRUCTURED QUERY LANGUAGE)是最重要的關系資料庫操作語言,並且它的影響已經超出資料庫領域,得到其他領域的重視和採用,如人工智慧領域的數據檢索,第四代軟體開發工具中嵌入SQL的語言等。

支持標准:

SQL 是1986年10 月由美國國家標准局(ANSI)通過的資料庫語言美國標准,接著,國際標准化組織(ISO)頒布了SQL正式國際標准。1989年4月,ISO提出了具有完整性特徵的SQL89標准,1992年11月又公布了SQL92標准,在此標准中,把資料庫分為三個級別:基本集、標准集和完全集。

4. sql將多行數據根據同一個關鍵值合並

不知道你什麼資料庫,oracle10g以上可以
with t1 as(
select 'A' id, 'XX' name union all
select 'A' ,'YY' union all
select 'A' ,'ZZ' union all
select 'B' ,'XY' union all
select 'B' ,'XZ'
)
select id,replace(wmwm_concat(name),',','')
from t1
group by id

--結果
A XXYYZZ
B XYXZ

5. SQL多行合並為一行,SQL語句如何寫

selectid,max(語文)語文,max(數學)數學,max(英語)英語
fromtab
groupbyid

6. SQL實現多行數據合並為一行

select a,b max(c),max(d)...,max(j) from t group by a,b

7. sql語句能否實現兩行數據合並

select店名,
sum(1月)as1月,
sum(2月)as2月,
sum(3月)as3月,
sum(4月)as4月,
sum(5月)as5月
from(select*fromtable1unionallselect*fromtable2)a
groupby店名

有問題追問

8. SQL 將屬於同一個ID下的多行數據合並到一行

selectid,sum(isnull(data1,0))data1,sum(isnull(data2,0))data2from表1groupbyid

sqlserver寫法,其他資料庫略有不同

9. sql帶條件多條數據合並為一條數據並換行

修正現在的表添加一個唯一的標識
先按條件分組,統計兩個關鍵的內容
數量:大於1的時需要刪除的
max或min的標識: 用於刪除得行標識
delete from 表where id in(
slect id ,count(*) ,max(id) from 表 groupby id
having count(*)>1
)
如果有大於2的重復記錄,需要在寫循環刪除

10. sql查詢時如何合並兩行數據,具體如下。

看樣子像sqlserver,就按sqlserver的寫了

創建測試表,數據:

createtabletest
(requestidint,
logidint,
operatedatevarchar(10),
operatetimevarchar(10),
operatorint);


insertintotestvalues(502,1372,'2018-06-13','16:16:03',155);
insertintotestvalues(502,1446,'2018-06-14','09:00:32',153);
insertintotestvalues(502,1472,'2018-06-14','09:33:07',157);
insertintotestvalues(502,1474,'2018-06-14','09:35:11',151);
insertintotestvalues(502,1657,'2018-06-14','15:17:10',153);
insertintotestvalues(502,1660,'2018-06-14','15:18:17',155);
insertintotestvalues(502,1661,'2018-06-14','15:19:01',153);
insertintotestvalues(502,1662,'2018-06-14','15:19:48',157);
insertintotestvalues(502,1677,'2018-06-14','15:31:34',151);
insertintotestvalues(502,1694,'2018-06-14','16:42:51',12);
insertintotestvalues(502,1709,'2018-06-14','18:08:45',9);
insertintotestvalues(502,1730,'2018-06-15','08:09:14',158);
insertintotestvalues(502,1732,'2018-06-15','08:09:16',157);

執行:

withtas
(selecttest.*,row_number()over()rnfromtest)
selectt1.requestid,t1.logid,cast(t1.operatedate+''+t1.operatetimeasdatetime)begintime,cast(t2.operatedate+''+t2.operatetimeasdatetime)endtime,t1..requestid=t2.requestidandt1.rn=t2.rn+1

查詢結果:

最後時間沒按你那種方式顯示,你那種到時候算操作時間也麻煩,還不如改成時間日期類型了。