SQL row/column Conversion

Source: Internet
Author: User
Take three sites and three parameters as an example. 1. When the parameter column is selected, selectzad_time, zad_sid, zad_data1, zad_data2, zad_data3, zad_data4from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] wherezad_sidin (000002,) andzad_time201304162. select * f

Take three sites and three parameters as an example. 1. select zad_time, zad_sid, zad_data1, zad_data2, zad_data3, zad_data4 from [XHAQMMS_CDB] When the parameter column is used. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_sid in (000002, 20130416) and zad_time = '000000' 2. select * f

Take three sites and three parameters as an example.

1. When using parameters as columns

Select zad_time, zad_sid, region, zad_data2, zad_data3, zad_data4 from [region]. [dbo]. [region] where zad_sid in (000002, 20130416) and zad_time = '2016'

2. Use a parameter as a line

Select * from (select zad_time, zad_sid, parameter = 'so2', concentration value = zad_data1 from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_time = '000000' and zad_sid in ('000000', '000000', '000000') union all select zad_time, zad_sid, parameter = 'no2 ', concentration value = zad_data2 from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_time = '20160901' and zad_sid in ('20160901', '20160901', '20160901') union all select zad_time, zad_sid, parameter = 'pm10 ', concentration value = zad_data3 from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_time = '20160301' and zad_sid in ('20160301', '20160301', '20160301') t order by zad_time

When using a site as a column

Select zad_time, parameter, supply and marketing Mall, ssss, dddd from (select zad_time, parameter = 'so2 ', sum (case zad_sid when '000000' then zad_data1 end) as 'supply and marketing mall ', sum (case zad_sid when '000000' then zad_data1 end) as 'sss', sum (case zad_sid when '000000' then zad_data1 end) as 'ddddd' from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_time = '000000' group by zad_time union all select zad_time, parameter = 'no2 ', sum (case zad_sid when '100' then zad_data2 end) as 'supply and marketing mall ', sum (case zad_sid when '100' then zad_data2 end) as 'sss', sum (case zad_sid when '100' then zad_data2 end) as 'ddddd' from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_time = '000000' group by zad_time union all select zad_time, parameter = 'pm10 ', sum (case zad_sid when '100' then zad_data3 end) as 'supply and marketing mall ', sum (case zad_sid when '100' then zad_data3 end) as 'sss', sum (case zad_sid when '100' then zad_data3 end) as 'ddddd' from [XHAQMMS_CDB]. [dbo]. [XHZ_T_DAYREP_AQI_XZ] where zad_time = '000000' group by zad_time) t order by zad_time

, Website space, Hong Kong server, virtual host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.