Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:55:40+00:00 2026-06-15T19:55:40+00:00

I have some data organized by quintile labels (-1, 1, 2, 3, 4, 5).

  • 0

I have some data organized by quintile labels (-1, 1, 2, 3, 4, 5). For each of these values in a Quintile column, there is a value in another column called ret. Lastly, there is a column of dates containing month-end dates as integers.

My goal is to visualize all of the Quintile returns data at the same time, each as its own column, with only the date column acting like an index.

Essentially, I want to pivot on the Quintile column and I have seen other places advising the use of IF statements in MySQL as a way to achieve this.

For example, here is a query that would show one Quintile’s worth of the data:

select yearmonth, ret
where Quintile=1
from quintile_returns

But I don’t want to repeat this for all Quintile labels, save out the data individually, and piece it together in Python Pandas or Excel or something. I want to make SQL show it as distinct columns.

But when I try this IF statement style poor man’s pivot, this is the query I use:

select yearmonth, 
       IF(Quintile=1, ret, NULL) as Q1_ret,
       IF(Quintile=2, ret, NULL) as Q2_ret
from quintile_returns

I basically get a diagonal of valid data back. All the rows where the Quintile is not 1 still show up, populated with NULL, and then so on for Quintile 2.

How do I avoid all of these extra NULL values? Basically, I want to tell SQL to return the column’s value only if the condition is satisfied, and do not use NULL or anything else as a default else-like placeholder.

Is there a way to do this that does not involve nested join-type statements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:55:42+00:00Added an answer on June 15, 2026 at 7:55 pm

    You can use GROUP BY to only show one row for each yearmonth value, and then SUM() along with your IF() statements so that the ret VALUES are only summed when the columns’ IF() condition evaluates to TRUE:

    SELECT `yearmonth`,
        SUM(IF(`Quintile` = 1, ret, NULL)) as `Q1_ret`,
        SUM(IF(`Quintile` = 2, ret, NULL)) as `Q2_ret`
    FROM `quintile_returns`
    GROUP BY `yearmonth`
    

    Otherwise, you had the right idea with the IF() statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data that is stored in a TIMESTAMP(6) WITH TIMEZONE column in
I have some data that won't printf.... echo works, but not printf There is
I have some data that I would like to visualize. Each byte of the
I have an excel sheet which has some huge data. Data is organized as
I have a lot of JSON data which is organized in nested lists. Each
I have some data loaded as a np.ndarray and need to convert it to
I have some data from log files and would like to group entries by
I have some data files in the resources/ folder for my Rubymotion project. How
I have some data which (quite reasonably) uses null and false for different meanings.
I have some data and need to create a json file with this structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.