MySQL or hive data rows become columns

Source: Internet
Author: User

For mysql/hive to be counted, you can use a function if you want rows to become columns.

Case field A When value B then C [if D then e]* [ELSE F] END

When the field a= value B, return C (if the field is the value of the field, it can be a fixed value plus single quotation marks), when A=d, return E, otherwise return F.


Such as:

Data table structure: (for example, ID has duplicates)


Select Id,sum (case action is ' article ' then count else 0 end) as Article,sum (case action is ' user_comment ' then count else 0 end) as user_comment, sum (case action "profile" then count else 0 end) as ' profile ', sum (case action when ' user _follow ' then count else 0 end] as User_follow, sum (case action if ' user_repost ' then count else 0 end) as User_repost, SUM (case action, ' user_timeline ' then count else 0 end) as User_timeline from T_stat_user_action where Stat_time= ' 2015 0725 ' and id= ' 1820485935 ' GROUP by ID

Result is


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL or hive data rows become columns

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.