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
Here is a version of the query using
CASEwith an aggregate function:See SQL Fiddle with Demo (sql server version). You can replace the empty string with a
null.Edit #1: The key to getting this to work properly is the aggregate function. The
CASEstatement is putting each of theErrIdvalues 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:The result is:
As you can see the
id=1500you will get multiple rows which you do not want. So if you tell it that you want themax()value for eacherrid, then you will get one row per record.