0

I have the following temporary table I created from a view in T-SQL:

Id  |       Expiration Date      | Expiration Update Date
--------------------------------------------------------
38  |   2018-01-31 00:00:00.000  |  Dec 13 2017  6:28PM
37  |   2017-08-31 00:00:00.000  |  Jun  1 2017  6:15PM
37  |   2017-06-15 00:00:00.000  |  May 10 2017  8:33PM
38  |   2017-12-31 00:00:00.000  |  Nov 21 2017  3:38PM
37  |   2018-03-31 00:00:00.000  |  Feb 16 2018  7:10PM
38  |   2018-09-30 00:00:00.000  |  Mar 13 2018  2:29PM
37  |   2018-01-31 00:00:00.000  |  Dec 13 2017  6:28PM
39  |   2018-10-31 00:00:00.000  |  Jan 22 2018  6:38PM
37  |   2017-06-30 00:00:00.000  |  May  9 2017  8:30PM
37  |   2017-12-31 00:00:00.000  |  Nov 21 2017  3:38PM
39  |   2018-08-31 00:00:00.000  |  Jan  2 2018  4:08PM

I want to create query that will display it as

    Id | Expiration Date 1| Update Date 1       | Expiration Date 2| Update 2
   -----------------------------------------------------------------------------
    37 | 2017-06-30       | Nov 21 2017  3:38PM | 2017-12-31       |Nov 21 2017  3:38PM
    38 | 2017-12-31       | Nov 21 2017  3:38PM | 2018-01-31       |Dec 13 2017  6:28PM
    39 | 2018-08-31       | Jan  2 2018  4:08PM | 2018-10-31       |Jan 22 2018  6:38PM

I cut out the time stamp for the expiration dates to save space. The idea is a new column would be created for every instance of an expiration date of that ID along with a second column for when that expiration date was updated in the system. The catch is each idea does not have an equal number of expiration dates so the IDs with less expiration dates will have NULL values for the extra columns, which I am ok with.

  • Handling different number of columns dynamically isn't really what SQL is designed for, and solutions are somewhat ugly. If you can do this outside the database, it's probably going to be better. – James Z Apr 06 '18 at 16:19
  • I wrote an answer for this, with comments. The question is not an exact duplicate of the one sited. This one needs to pivot two different values in a 1,1; 2,2; 3,3 column order. It is worth letting people answer. Please consider reopening. – James L. Apr 06 '18 at 17:04
  • I dunno, there are already questions that answer how to pivot multiple columns, and all a reader has to do is apply the same dynamic approach to it. – Tab Alleman Apr 06 '18 at 17:19

0 Answers0