-1

I have spent an hour already on this problem.

I want to dynamically generate columns based on the values from the column AttendanceDate.

I have found some similar questions, but unfortunately the examples were too complicated for me to comprehend.

Data:

enter image description here

Expected output:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Giovanni Lobitos
  • 852
  • 7
  • 19
  • 4
    Google: SQL Server dynamic pivot. – Gordon Linoff Jul 07 '18 at 14:03
  • 3
    It is a common question... have you tried using other examples from StackOverflow for such a Pivot? For example, here someone asked about columns with names such as "name", "email", "phone"... you would be using your dates, but it doesn't matter because it's dynamic... https://stackoverflow.com/questions/12210692/t-sql-dynamic-pivot – Mike M Jul 07 '18 at 14:07
  • You are just going to be referred to the many many other examples. Try something.. anything. – Nick.Mc Jul 07 '18 at 14:45
  • If you don't understand what you've found, show what you have tried so far at least, so we can help you. Any answer we provide would, otherwise, mean you don't understand it either, meaning it's useless to you. Unless you can understand and maintain the code you're provided you shouldn't use it. – Thom A Jul 07 '18 at 14:49
  • I kinda gave up on the pivot thing. I just performed multiple queries. The queries are in this order: 1. List of Students 2. All Dates (Distinct) 3. All Statuses Per Student with the date condition. I'll optimize this when I need to. For now, my focus is the prototype. Thanks, everyone. – Giovanni Lobitos Jul 07 '18 at 15:22

1 Answers1

1

This can be done with the stuff method as mention in comments or with a while exists implementation:

http://rextester.com/FPU47008

Ian-Fogelman
  • 1,595
  • 1
  • 9
  • 15