SQL Pivot not working (1 Viewer)

Fikus01

New member
Local time
Today, 07:00
Joined
Aug 17, 2017
Messages
9
Morning everyone, I'm trying to get a pivot query to work on the below data source. My aim is to get the CarId to be the column headers and the axle serial as the data source all on 1 row. the table name MTDN is actually a query that gets the information I need into 1 place.

Table: should be an attachment as the copy and paste I tried, didn't work!


Syntax:

SELECT [CarId],[Axle Serial]

FROM [MTDN] AS SourceTable

PIVOT (MAX([Axle Serial]) FOR [CarId]

IN([110**-1],[110**-2],[110**-3],[110**-4],[120**-1],[120**-2],[120**-3],[120**-4],[130**-1],[130**-2],[130**-3],[130**-4],[140**-1],[140**-2],[140**-3],[140**-4]))

AS Resultsheet;


I cant see what I've done wrong but it wont let me save the query, access keeps saying there id an error in the from clause and when I ok it, the word PIVOT is highlighted!

I am quite new to SQL so I've probably made a basic error somewhere but I cant see it amongst all the online examples. I can get the data to pivot in access but when I try to use the data in a report, it automatically reverts back to the table view!

any help will be appreciated.

Thanks

Fikus
 

Attachments

  • Untitled.png
    Untitled.png
    10.9 KB · Views: 143
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 15:00
Joined
Feb 19, 2013
Messages
16,607
In Access, a pivot query is a view that can only be seen as a query - the underlying query is a simple select query. You cannot use it as a recordsource to a form or report and have it appear as a pivot. Suggest try a crosstab query instead.

With regards your code, not sure the 'AS Resultsheet' is correct
 

Fikus01

New member
Local time
Today, 07:00
Joined
Aug 17, 2017
Messages
9
ok thanks, I tried a crosstab but it doesn't like it, keep getting an error related to a query where I use a combo on my front page to select my required results. for the same reason I cant seem to make a table with this result set.

all I need to do is have column 1 results as my column names, and then have the axle serial listed underneath it. the set is already narrowed down in 3 querys to isolate these particular values. I thought it would be easier in SQL. copying it out to msexcel to manipulate the results works but is time consuming.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:00
Joined
May 7, 2009
Messages
19,233
Put your db in zip file and upload it.
 

Fikus01

New member
Local time
Today, 07:00
Joined
Aug 17, 2017
Messages
9
im afraid I cant do that, this is a work project and the database contains sensitive information hence just sharing an image of the table in the original post
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:00
Joined
Feb 19, 2013
Messages
16,607
does sound like your data is not normalised and you are trying to get a normalised view.

You haven't said what your error is, but it may be to do with the fact that for crosstabs you have to specify the parameters - see parameters option on the ribbon
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:00
Joined
May 7, 2009
Messages
19,233
i tried to recreate your table and
found out that you cannot use
it in Pivot or in Crosstab.

the good news is i made a Sub
that will make a query (zzMTD)
that will pull the data that you need.

the bad news is, if you have many
CardID the Sub will fail since
it is building a Query from the MTD table.

so, you may try it if it works.

put the code in Standard Module and run it.

you need a form (Datasheet) to use the zzMTD
query. and on the Open Event of that form,
call the Sub again:

Private Sub Form_Open(Cancel As Integer)
Call CreatePivot
End Sub

this will dynamically recreate the query.

Code:
Public Sub CreatePivot()
    Dim db As DAO.Database
    Dim qd As DAO.QueryDef
    Dim rs As DAO.Recordset
    Dim strSource As String
    Dim strSQL As String
    Dim bolNext As Boolean
    Set db = CurrentDb
    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT CardID FROM MTDN ORDER BY CardID", dbOpenDynaset)
    With rs
        If Not (.BOF And .EOF) Then .MoveFirst
        While Not .EOF
            If Not bolNext Then
                strSource = " FROM MTDN WHERE CardID='" & ![CardID] & "';"
                strSQL = "SELECT MAX([axle_serial]) As [" & ![CardID] & "],"
                bolNext = True
            Else
                strSQL = strSQL & "(SELECT MAX([axle_serial]) FROM MTDN WHERE CardID='" & ![CardID] & "') AS [" & ![CardID] & "],"
            
            End If
                
            .MoveNext
        Wend
        .Close
    End With
    Set rs = Nothing
    If Len(strSQL) > 0 Then strSQL = Left(strSQL, Len(strSQL) - 1)
    strSQL = strSQL & strSource
    On Error Resume Next
    Set qd = db.QueryDefs("zzMTD")
    If Err.Number <> 0 Then
        On Error GoTo 0
        db.QueryDefs.Append db.CreateQueryDef("zzMTD", strSQL)
        db.QueryDefs.Refresh
        Application.RefreshDatabaseWindow
    Else
        qd.SQL = strSQL
    End If
    Set qd = Nothing
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:00
Joined
May 7, 2009
Messages
19,233
Guess i was wrong:

TRANSFORM Max(MTDN.axel_serial) AS MaxOfaxel_serial
SELECT "Max Value" AS CardID
FROM MTDN
GROUP BY "Max Value"
PIVOT MTDN.CardID;
 

Users who are viewing this thread

Top Bottom