0

Would you help me, please, to make pivot table from this (database in MS SQL Server 2016):

clients date_start  date_end    result
----------------------------------------
client1 01.09.2019  05.09.2019  red
client1 01.09.2019  05.09.2019  blue
client1 01.09.2019  05.09.2019  green
client2 01.09.2019  05.09.2019  red
client2 01.09.2019  05.09.2019  blue
client3 01.09.2019  05.09.2019  yellow

into this:

clients red blue    green   yellow
----------------------------------
client1 1   1       1   
client2 1           1   
client2                     1

Details: I have a table with clients' results. Each result is unique and it contains datestart and date_end. I wanna select clients' results on specific date. If date is between date_start and date_end, it goes into the pivot table.

Initially, we don't know all the kinds of results (i.e. it can be orange tomorrow). It should be dynamic.

Alex Ivanov
  • 657
  • 1
  • 8
  • 17
  • 2
    The solution for this is likely to be product-specific. Please tag your question with the specific database that you're using. [Why should I “tag my RDBMS”?](https://meta.stackoverflow.com/questions/388759/why-should-i-tag-my-rdbms) – Eric Brandt Apr 20 '20 at 14:24
  • 2
    This is typically better done in your application when you *display* the data. –  Apr 20 '20 at 14:29
  • Eric, thank you, the database is MSSQL. a_horse_with_no_name, unfortunatelly, the clients' data is confidential, but the structure I displayed in the very beginning of the question is absolutelly the same. – Alex Ivanov Apr 20 '20 at 14:36
  • 1
    Check this should help help you. https://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query – Pankaj_Dwivedi Apr 20 '20 at 14:43
  • Pankaj_Dwivedi, Eric Brandt thank you very much for the links. It completely solved my question. – Alex Ivanov Apr 20 '20 at 15:04

0 Answers0