2

I have the following SQL statement to create a summary view

TRANSFORM Sum(Amount) AS CurrAmount 
SELECT Currency, Sum(Amount) AS TotalAmount 
From MyTable 
GROUP BY Currency
ORDER BY Currency 
PIVOT Source

It creates the following view

Currency    TotalAmount Retail  Corporate     Others
EUR               7,071    585      2,345      4,141
GBP              10,444  2,322      4,889      3,233
JPY               7,050  1,295      4,500      1,255
USD               1,625    250        450        925

I am looking for help wherein I need the 'TotalAmount' field to appear as the last column. Much appreciated

Niz

4
  • In datasheet view, you can drag&drop TotalAmount to the position you want it, and save the query. It will stay there, if you reopen the query, but not if the query structure changes. So this is only a workaround, but it may help.
    – Andre
    Nov 30, 2015 at 22:19
  • Thanks Andre. This definitely is the option in MSAccess. I missed to mention though that my requirement is to use the SQL in my application and in there, I am unable to move it.
    – Niz
    Dec 1, 2015 at 14:48
  • Any help here friends?
    – Niz
    Dec 3, 2015 at 16:28
  • It may be impossible. But let's see... Oh, what is your application?
    – Andre
    Dec 3, 2015 at 16:32

2 Answers 2

1
+50

I think your requirement for column ordering/sequence can be handled by the IN clause that is supported by the TRANSFORM operator. Have a look at this:

TRANSFORM <aggregate-function-expression>
    <select-statement>
PIVOT <expression>
[IN (<column-value-list>)]

where <aggregate-function-expression> is an expression created with one of the aggregate functions, <select-statement> contains a GROUP BY clause, and <column-value-list> is a list of required values expected to be returned by the PIVOT expression, enclosed in quotes and separated by commas. (You can use the IN clause to force the output sequence of the columns.)

In other words, just use IN and put your quote/comma delimited list of columns in the desired order (e.g. IN ("Currency", "Retail", "Corporate", "Others", "TotalAmount"))

Seems a little complicated to me, but it appears to be supported by Access.

Note: this info was grabbed from the following article: TRANSFORM Statement

2
  • Ah, of course. I have used the (somewhat similar) IN clause for pivot queries in T-SQL, but totally forgotten that Access has this too. Thanks! (Note: I have to wait until the bounty is 24h old to award it.)
    – Andre
    Dec 4, 2015 at 9:33
  • It somewhat does the job but behaves quite strange at times. Moreover, the departments (retail, corporate) are not fixed for me. New departments keep coming and therefore I cannot be hard coding it every time. I had it resolved in a different way.
    – Niz
    Dec 31, 2015 at 13:51
0

The solution using IN clause somewhat does the job but behaves quite strange at times. Moreover, the departments (retail, corporate) are not fixed for me. New departments keep coming and therefore I cannot be hard coding it every time. I had it resolved in a simpler way.

SELECT Currency,
(SELECT SUM(t1.Amount) FROM MyTable t1 WHERE t1.Source = 'Retail' AND t1.Curr = pos.Curr) Retail,
(SELECT SUM(t1.Amount) FROM MyTable t1 WHERE t1.Source = 'Corporate' AND t1.Curr = pos.Curr) Corporate,
(SELECT SUM(t1.Amount) FROM MyTable t1 WHERE t1.Source NOT IN ('Retail','Corporate') AND t1.Curr = pos.Curr) Others,
(SELECT SUM(t1.Amount) FROM MyTable t1 WHERE t1.Curr = pos.Curr) Total
FROM MyTable pos
GROUP BY Curr
ORDER BY Curr

Even in the above, if there are new departments (appearing in Others) I will end up changing the SQL if I may need to specify, but still is in my control unlike the TRANSFORM statement where is goes out of control.

Thanks guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.