[Solved] Show two decimal places for average

Discuss the database features
Post Reply
wagtunes
Posts: 20
Joined: Tue Apr 26, 2016 6:11 pm

[Solved] Show two decimal places for average

Post by wagtunes »

I have a simple query that I want to format to 2 decimal places. Here is the query.

SELECT AVG( "Score" ) AS "Score", "Oil Pattern" FROM "Scores" GROUP BY "Oil Pattern" ORDER BY "Score" DESC

The database is simple. It's just 2 fields. An oil pattern and a score for that oil pattern. There are 12 oil patterns and several hundred scores for each one. Right now, the query comes out with the results something like this...

Viper - 261
Weber - 257
Dragon - 254

And so on.

I want the results to show with decimals like this...

Viper - 261.48
Weber - 257.29
Dragon - 254.73

How do I do this? I know I need something like...

AS Decimal (5,2) Varchar(5)

But no matter how I try to insert that in the above query I keep getting syntax errors. I'm thinking I'm missing a parenthesis somewhere but I'm not sure. Any assistance you experts can give me will be greatly appreciated.

 Edit: Changed subject, was Need Help Formatting A Simple Query 
Make your post understandable by others 
-- MrProgrammer, forum moderator 

Thanks.
Last edited by MrProgrammer on Fri Dec 29, 2023 5:57 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
OpenOffice 3.4.1 on Windows 8
FJCC
Moderator
Posts: 9428
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Need Help Formatting A Simple Query

Post by FJCC »

Try casting your Score value as a DOUBLE before taking the average

Code: Select all

SELECT AVG(CAST("Score" AS DOUBLE)) AS "Score", "Oil Pattern" FROM "Scores" GROUP BY "Oil Pattern" ORDER BY "Score" DESC
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
wagtunes
Posts: 20
Joined: Tue Apr 26, 2016 6:11 pm

Re: Need Help Formatting A Simple Query

Post by wagtunes »

FJCC wrote: Fri Dec 29, 2023 5:29 pm Try casting your Score value as a DOUBLE before taking the average

Code: Select all

SELECT AVG(CAST("Score" AS DOUBLE)) AS "Score", "Oil Pattern" FROM "Scores" GROUP BY "Oil Pattern" ORDER BY "Score" DESC
Thank you. That did it. Now I'm going to study the code to see if I understand it. Much appreciated. Will mark as solved.

How do I mark as solved? Not letting me change subject.
OpenOffice 3.4.1 on Windows 8
FJCC
Moderator
Posts: 9428
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Show two decimal places for average

Post by FJCC »

By the way, it would be preferable to define the column as type DOUBLE in the table so you don't have to deal with CASTing in the query.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
wagtunes
Posts: 20
Joined: Tue Apr 26, 2016 6:11 pm

Re: [Solved] Show two decimal places for average

Post by wagtunes »

FJCC wrote: Fri Dec 29, 2023 6:22 pm By the way, it would be preferable to define the column as type DOUBLE in the table so you don't have to deal with CASTing in the query.
Thank you. I didn't know that. I'm pretty new to this as you can probably guess. Still learning.
OpenOffice 3.4.1 on Windows 8
Post Reply