首页 » SQL Server » sqlserver取相同列下其余不同列的数据连接

sqlserver取相同列下其余不同列的数据连接

原文 http://blog.csdn.net/SinCsdn_Jun/article/details/77941378

2017-09-12 11:20:51阅读(407)

今天在项目中遇到的这个问题,确实纠结我挺久的。其实这种需求如果用代码来实现的话只需要一个典然后就能实现,然而,有时候就偏偏让你写个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 #TMP3 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 #TMP3 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 #TMP3_3 from #TMP3 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 #TMP3_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 #TMP3
DROP TABLE #TMP4
DROP TABLE #TMP5
DROP TABLE #TMP_F
DROP TABLE #TMP1_1
DROP TABLE #TMP2_2
DROP TABLE #TMP3_3
DROP TABLE #TMP4_4
DROP TABLE #TMP5_5
                                                                                                                                                                    
代码看起来确实非常凌乱,这确实是我本身的原因,但是还是希望对大家有所帮助,也是我自己的一个知识积累,希望下次遇到的时候能够更快的实现

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹