function to get only the integer [duplicate] - sql-server

This question already has answers here:
Issue with Round function in ssrs
(3 answers)
Closed 8 years ago.
What function I have to use to get as a Result 1 in the following expression, in SQL Server or SSRS please
select ROUND((3 - (4 * 0.32)), 1) = 1.70

FLOOR is the function that you need: http://msdn.microsoft.com/en-us/library/ms178531.aspx
select FLOOR(ROUND((3 - (4 * 0.32)), 1))

ROUND((3 - (4 * 0.32)), 0)
=> 2
ROUND()
If that is not the desired result then you probably want to use FLOOR

Related

Percentage value not being shown in sql server [duplicate]

This question already has answers here:
SQL Server, division returns zero
(6 answers)
Division of integers returns 0
(2 answers)
Closed 4 months ago.
I want to calculate the percentage change of some statistics id using SQLSEVER.
I want to calculate %change using = ((curr-prev)/prev) * 100
My %change expected there was 2.63% since (400008/15189461)* 100 but I am getting result as 0.00%.
select
c.poscount as curr,
p.poscount as 'prev',
(p.PosCount - c.PosCount) 'difference',
CONVERT(
decimal(2, 2),
(p.PosCount - c.PosCount)/ NULLIF(p.PosCount, 0)
)   as   'Poscountchange'
from
(
select
*
from
Goker.dbo.LocalStatisticsDetail_daily
where
statisticid = '13527'
)   c
inner join (
select
*
from
Goker.dbo.LocalStatisticsDetail_daily
where
statisticid = '13373'
) p on c.fieldname = p.fieldname
Why I am getting 0% as percentage change? I didn't wanted to change to decimal at first and my result was 0% before converting it into decimal.

How to select the item that has the greatest value in dataframe ? In Pyspark [duplicate]

This question already has answers here:
Find maximum row per group in Spark DataFrame
(2 answers)
Closed 1 year ago.
I would like to select the item that has the greatest value. For exemple in this table I would like to select MAC09
Identifiant
Val
MAC26
36
MAC10
9
MAC02
2
MAC32
11
MAC09
37
MAC28
10
there are several way of doing it, here is a solution using a rank
from pyspark.sql import functions as F, Window
df.withColumn("rnk", F.rank().over(Window.orderBy(F.col("Val").desc()))).where(
"rnk = 1"
).drop("rnk").show()
+-----------+---+
|Identifiant|Val|
+-----------+---+
| MAC09| 37|
+-----------+---+

Sql Server is rounding the value while calculating average [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Decimal values in SQL for dividing results
(6 answers)
Closed 2 years ago.
I am trying to calculate average and the apply CEILING on the average, but somehow when i calculate average it is always rounding off the value.
DECLARE #AverageStudents INT
SELECT #AverageStudents = CEILING((SELECT #TotalUsersInAllScools + #TotalUsersInAllColleges AS FLOAT) + (SELECT #TotalSchools + #TotalUsersColleges AS FLOAT))
SELECT #AverageStudents AS 'AverageStudents'
In my case #TotalUsersInAllScools + #TotalUsersInAllColleges = 875 , and #TotalSchools + #TotalUsersColleges = 216
875/216 = 4.0509 and CEILING of it should give me output 5 but its giving 4.
What i observed is #TotalUsersInAllScools + #TotalUsersInAllColleges AS FLOAT) + (SELECT #TotalSchools + #TotalUsersColleges AS FLOAT)is giving output as 4

Keep only first two digits after decimal with no rounding [duplicate]

This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 4 years ago.
i would like to keep only first two digits after decimal but i don't want to round or convert the value.
For example:
143,655 -> 143.65
547934,945 -> 547934,94
Converting or rounding the values doesn't work, it modifies the values.
use the ROUND() with truncate function
https://learn.microsoft.com/en-us/sql/t-sql/functions/round-transact-sql
select round(143.655, 2, 1),
round(547934.945, 2, 1)
the last parameter, when non-zero, it will truncate

Not returning Decimal [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 7 years ago.
Can anyone tell me why this is not returning a decimal- only Zeroes.
Set PercentComplete = (CSCCalled + HospCalled + NoCalls)/(TotalInventory)
CSCCalled is an Integer
HospCalled is an Integer
NoCalls is an Integer
TotalInventory is an Integer
In the TempTable- PercentComplete is Decimal(9,3)
The results end up either being a 1 or 0, not a decimal. Does anyone have any suggestions?
Thanks,
Scott
You can cast one value to float in order to get a float result or you can add a float constant to promote every integer into a float. Try one of the following:
Set PercentComplete = 1.0 * (CSCCalled + HospCalled + NoCalls)/(TotalInventory)
or
Set PercentComplete = (CAST(CSCCalled AS FLOAT) + HospCalled + NoCalls)/(TotalInventory)

Resources