Build SQL query with dynamic columns
My tables go as follows: Patients table PatientId Name 1 James ... Visits table Date PatientID_FK Weight 1/1 1 220 2/1 1 210 ... How can I build a query that returns PatientId Name Visit1Date Visit1Weight Visit2Date Visit2Weight ... 1 James 1/1 220 2/1 210 2 ... How can we add more columns in this way? How to write that SELECT ? Please help. Some posts on StackExchange say it is impossible for a SQL statement to handle it. Is it really so? This type of data transformation will need to be done with both a pivot and the unpivot functions. Since your visits will be unknown, then you will want to