sqlserver取相同列下其餘不同列的資料連線

2021-08-08 02:32:55 字數 4104 閱讀 6141

今天在專案中遇到的這個問題,確實糾結我挺久的。其實這種需求如果用**來實現的話只需要乙個典然後就能實現,然而,有時候就偏偏讓你寫個sql,然後就去執行下就ok了,因此,問題就來了(標題挺亂的,也取得很不好)

其實這裡的思想很簡單,就是分列插入臨時表,然後處理臨時表,最終得到結果

語文能力很差,那就直接上**:

/*******原需求:

--1 匯出所有四級(省、市、區/縣、鎮/街道)、所屬區號、工作時長、能否取貨、能否派貨、所屬分部

--1.1 若「所屬區號、上班時間、下班時間、是否有記錄、所屬分部」存在不同的值,則並列顯示(逗號隔開)

********/

--第一步:取出所需欄位,並去重

select distinct dr_250,dr_251,dr_252,dr_260,dr_234,dr_254,dr_273,dr_274,dr_208

into #tmp from tb_driverdomainnew

--第二步:取單個差異字段,並儲存到臨時表中

select distinct dr_250,dr_251,dr_252,dr_260,dr_208 into #tmp1 from #tmp--選擇有dr_208不相同的記錄

select distinct dr_250,dr_251,dr_252,dr_260,dr_234 into #tmp2 from #tmp--選擇有dr_234不相同的記錄

select distinct dr_250,dr_251,dr_252,dr_260,dr_254 into #t*** from #tmp--選擇有dr_254不相同的記錄

select distinct dr_250,dr_251,dr_252,dr_260,dr_273 into #tmp4 from #tmp--選擇有dr_273不相同的記錄

select distinct dr_250,dr_251,dr_252,dr_260,dr_274 into #tmp5 from #tmp--選擇有dr_274不相同的記錄

--第三步:合併不停記錄,並用逗號隔開

select dr_250,dr_251,dr_252,dr_260,

(select ',' + dr_208 from #tmp1 as a where b.dr_260 = a.dr_260 and b.dr_251 = a.dr_251 and b.dr_250 = a.dr_250 and b.dr_252 = a.dr_252 for xml path('')) as dr_208

into #tmp1_1 from #tmp1 as b

group by dr_250,dr_251,dr_252,dr_260

select dr_250,dr_251,dr_252,dr_260,

(select ',' + dr_234 from #tmp2 as a where b.dr_260 = a.dr_260 and b.dr_251 = a.dr_251 and b.dr_250 = a.dr_250 and b.dr_252 = a.dr_252 for xml path('')) as dr_234

into #tmp2_2 from #tmp2 as b

group by dr_250,dr_251,dr_252,dr_260

select dr_250,dr_251,dr_252,dr_260, --數值型別需要轉換convert(nvarchar(3),dr_254)

(select ',' + convert(nvarchar(3),dr_254) from #t*** as a where b.dr_260 = a.dr_260 and b.dr_251 = a.dr_251 and b.dr_250 = a.dr_250 and b.dr_252 = a.dr_252 for xml path('')) as dr_254

into #t***_3 from #t*** as b

group by dr_250,dr_251,dr_252,dr_260

select dr_250,dr_251,dr_252,dr_260,

(select ',' + dr_273 from #tmp4 as a where b.dr_260 = a.dr_260 and b.dr_251 = a.dr_251 and b.dr_250 = a.dr_250 and b.dr_252 = a.dr_252 for xml path('')) as dr_273

into #tmp4_4 from #tmp4 as b

group by dr_250,dr_251,dr_252,dr_260

select dr_250,dr_251,dr_252,dr_260,

(select ',' + dr_274 from #tmp5 as a where b.dr_260 = a.dr_260 and b.dr_251 = a.dr_251 and b.dr_250 = a.dr_250 and b.dr_252 = a.dr_252 for xml path('')) as dr_274

into #tmp5_5 from #tmp5 as b

group by dr_250,dr_251,dr_252,dr_260

--第四步:連線獲取所有記錄

select t1.dr_250,t1.dr_251,t1.dr_252,t1.dr_260,t1.dr_208,t2.dr_234,t3.dr_254,t4.dr_273,t5.dr_274 into #tmp_f from #tmp1_1 t1

left join #tmp2_2 t2 on t2.dr_260 = t1.dr_260 and t2.dr_251 = t1.dr_251 and t2.dr_250 = t1.dr_250 and t2.dr_252 = t1.dr_252

left join #t***_3 t3 on t3.dr_260 = t1.dr_260 and t3.dr_251 = t1.dr_251 and t3.dr_250 = t1.dr_250 and t3.dr_252 = t1.dr_252

left join #tmp4_4 t4 on t4.dr_260 = t1.dr_260 and t4.dr_251 = t1.dr_251 and t4.dr_250 = t1.dr_250 and t4.dr_252 = t1.dr_252

left join #tmp5_5 t5 on t5.dr_260 = t1.dr_260 and t5.dr_251 = t1.dr_251 and t5.dr_250 = t1.dr_250 and t5.dr_252 = t1.dr_252

--第五步:去掉開頭逗號,得到最終結果

select dr_250,dr_251,dr_252,dr_260

,substring(dr_234, 2, len(dr_234)-1) dr_234

,substring(dr_254, 2, len(dr_254)-1) as dr_254

,substring(dr_273, 2, len(dr_273)-1) as dr_273

,substring(dr_274, 2, len(dr_274)-1) as dr_274

,substring(dr_208, 2, len(dr_208)-1) as dr_208

from #tmp_f

where isnull(dr_260, '')<>''

--第六步:刪臨時表

drop table #tmp

drop table #tmp1

drop table #tmp2

drop table #t***

drop table #tmp4

drop table #tmp5

drop table #tmp_f

drop table #tmp1_1

drop table #tmp2_2

drop table #t***_3

drop table #tmp4_4

drop table #tmp5_5

**看起來確實非常凌亂,這確實是我本身的原因,但是還是希望對大家有所幫助,也是我自己的乙個知識積累,希望下次遇到的時候能夠更快的實現

excel中兩列匹配,取匹配列的後一列資料

剛在處理excel資料時,遇到如下問題 已知a列的英文名和b列的中文名,現有一批c列的英文名,想根據a b列的對應規則,輸出c列對應的中文名。但是c列的順序和a列順序不一致,也不一定所有內容都被包含在a列。舉例 a b c d prvnce name 省名稱 accs nbr latn name 本...

sql 多列資料相同的情況下進行合併。

if not object id t goods is null drop table t goods gocreate table t goods 名稱 nvarchar 10 not null,型號 nvarchar 20 null,生產廠家 nvarchar 20 null,庫存 int nu...

合併同一行內不同列的相同資料

有乙個表 a b c a b b null null d b c y null c a a y r v a null c null null 合併同一行裡的重複資料,合併完後的資料為 a b c null null d b c y a y r v a c 去除重複資料,但是首次出現的順序不變 生成測...