I have a qty column in my table, and i'm trying to sum the values. The field type is varchar(20). The sum of qty below should add up to exactly 0. It's a mix of negative and positive decimals.
exhibit A (screenshot)
When i perform the sum below, i'm getting a tonne of decimals instead of 0. I'm assuming this is a datatype issue. What's the best way to work around this?
exhibit B (screenshot)
You should not be storing numeric data as a string but if you do, then you will need to cast() it to apply a SUM() aggregate to it:
SELECT SUM(CAST(yourcolumn AS DECIMAL(10, 2)))
FROM yourtable
So your query will be:
select sum(cast(qty as DECIMAL(10, 2)))
from inventory i
where i.refDocNum = 485
and i.refApp = 'WO'
and i.type in (20, 21)
Related
I am trying to find max invoice:
SELECT IFNULL(MAX(SUBSTRING_INDEX(invoice,'I', -1)) + 1, 1) AS invoice
FROM sales
SQL Fiddle
When I run this SQL query, it can not count more than 10.
invoice
20221026P1I1
20221026P1I2
20221026P1I3
20221026P1I4
20221026P1I5
20221026P1I6
20221026P1I7
20221026P1I8
20221026P1I9
20221026P1I10
20221026P1I11
20221026P1I12
I am trying to find max invoice 12 + 1 = 13
Your use of SUBSTRING_INDEX() is correct, however you should cast the string value to a bona fide integer:
SELECT COALESCE(MAX(CAST(SUBSTRING_INDEX(invoice, 'I', -1) AS UNSIGNED)), 1) AS invoice
FROM sales;
The problem with trying to find the max of the text substrings themselves is that text numbers sort lexicographically, e.g.
1
10
11
2
23
But this isn't the behavior you want, you want the numeric maximum. Hence we should cast these substrings and then compare.
Side note: You could have avoided this problem entirely by maintaining a pure numeric invoice number column. You may want to change your table design to include such a column.
I am generating a query table using SQL SELECT as below. This formats the numbers in the column 'Free Cashflow (USD mm)' with 2 digits, but the resulting column data type is 'text'.
How can I instead assign a number data type, e.g. 'float' with 2 digits, to the new column 'Free Cashflow (USD mm)' ?
SELECT
(format(("fcf" / "fxusd") / 1000000, 2) as 'Free Cashflow (USD mm)'
FROM "SF1"
You can CAST it to DECIMAL. Note that for values >= 1000, FORMAT will insert a , in the result, which will prevent the CAST from working correctly. Since FORMAT effectively just ROUNDs the value to the given number of decimal places, you can use ROUND instead to resolve that problem:
SELECT
CAST(ROUND(("fcf" / "fxusd") / 1000000, 2) AS DECIMAL(9,2)) as 'Free Cashflow (USD mm)'
FROM "SF1"
Demo on dbfiddle
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 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
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)