aaxen
asked on
Using Pivot
Hi experts,
Let say i have the following table:
ID Period Status
------- ------------- -----------
1 January P
1 March A
1 Febuary W
2 March W
2 January A
2 Febuary A
3 Febuary P
3 January P
3 March W
How do i convert this table into the following table using Pivot ( or any other way)
ID January Febuary March
1 P W A
2 A A W
3 P P W
Something like that for using ADO.net queries
Let say i have the following table:
ID Period Status
------- ------------- -----------
1 January P
1 March A
1 Febuary W
2 March W
2 January A
2 Febuary A
3 Febuary P
3 January P
3 March W
How do i convert this table into the following table using Pivot ( or any other way)
ID January Febuary March
1 P W A
2 A A W
3 P P W
Something like that for using ADO.net queries
ASKER
let me try
ASKER
hmm doesnt work :(
This is my full code
This is my full code
Private Sub btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Search.Click
Try
Dim ds1 As New DataSet
Dim SQL As String
SQL = "TRANSFORM Max([Status]) AS MaxOfStatus "
SQL &= "Select Case [Property_ID]"
SQL &= "FROM Payment"
SQL &= "GROUP BY [Property_ID]"
SQL &= "PIVOT Period In ('January','February','March','April','May','June','July','August','September','October','November','December')"
ds1.Clear()
MessageBox.Show(SQL)
OleDbDataAdapter1.SelectCommand.CommandText = SQL
OleDbDataAdapter1.Fill(ds1, "rentdue")
DataGrid2.DataSource = ds1.Tables("rentdue")
Catch
End Try
End Sub
You have a CASE statement there for some reason.
SQL &= "Select Case [Property_ID]"
This is MS Access as the zone suggests, correct?
Removing 'Case' from the select statement should fix this, but if not take a look at this line:
SQL &= "PIVOT Period In ('January','February','Mar ch','April ','May','J une','July ','August' ,'Septembe r','Octobe r','Novemb er','Decem ber')"
To get double quotes in VB you have to use two to get one (i.e. ""), so code would become:
SQL &= "PIVOT Period In (""January"",""February"", ""March"", ""April"", ""May"","" June"",""J uly"",""Au gust"",""S eptember"" ,""October "",""Novem ber"",""De cember"")"
Hope that helps.
If this is SQL Server backend, then you will have to construct SQL much differently so please advise.
SQL &= "Select Case [Property_ID]"
This is MS Access as the zone suggests, correct?
Removing 'Case' from the select statement should fix this, but if not take a look at this line:
SQL &= "PIVOT Period In ('January','February','Mar
To get double quotes in VB you have to use two to get one (i.e. ""), so code would become:
SQL &= "PIVOT Period In (""January"",""February"",
Hope that helps.
If this is SQL Server backend, then you will have to construct SQL much differently so please advise.
ASKER
still doesnt work...
Private Sub btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Search.Click
Try
Dim ds1 As New DataSet
Dim SQL As String
SQL = "TRANSFORM Max(status) AS MaxOfStatus "
SQL &= "Select Property_ID"
SQL &= "FROM Payment"
SQL &= "GROUP BY Property_ID"
SQL &= "PIVOT Period (""January"",""February"",""March"",""April"",""May"",""June"",""July"",""August"",""September"",""October"",""November,""December"")"
ds1.Clear()
MessageBox.Show(SQL)
OleDbDataAdapter1.SelectCommand.CommandText = SQL
OleDbDataAdapter1.Fill(ds1, "rentdue")
DataGrid2.DataSource = ds1.Tables("rentdue")
Catch
End Try
End Sub
ASKER
suppose to be query for vb.net using ado to connect acess so i assume its the same query send to acess
What error message are you getting?
Need more details on "doesn't work", please...
Need more details on "doesn't work", please...
BTW, you removed the IN statement from the PIVOT:
SQL &= "PIVOT Period (""January"",""February"", ""March"", ""April"", ""May"","" June"",""J uly"",""Au gust"",""S eptember"" ,""October "",""Novem ber,""Dece mber"")"
Should be:
SQL &= "PIVOT Period IN(""January"",""February" ",""March" ",""April" ",""May"", ""June""," "July"","" August""," "September "",""Octob er"",""Nov ember,""De cember"")"
SQL &= "PIVOT Period (""January"",""February"",
Should be:
SQL &= "PIVOT Period IN(""January"",""February"
ASKER
i click the button the data grid doesnt show any table
ASKER
tried that as well still no table show...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You have a Try/Catch, but are not doing anything in the Catch. You may want to add something while debugging to display what the error message is or comment out the try/catch code until working.
ASKER
Ahhh already got it.. thanks Its because of the space between each sql &= line
Glad you saw that is what I changed above.
+Pivot by Period
+Take max value of Status -- should be highest letter
+Grouped by ID
Open in new window