I am trying to get the percent and it just shows up as zero. I want to show two decimal places such as 0.65
Here is a piece of the query I am selecting:
select count(numbers)/count(othernumbers) decimal(3,2) as"rate"
if I use this it shows up as 0 and gets rid of the rest
select count(numbers)/count(othernumbers) as"rate"
need to convert both of your "count(numbers)" and "count(othernumbers)" to decimal also.
select convert(decimal(5,2), count(numbers))
/
convert(decimal(5,2), count(othernumbers))
as"rate"
Here's an example that works in SSMS (Sql Server):
select Convert(decimal(3,2), convert(decimal(4,2), 1.0) / convert(decimal(4,2), 10.0)) as [rate]
You have to use this convert value to Decimal, This will give you the decimal till two places
SELECT CONVERT( DECIMAL(10,2),
( CONVERT(DECIMAL(10,2), numbers) /
CONVERT(DECIMAL(10,2), othernumbers) ) ) AS rate
SELECT FORMAT(this / that, 2) as rate
Meanwhile, I question whether COUNT(numbers) is what you want. That counts how many rows have a non-NULL value in the column numbers.
Also, fraction is usually more like x / (x+y) -- meaning the fraction of the total (x+y) that is x.
A "percentage" needs 100* somewhere. 13/20 is the fraction 0.65 or the percentage 65.00 .
Have you checked ceil(), floor() and round()
If you want without rounding anything
SELECT TRUNCATE(count(numbers)/count(othernumbers),2) as "rate"
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_truncate
select convert(decimal(11, 2), 102)
select convert(decimal(11, 2), 102.5)
select convert(decimal(11, 2), 102.74)
select convert(decimal(11, 2), 102.745)
Results:
102.00;
102.50;
102.74;
102.75
Related
I want to use FORMAT on SUM to produce a currency style format with commas and no decimal places. I'm trying the following using MySQL 5.7:
SELECT FORMAT(SUM(x.07_17 / fx.07_17), 0) AS total.....
The problem is this changes the data drastically. Without the format, I get the correct result of SUM 350914 but with the format, in place, I get 350, so just the first 3 numbers.
What is it I'm doing wrong?
You can check the documentation to see how each parameters works for the FORMAT function https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_format
This query shows you different style to display your data (Sql fiddle example)
SELECT SUM(c1 / c2) AS total1
, CONCAT(FORMAT(SUM(c1 / c2), 3, 'fr_FR'), ' €') AS total2
, CONCAT('$', FORMAT(SUM(c1 / c2), 3)) total3
FROM ( SELECT 148277 c1
, 5.1561 c2 ) t
I need an SQL query , with multiple AND's. Let me explain with an example,
For example I want to search in a database for a property , who's price is greater than 1000 and less than 2000 (price is between 1000-2000), and its area is greater than 1000 sqft. and less than 2000 sq ft. (area is between 1000-2000).
So i was guessing that the query could be,
SELECT *
FROM table_name
WHERE (price>1000 AND price<2000) AND (area>1000 AND area<2000)
this is something i need ! Thank you
Your original query looks fine to me, but you can also use BETWEEN if you like, try this:
SELECT * FROM table_name WHERE (price BETWEEN 1001 AND 2000) AND (area BETWEEN 1001 AND 2000);
expr BETWEEN min AND max
If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments
Use between instead of and
SELECT * FROM table_name WHERE (price between 1001 AND 1999) AND (area between 1001 AND 1999)
Use this, i think it will solve your problem. It works for me:
SELECT * FROM table_name WHERE price and area BETWEEN 1001 and 1999.
If we have same values of the parameters, then we can add the condition with and all the parameters.
I have a variance report query here I need the 'Variance' to not have 10 decimal points in the Variance Column. What is the most convenient way to round Variance results to the 100th?
WITH A AS
(
select
A.FACTORY,
A.JOB_NUMBER,
A.PROCESS_STAGE,
A.PART_CODE,
B.PART_DESC_1,
A.INPUT_QTY_STD,
A.QUANTITY_INPUT,
A.QUANTITY_OUTSTANDING,
A.INPUT_QTY_ACTUAL,
(A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)) as variance,
A.ACTUAL_CLOSE_DATE
from
(select * from [man_prod].[dbo].[JOB_STAGE_LINES]
where JOB_NUMBER in (select JOB_NUMBER from JOB_OUTPUTS where
BF_QTY_ACTUAL<>0
and ABS(DATEDIFF(HOUR,ACTUAL_CLOSE_DATE,GETDATE())) < 12 and STATUS_FLAG='C'
)) A
join fin_prod.dbo.PRODUCT_MASTER B
ON A.PART_CODE=B.PART_CODE
WHERE
A.INPUT_QTY_STD<>0 and
A.QUANTITY_OUTSTANDING <>0
)
SELECT * FROM A WHERE A.variance >10.000000 OR A.variance <-10
order by PROCESS_STAGE asc ,PART_CODE asc, variance desc ;
The Variance column comes out at 00.0000000000 i need it to display 00.000 or 00.000000
Help is greatly appreciated
Use the MySQL ROUND() function, the second argument is the number of decimal places if it is positive.
ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
In this example if the value is 0.0000000000 it would be rounded to 3 decimal places, or 0.000.
You can use the TRUNCATE option:
TRUNCATE((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
or use the ROUND if you are looking for rounding(as suggested by doublesharp)
ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
Using Convert to convert it to a decimal of the desired length is what i prefer when i am not actually rounding the value, just formatting.
CONVERT(DECIMAL(10,3),10000)
I created a field called 'hours_spent' in MySQL using the decimal datatype to store time. The values are stored like this 1.30, 2.30 etc... (for 1hr30min, 2hr30min).
I want to calculate the sum of various time values.
The sum of time is not what I expected: 1.30 + 2.30 = 3.60, whereas I expected 4.00.
I used the SUM function in MySQL to count the hours_spent field. If the values are 0.30 + 1.50 = 1.80, whereas I expected 2.20.
My first mistake was to use the decimal type instead of the time datatype, but I cannot change datatype.
So, is there any way to sum the time values and get result as I expect?
Thanks
I prepared you a demo at sqlfiddle, you can try it there if you want:
http://www.sqlfiddle.com/#!2/c9afc/2
Here are the query samples:
select #indexer:=instr(dateasdecimal, '.')
, left(dateasdecimal, #indexer-1) * 60 + substr(dateasdecimal, #indexer+1) as totalMinutes
from testtable;
select #indexer:=instr(dateasdecimal, '.')
, sum(left(dateasdecimal, #indexer-1) * 60 + substr(dateasdecimal, #indexer+1)) as totalMinutes
from testtable;
Note: Please don't forget to accept answers to your questions:
https://meta.stackexchange.com/a/65088/200585
To convert a decimal into seconds, you could use this:
truncate(hours_spent,0)*60+(hours_spent-truncate(hours_spent,0))*100
and then you can do the sums easily. Then you can convert back seconds to the decimal format with this:
truncate(seconds/60,0)+truncate(mod(seconds, 60)/100,2)
You could always turn the decimals into a string, cast as time, then sum that time using time_to_sec and produce a formatted time with sec_to_time. Of course, it would be much better to be storing those times a different way, even if it involves converting the entire dataset.
SELECT sec_to_time(sum(time_to_sec(goodTime))) FROM (
SELECT CAST(badTime AS TIME) AS goodTime FROM (
SELECT REPLACE(badTime, '.', ':') AS badTime FROM (
SELECT CAST(badTime AS dec(4,2)) AS badTime FROM (
SELECT 1.3 AS badTime
UNION select 2.3
) z
) y
) x
) w
Suppose I have INT column, and I am using -1 to signify that no data was available at the time of the INSERT. I'd like to get an AVG of all values of this column that are 0 or larger.
Is this possible?
Thanks.
I forgot to mention that I'm doing this alongside other AVG's, so it's select avg(a), avg(b), avg(d) from tab; ... so I can't use b>0... because the others need to use the row data regardless of this one column's data being -1.
It occurs to me though that I could augment the AVG result e.g. if it would normally be (4 + 5 + -1 + -1 + 6) / 5. But if I know how many -1's there are I could "fix" the result to exclude them.
This might help:
If you want to ignore the -1 values from the average:
SELECT AVG(`a`), AVG(IF(`b` > -1, `b`, NULL)), AVG(`c`) FROM `t`;
If you want to consider the -1 values in the average:
SELECT AVG(`a`), AVG(IF(`b` > -1, `b`, 0)), AVG(`c`) FROM `t`;
I've assumed dummy column- and table- names and assumed column b as the one for which you want to consider only values >= 0. Please feel free to put in names as per your schema.
SELECT AVG(`field`) FROM `table` WHERE `field` >= 0
Please use this:
SELECT AVG(Column),
SUM(IF(Column>0))/COUNT(IF(Column>0))
FROM Table
Could you do something as such
SUM(column) / COUNT(*) as Average FROM 'table' WHERE 'column' >= 0