PostgreSQL row to column after adding to a whole data

Source: Internet
Author: User
Tags postgresql

The basic idea here is to use Max, because the rest of the columns are null, so Max can finally get the value of this line.

Common query:

SELECT ICD,  CaseWhen (Row_number () + (PARTITION by Inner_cd ORDER by Slide_seq))=1Then Slide_qty END asSlide_qty1, CaseWhen  (Row_number () over (PARTITION by Inner_cd ORDER by Slide_seq))=1Then Slide_s_unit_price END asSlide_s_unit_price1, CaseWhen  (Row_number () over (PARTITION by Inner_cd ORDER by Slide_seq))=2Then Slide_qty END asSlide_qty2, CaseWhen  (Row_number () over (PARTITION by Inner_cd ORDER by Slide_seq))=3Then Slide_qty END asSlide_qty3from"Temp"."User"Mmwhere Mm.icd='addm01-20160612-1-100002-001'

The data obtained are:

After using Max:

SELECT MAX ( CaseWhen slide_seq =1Then Slide_qty END) asslide_qty1, MAX ( CaseWhen slide_seq =2Then Slide_qty END) asSlide_qty2, MAX ( CaseWhen slide_seq =3Then Slide_qty END) asSlide_qty3, MAX ( CaseWhen slide_seq =4Then Slide_qty END) asSlide_qty4, MAX ( CaseWhen slide_seq =5Then Slide_qty END) asslide_qty5, MAX ( CaseWhen slide_seq =6Then Slide_qty END) asSlide_qty6, MAX ( CaseWhen slide_seq =7Then Slide_qty END) asSlide_qty7, MAX ( CaseWhen slide_seq =8Then Slide_qty END) asSlide_qty8, MAX ( CaseWhen slide_seq =9Then Slide_qty END) asSlide_qty9, MAX ( CaseWhen slide_seq =TenThen Slide_qty END) asSlide_qty10fromuserwhere anken_id='addm01-20160612-1-100002-001'

The results obtained:

It can then be combined by a subquery

SELECT * fromm_product INNER JOIN (SELECT anken_id, ICD, MAX ( CaseWhen slide_seq =1Then Slide_qty END) asslide_qty1, MAX ( CaseWhen slide_seq =2Then Slide_qty END) asSlide_qty2, MAX ( CaseWhen slide_seq =3Then Slide_qty END) asSlide_qty3, MAX ( CaseWhen slide_seq =4Then Slide_qty END) asSlide_qty4, MAX ( CaseWhen slide_seq =5Then Slide_qty END) asslide_qty5, MAX ( CaseWhen slide_seq =6Then Slide_qty END) asSlide_qty6, MAX ( CaseWhen slide_seq =7Then Slide_qty END) asSlide_qty7, MAX ( CaseWhen slide_seq =8Then Slide_qty END) asSlide_qty8, MAX ( CaseWhen slide_seq =9Then Slide_qty END) asSlide_qty9, MAX ( CaseWhen slide_seq =TenThen Slide_qty END) asslide_qty10 from Userwhere user.anken_id='addm01-20160612-1-100002-001'GROUP by user.inner_cd,user.anken_id) T1 on m_product.anken_id=t1.anken_idwheret1.anken_id ='addm01-20160612-1-100002-001'

You can get rich row-to-column data.

PostgreSQL row to column after adding to a whole data

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.