简体   繁体   中英

How do I format numbers in and Access crosstab query to show two decimal places?

I have an Access crosstab query that displays the following results:

| SHORE_TYPE  | Total Miles | Class 1           | Class 2          | Class 4 |
| ONSHORE     | 31.37       | 0.337121212121212 | 12.4617424242424 | 0       |

I'd like it to display the following results instead. Note the 'Class' columns here show two decimal places:

| SHORE_TYPE  | Total Miles | Class 1 | Class 2 | Class 4 |
| ONSHORE     | 31.37       | 0.34    | 12.46   | 0.00    |

I've been able to configure the 'Total Miles' column by changing the Format and Decimal Places properties (in the Design View) to "Fixed" and "2," respectively. However, the query column (in Design View) that determines the value in the Class column has only a Format property, which I set to "Fixed"; there is not a Decimal Places property for me to adjust.

I have some similar crosstab queries that are showing the results in the way I desire, but I can't determine any differences between this one and those. Also, I've sometimes seen some of my queries display it the wrong way one time, then the desired way the next time.

This makes me wonder if the problem is a bug in Access, or if there is a something implicitly defined in my code that I should explicitly define.

Here is my SQL:

TRANSFORM IIf(IsNull(Sum([qryPartL].[MILES_OF_PHYS_LENGTH])),0,
Sum([qryPartL].[MILES_OF_PHYS_LENGTH])) AS SumOfMILES_OF_PHYS_LENGTH
SELECT qryPartL.SHORE_TYPE, Sum(qryPartL.MILES_OF_PHYS_LENGTH) AS [Total Miles]
FROM qryPartL
GROUP BY qryPartL.SHORE_TYPE
PIVOT qryPartL.CLASS_LOC_text In ("Class 1","Class 2","Class 4");

EDIT: After closing and re-opening this query, the Total Miles column is now displaying 31.3714015..., and the properties I had previously set for this column in the Design View are now blank. So, it looks like Access does not consistently save these property settings. At least not in the context in which I was using them.

The trick is to use a series of nested functions.

  • CDbl: Converts the data to a Double number data type
  • FormatNumber: Returns an expression formatted as a number with a specified precision (2)
  • Nz: Returns the specified value (0) when a field is null
    • The CDbl function won't work if a value is Null.

I also removed the IIf function from the TRANSFORM clause since Nz works better in this case.

Here is the new SQL that returns the desired results. (I've added new lines and indents to make it easier to read. This is a not necessary step, and may in fact not be remembered by Access.)

TRANSFORM 

 CDbl(
    FormatNumber(
         Nz(
             Sum([qryPartL].[MILES_OF_PHYS_LENGTH])
         ,0)
     ,2)
 ) AS SumOfMILES_OF_PHYS_LENGTH

SELECT qryPartL.SHORE_TYPE, 

 CDbl(
    FormatNumber(
         Nz(
             Sum(qryPartL.MILES_OF_PHYS_LENGTH)
         ,0)
     ,2)
 ) AS [Total Miles]

FROM qryPartL
GROUP BY qryPartL.SHORE_TYPE
PIVOT qryPartL.CLASS_LOC_text In ("Class 1","Class 2","Class 4");

Thanks to Allen Browne and a tip on his awesome Access website for leading me to this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM