经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
联合查询(姑且称之为联合查询)的最差解
来源:cnblogs  作者:ismatch  时间:2019/6/28 8:44:59  对本文有异议

1.数据如下

TimePoint PollutantCode StatusName Value
2019-03-16 01:00:00.000 PM10 大气温度 11.096
2019-03-16 01:00:00.000 PM10 大气压力 102.354
2019-03-16 01:00:00.000 PM2.5 大气温度 14.525
2019-03-16 01:00:00.000 PM2.5 大气压力 101.358
2019-03-16 02:00:00.000 PM10 大气温度 10.134
2019-03-16 02:00:00.000 PM10 大气压力 102.312
2019-03-16 02:00:00.000 PM2.5 大气温度 13.883
2019-03-16 02:00:00.000 PM2.5 大气压力 101.3
2019-03-16 03:00:00.000 PM10 大气温度 10.368
2019-03-16 03:00:00.000 PM10 大气压力 102.249
2019-03-16 03:00:00.000 PM2.5 大气温度 14.033
2019-03-16 03:00:00.000 PM2.5 大气压力 101.258

2.要求

  1. 12条数据可以变成3条数据,并且列变成(TimePoint,PM2_5大气温度,PM2_5大气压力,PM10大气温度,PM10大气压力)

3.建表

  1. IF OBJECT_ID('tempdb..#TestTable') IS NOT NULL
  2. DROP TABLE #TestTable;
  3. CREATE TABLE #TestTable
  4. (
  5. Id INT IDENTITY(1,1),
  6. TimePoint DATETIME,
  7. PollutantCode VARCHAR(10),
  8. StatusName NVARCHAR(50),
  9. Value VARCHAR(50)
  10. )
  11. INSERT INTO #TestTable(TimePoint,PollutantCode,StatusName,Value)
  12. SELECT '2019-03-16 01:00:00.000','PM10', '大气温度','11.096'
  13. UNION SELECT '2019-03-16 01:00:00.000' , 'PM10','大气压力','102.354'
  14. UNION SELECT '2019-03-16 01:00:00.000' , 'PM2.5','大气温度','14.525'
  15. UNION SELECT '2019-03-16 01:00:00.000' , 'PM2.5','大气压力','101.358'
  16. UNION SELECT '2019-03-16 02:00:00.000' , 'PM10','大气温度','10.134'
  17. UNION SELECT '2019-03-16 02:00:00.000' , 'PM10','大气压力','102.312'
  18. UNION SELECT '2019-03-16 02:00:00.000' , 'PM2.5','大气温度','13.883'
  19. UNION SELECT '2019-03-16 02:00:00.000' , 'PM2.5','大气压力','101.3'
  20. UNION SELECT '2019-03-16 03:00:00.000' , 'PM10','大气温度','10.368'
  21. UNION SELECT '2019-03-16 03:00:00.000' , 'PM10','大气压力','102.249'
  22. UNION SELECT '2019-03-16 03:00:00.000' , 'PM2.5','大气温度','14.033'
  23. UNION SELECT '2019-03-16 03:00:00.000' , 'PM2.5','大气压力','101.258'

4.Show your the code(最差解)

  1. SELECT a.TimePoint,a.Value PM2_5大气温度,b.Value PM2_5大气压力,d.Value PM10大气温度,c.Value PM10大气压力
  2. FROM
  3. (
  4. SELECT *
  5. FROM #TestTable
  6. WHERE StatusName = '大气温度'
  7. AND PollutantCode = 'PM2.5'
  8. ) a
  9. LEFT JOIN
  10. (
  11. SELECT *
  12. FROM #TestTable
  13. WHERE StatusName = '大气压力'
  14. AND PollutantCode = 'PM2.5'
  15. ) b
  16. ON a.TimePoint = b.TimePoint
  17. LEFT JOIN
  18. (
  19. SELECT *
  20. FROM #TestTable
  21. WHERE StatusName = '大气压力'
  22. AND PollutantCode = 'PM10'
  23. ) c
  24. ON a.TimePoint = c.TimePoint
  25. LEFT JOIN
  26. (
  27. SELECT *
  28. FROM #TestTable
  29. WHERE StatusName = '大气温度'
  30. AND PollutantCode = 'PM10'
  31. ) d
  32. ON a.TimePoint = d.TimePoint

5.这种不知道算不算行转列...应该有更好的解决方案...期待有缘人可以解答...

原文链接:http://www.cnblogs.com/Mysdm/p/11096790.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号