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
aaxenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
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

0
aaxenAuthor Commented:
let me try
0
aaxenAuthor Commented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kevin CrossChief Technology OfficerCommented:
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.
0
aaxenAuthor Commented:
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

0
aaxenAuthor Commented:
suppose to be query for vb.net using ado to connect acess so i assume its the same query send to acess
0
Kevin CrossChief Technology OfficerCommented:
What error message are you getting?

Need more details on "doesn't work", please...
0
Kevin CrossChief Technology OfficerCommented:
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"")"
0
aaxenAuthor Commented:
i click the button the data grid doesnt show any table
0
aaxenAuthor Commented:
tried that as well still no table show...
0
Kevin CrossChief Technology OfficerCommented:
Here, copy and past this directly over same 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 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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
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.
0
aaxenAuthor Commented:
Ahhh already got it.. thanks Its because of the space between each sql &= line
0
Kevin CrossChief Technology OfficerCommented:
Glad you saw that is what I changed above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.