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

2017-09-12 12:20:37来源:CSDN作者:SinCsdn_Jun人点击

`/*******原需求：--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 BGROUP BY DR_250,DR_251,DR_252,DR_260select 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 BGROUP BY DR_250,DR_251,DR_252,DR_260select 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 BGROUP BY DR_250,DR_251,DR_252,DR_260select 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_273INTO #TMP4_4 from #TMP4 as BGROUP BY DR_250,DR_251,DR_252,DR_260select 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 BGROUP 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  T1LEFT 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_252LEFT 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_252LEFT 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_252LEFT 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 #TMPDROP TABLE #TMP1DROP TABLE #TMP2DROP TABLE #TMP3DROP TABLE #TMP4DROP TABLE #TMP5DROP TABLE #TMP_FDROP TABLE #TMP1_1DROP TABLE #TMP2_2DROP TABLE #TMP3_3DROP TABLE #TMP4_4DROP TABLE #TMP5_5`
`                                                                                                                                                                    `
`代码看起来确实非常凌乱，这确实是我本身的原因，但是还是希望对大家有所帮助，也是我自己的一个知识积累，希望下次遇到的时候能够更快的实现`