It is actually the execution sequence of SQL Server column-and-column conversion.

Source: Internet
Author: User

Select top 10 RQ, LB, zsz, ltsz from hq_zqgm_scgm order by RQ

 

Industry ConversionCodeYes:

Select RQ, max (Case LB when 1 then zsz end) as zsz1, max (Case LB when 2 then zsz end) as zsz2
, Max (Case LB when 0 then zsz end) as zsz3
, Max (Case LB when 1 then ltsz end) as ltsz1, max (Case LB when 2 then ltsz end) as ltsz2
, Max (Case LB when 0 then ltsz end) as ltsz3
From hq_zqgm_scgm group by RQ order by RQ

Previous Code is hard to understand

Decomposition steps:

Select
Select RQ, (Case LB when 1 then zsz end) as zsz1, (Case LB when 2 then zsz end) as zsz2
, (Case LB when 0 then zsz end) as zsz3
, (Case LB when 1 then ltsz end) as ltsz1, (Case LB when 2 then ltsz end) as ltsz2
, (Case LB when 0 then ltsz end) as ltsz3
From hq_zqgm_scgm

 

Grouping value:

Select RQ, max (Case LB when 1 then zsz end) as zsz1, max (Case LB when 2 then zsz end) as zsz2
, Max (Case LB when 0 then zsz end) as zsz3
, Max (Case LB when 1 then ltsz end) as ltsz1, max (Case LB when 2 then ltsz end) as ltsz2
, Max (Case LB when 0 then ltsz end) as ltsz3
From hq_zqgm_scgm group by RQ order by RQ

 

Related Article

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.