We do not have access to SQL server at work so I have to design an app within Excel VBA and using a text file (CSV) to store the data.
I have no problem querying data, joining the CSV's, but I would like to use the SQL Pivot/Unpivot statement to transpose one of the columns into rows. I'm not sure if that functionality exists as I keep getting the error that there's a syntax error in my FROM clause.
Public Function getData() As ADODB.Recordset
Dim path As String, conn As ADODB.Connection, rs As ADODB.Recordset
path = ThisWorkbook.path & "\"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & path & ";" & _
"Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;""")
rs.ActiveConnection = conn
rs.Source = "SELECT * " & _
"FROM " & _
"(SELECT emp_id, client, allocation " & _
"FROM ALLOCATIONdb.csv) AS s " & _
"PIVOT (SUM(allocation) FOR client IN (client1, client2)) AS pvt"
Set getData = rs
End Function
My data currently looks like the top of the picture and I want it to look like the bottom.
This is simple enough to do with a pivot table but I wanted the user to be able to type over the data.
Additionally, is there anyway to make it dynamic because there will be an unknown number of possible clients so the number of rows would need to expand.
thanks in advance
This might not be the best of the solution, but it did work for me. I converted my data to a list and passed it to a function called "ToDataTable" to convert into datatable first.
Then using GetInversedDataTable() function will convert it to the format you want.
Use it like this :
The three columns are provided and a new DataTable is returned.
The example below will use the source table and the params below to build a Pivot Table. In your case here : X axis column: "client" Y axis column: "emp_id" Z axis column: "allocation" Null value: "0"; Sum of values: true
End Function
Transform ended up doing what I needed.
thanks for the help!