Solved

mySQL. SQL query. Substitute for Numeric key word.

Posted on 2016-11-02
3
34 Views
Last Modified: 2016-11-02
Let say I need to solve the problem :

With a precision of two decimal places, determine the average number of guns for the battleship classes.


SELECT AVG(numGuns+0.0)
FROM Classes
WHERE type = 'bb'

Open in new window


The query about work fine but show me result with more than 2 decimal points.

I need to write sql query that will be analog query below but will be work on mySQL server ( as far as I understand Numeric key word didn't work in mySQL server )
SELECT CAST ( AVG(numGuns+0.0) AS NUMERIC(10,2) )
FROM Classes
WHERE type = 'bb'

Open in new window


So how can I rewrite the query above in order that it'll work in mySQL server.
Thx in advance !
0
Comment
Question by:SunnyX
3 Comments
 
LVL 13

Assisted Solution

by:F Igor
F Igor earned 56 total points
ID: 41870557
You can use the FORMAT function


SELECT FORMAT( numeric_expression, decimals) FROM table

Open in new window



http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_format
0
 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal earned 444 total points
ID: 41870612
Try..

--WITH OUT ROUNDING

SELECT TRUNCATE( AVG(numGuns) ,  2 )  AS numGuns
FROM Classes
WHERE type = 'bb'

--WITH ROUNDING

SELECT ROUND( AVG(numGuns) ,  2 )  AS numGuns
FROM Classes
WHERE type = 'bb'
0
 

Author Closing Comment

by:SunnyX
ID: 41870769
thx everybody !
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now