TRANSFORM in SQL Statement

lmonaghan

Board Regular
Joined
Nov 27, 2004
Messages
84
I searched the forums and I tried the advice from one string which did not work like a charm. Please help in assigning the problem with the following statement:

Code:
    SQL = "TRANSFORM SUM(LOAD.DITMIL) AS SUMOFDITMIL " _
        & "SELECT LOAD.DIUNIT, LOADEXT.DIDV# " _
        & "FROM I93FILE.LOAD, I93FILE.LOADEXT " _
        & "WHERE LOAD.DIODR# = LOADEXT.DEODR# AND LOAD.DIDISP = LOADEXT.DEDISP " _
        & " AND LOAD.DIDATE BETWEEN 85261 AND 85267 " _
        & " AND LOAD.DIMULT<>'S' " _
        & " AND LOAD.DICONT IN ('0','1') " _
        & " AND LOADEXT.DIDV#<>'LOG' " _
        & "GROUP BY LOAD.DIUNIT, LOADEXT.DIDV# " _
        & "ORDER BY LOAD.DIUNIT " _
        & "PIVOT LOAD.DIDATE; "

Thanks in advance for your help!
 
Lita,

This should be doable. I have successfully used TRANSFORM within Excel; and have kept some scribbled notes too.

How about attacking this first up without the TRANSFORM step just to be sure that the query is working OK? Maybe you've done that already? When that is confirmed, sort out the SQL.

My notes show in summary,
Code:
TRANSFORM [data field]
SELECT [row field/s]
FROM [data source]
optional WHERE [criteria]
GROUP BY [row field/s]
PIVOT [column field] optional IN [particular column field/s]

One observation, from my notes and an example I've kept is that the row field/s for the SELECT and GROUP BY are identical. This is not the case in your code. Viz, your SELECT has units.undv# & COUNT(...) and the GROUP BY has only the units.undv#. Without being sure, it looks like the COUNT should not be there. It is a data field not a row field?

HTH
Fazza
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Thank you for your response! I tried your suggestion of bringing in the data without the TRANSFORM and it is imported just find. I then did an aggregate function and grouping it by two different fields and that worked fine. The last step was to combine the two with the TRANSFORM and it doesn't work.

Here is the succession of queries that I wrote:

Code:
    SQL = "SELECT units.ununit, units.unctyt, units.undv# " _
    & "FROM i93file.units " _
    & "WHERE units.undel<>'D' AND units.unflet IN ('01') " _
    & "ORDER BY units.ununit "
    
    SQL2 = "SELECT units.undv#, units.unctyt, count(units.ununit) as count " & _
    "FROM i93file.units " & _
    "WHERE units.undel<>'D' AND units.unflet IN ('01') " & _
    "GROUP BY units.undv#, units.unctyt " & _
    "ORDER BY units.undv# "
    
    SQL3 = "TRANSFORM COUNT(units.ununit) as units " & _
    "SELECT units.undv# " & _
    "FROM i93file.units " & _
    "WHERE units.undel<>'D' AND units.unflet IN ('01') " & _
    "GROUP BY units.undv# " & _
    "PIVOT units.unctyt; "

Any other thoughts?
 
Upvote 0
Well, it must be close, Lita. :)

Try simplifying it a little more and see if you can get ANY cross-tab result.

Just noticed, why is there a ";" at the end of the query string? What if that is deleted? Fix it?

Anyway, back to the earlier thought, simplify as much as possible. Say to,
Code:
    SQL3 = "TRANSFORM COUNT(units.ununit) " & _ 
    "SELECT units.undv# " & _ 
    "FROM i93file.units " & _ 
    "GROUP BY units.undv# " & _ 
    "PIVOT units.unctyt"

That is, have taken out the name change for the count - "AS units" and the whole WHERE clause, and removed the final (superfluous?) punctuation.

Does this simpler TRANSFORM work?

Fazza
 
Upvote 0
Lita,

If a query "doesn't work", please explain exactly what happens or doesn't work or error message received.

thanks,
Fazza
 
Upvote 0
Jumping into the conversation late...

For 95% of the SQL statements I use, I use the UI in Access (or MSQuery) to generate the query. Once that happens, I copy+paste+modify the SQL from that program into VB(A).

In your case, since you can create the desired result in Access, do so. Then, in Access, switch to the SQL view and copy the SQL statement into VB(A).
 
Upvote 0
My query returns the following error:
Run-Time error '1004':
Application-defined or object-defined error

I tried your latest suggestion and return the same error. I have also tried copying the SQL in Access and pasting it into Excel VBA with the same result.

It seems that I must be close! I do appreciate your help on the subject. I want to avoid bringing in the data and putting it into a pivot table, but if that is the end result, then so be it. I'd still like to understand why this isn't working though.

Lita
 
Upvote 0
When you write "your latest suggestion" it would help if you mentioned who you are referring to!

Have you established a database connection? Initialized the appropriate variables? As the correct data types?

It's hard to believe a SQL query runs in Access but fails in Excel. After all, SQL is SQL and irrespective of the supporting app, the SQL statement itself cannot yield different results. What you do before it may and what you do after it may but not not the execution of the SQL statement itself (unless, like I asked above, your database connection is different/incorrect).

My query returns the following error:
Run-Time error '1004':
Application-defined or object-defined error

I tried your latest suggestion and return the same error. I have also tried copying the SQL in Access and pasting it into Excel VBA with the same result.

It seems that I must be close! I do appreciate your help on the subject. I want to avoid bringing in the data and putting it into a pivot table, but if that is the end result, then so be it. I'd still like to understand why this isn't working though.

Lita
 
Upvote 0
Lita,

I am concerned there is an overlap of problems here. It seemed like the simpler queries worked and it was only the SQL to fix. I am now unclear if the simpler query still works.

1. Can you please confirm that your code works with your simpler query - not the cross tab.

2. After step 1 works, please test with the simplified cross-tab query & post the results including a print out of the SQL string. Maybe via a debug.print in VBA.

BTW, I am unclear if you tried deleting the final ";" in the earlier posted SQL.

regards,
Fazza
 
Upvote 0
Hi Tushar,

I write SQL either directly into MS Query or create it via code in Excel VBA or sometimes in the VBA immediate window. I had understood that there were subtle differences in syntax between SQL for MS Access and MS Excel so I have not used that route. Can you please comment on any details that need to change for SQL from MS Access to work in Excel?

regards,
Fazza
 
Upvote 0
Hi Lita,

Wondering about the status of this. Would appreciate your advice.

regards,
Fazza
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,344
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top