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

  • SEARCH
  • Home
  • 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 9089401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:08:26+00:00 2026-06-16T22:08:26+00:00

I’ve seen this SO question (How can I Pivot a table in DB2?) and

  • 0

I’ve seen this SO question (How can I Pivot a table in DB2?) and some of the answers/comments mention being able to acheive the same effect with a case statement but I’m struggling mightily to acheive this, probably due to a few years away from much SQL.

Here is a snapshot of some data I have:

ID    Date          ErrID   ErrDesc
---------------------------------------
164   2012-09-21    1402    Large V
164   2012-09-21    1409    Missing
416   2012-09-21    1409    Missing
1380  2012-09-21    1411    n - Mis
1500  2012-09-17    1411    n - Mis
1500  2012-09-21    1402    Large V

ID and Date taken together need to be unique in a query that would return something such as the following, where the empty slots are null. How can I achieve this, with a case statement or otherwise? Bear in mind I don’t think our version of DB2 supports “decode”, and the SQL I need also needs to be able to run on Derby for testing. Thanks in advance.

ID      Date        Err1402     Err1409     Err1411
-----------------------------------------------------------------------
164     2012-09-21  Large V     Missing
416     2012-09-21              Missing
1380    2012-09-21                          n - Mis
1500    2012-09-17                          n - Mis
1500    2012-09-21  Large V 
  • 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-16T22:08:26+00:00Added an answer on June 16, 2026 at 10:08 pm

    Here is a version of the query using CASE with an aggregate function:

    select id,
      date,
      max(case when errid = 1402 then ErrDesc else '' end) Err1402,
      max(case when errid = 1409 then ErrDesc else '' end) Err1409,
      max(case when errid = 1411 then ErrDesc else '' end) Err1411
    from yourtable
    group by id, date
    order by id
    

    See SQL Fiddle with Demo (sql server version). You can replace the empty string with a null.

    select id,
      date,
      max(case when errid = 1402 then ErrDesc else null end) Err1402,
      max(case when errid = 1409 then ErrDesc else null end) Err1409,
      max(case when errid = 1411 then ErrDesc else null end) Err1411
    from yourtable
    group by id, date
    order by id
    

    Edit #1: The key to getting this to work properly is the aggregate function. The CASE statement is putting each of the ErrId values into the columns but if you do not use the aggregate function you will wind up with multiple rows for each id, date. So if you use:

    select id,
      date,
      (case when errid = 1402 then ErrDesc else '' end) Err1402,
      (case when errid = 1409 then ErrDesc else '' end) Err1409,
      (case when errid = 1411 then ErrDesc else '' end) Err1411
    from yourtable
    order by id
    

    The result is:

    |   ID |                             DATE | ERR1402 | ERR1409 | ERR1411 |
    -------------------------------------------------------------------------
    |  164 | September, 21 2012 00:00:00+0000 | Large V |         |         |
    |  164 | September, 21 2012 00:00:00+0000 |         | Missing |         |
    |  416 | September, 21 2012 00:00:00+0000 |         | Missing |         |
    | 1380 | September, 21 2012 00:00:00+0000 |         |         | n - Mis |
    | 1500 | September, 17 2012 00:00:00+0000 |         |         | n - Mis |
    | 1500 | September, 21 2012 00:00:00+0000 | Large V |         |         |
    

    As you can see the id=1500 you will get multiple rows which you do not want. So if you tell it that you want the max() value for each errid, then you will get one row per record.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.