Link to home
Start Free TrialLog in
Avatar of aaxen
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
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Something like this should work:
+Pivot by Period
+Take max value of Status -- should be highest letter
+Grouped by ID
TRANSFORM Max([Status]) AS MaxOfStatus 
SELECT [ID] 
FROM M_Sales 
GROUP BY [ID]
PIVOT Period In ("January","February","March","April","May","June","July","August","September","October","November","December");

Open in new window

Avatar of aaxen
aaxen

ASKER

let me try
Avatar of aaxen

ASKER

hmm doesnt work :(

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

Open in new window

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','March','April','May','June','July','August','September','October','November','December')"

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"",""July"",""August"",""September"",""October"",""November"",""December"")"

Hope that helps.

If this is SQL Server backend, then you will have to construct SQL much differently so please advise.
Avatar of aaxen

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

Open in new window

Avatar of aaxen

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...
BTW, you removed the IN statement from the PIVOT:

SQL &= "PIVOT Period (""January"",""February"",""March"",""April"",""May"",""June"",""July"",""August"",""September"",""October"",""November,""December"")"

Should be:

SQL &= "PIVOT Period IN(""January"",""February"",""March"",""April"",""May"",""June"",""July"",""August"",""September"",""October"",""November,""December"")"
Avatar of aaxen

ASKER

i click the button the data grid doesnt show any table
Avatar of aaxen

ASKER

tried that as well still no table show...
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of aaxen

ASKER

Ahhh already got it.. thanks Its because of the space between each sql &= line
Glad you saw that is what I changed above.