Decimal place in select statement sql - sql

I have a perfectly working select statement which I have been using. However I have noticed that it does not return any more than zero decimal places. Which I thought Excel had been factoring out, when copied across, but its not.
I have tried to change the format, but I still get zero decimal places. I have reviewed all posts on here, but as I am using a Case statement as well it is not simple to include.
Round(AVG(case when [Duration] > 0 then [Duration] end), 3) as AVGLOS,
Any help welcomed as always.

Data type is an int
That is your problem.
The avg of integer values is always integer:
declare #t table (col int);
insert into #t values (2), (3);
select avg(col)
from #t;
----
2
So you should manipulate decimals, not integers like this:
declare #t table (col int);
insert into #t values (2), (3);
select avg(col * 1.)
from #t;
---
2.500000
So in your case just use this:
Round(AVG(case when [Duration] > 0 then [Duration]* 1. end), 3) as AVGLOS

Related

Show data where the value is after the decimal point

I would like to do (select value from table where...) where the value of a given experiment is not an integer, but it is decimal
You can use FLOOR function to do this. It will round up your numbers, so you can pick only this that are not integers.
create table #t (i decimal(12,6))
insert into #t values (1), (1.1)
select * from #t where FLOOR(i) <> i
You can do as
CREATE TABLE T( Val DECIMAL(10, 2));
INSERT INTO T VALUES
(10.10), (10);
SELECT *
FROM T
WHERE CAST(Val AS INT) <> Val
Returns: 10.10

hive/impala - inserted decimal showed as null

I created table as
create table test ( x decimal(5,2))
then I tried to insert a value
insert into test values ( cast( 1000.2 as decimal(5,2) ) );
insert into test values ( cast('2000.3' as decimal(5,2) ) );
insert into test values ( cast('3000,4' as decimal(5,2) ) );
but in the end select * from test; is returning 3x NULL value.
What I'm doing wrong ? I can't believe any of above mentioned statements doesn't work.
I'm using impala at recent Cloudera quickstart VM.
Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2
select cast(1000.2 as decimal(5,1)) as a,
cast(1000.2 as decimal(5,2)) as b,
cast(1000.2 as decimal(6,2)) as c
will give you
a 1000.2
b NULL
c 1000.2

TSQL Error converting data type varchar to float. Warning: Null value is eliminated by an aggregate or other SET operation

I am getting the above error, and it hits on my Insert on line 95:
"INSERT INTO #total"
I added the columns topic_name varchar(100), samplegroup_name varchar (50), subtopic varchar(50)
from a working query, and that caused the error. None of these columns are Floats, the above types are correct. If I comment out those columns then the query runs fine, so I'm not really sure what to do from here.
You have an issue with the order of the values you try to insert into #total.
Look at the definition of #total:
DECLARE #total TABLE (
item_id varchar (30),
item_dbkey int,
status varchar (7),
nT float,
nY float ,
sigmaX float,
sigmaSqX float,
sigmaY float,
topic_name varchar(100),
samplegroup_name varchar (50),
subtopic varchar(50)
)
and now note the insertion:
INSERT INTO #total
SELECT
Item_id,
CTR.item_dbkey,
TIP.Status,
tpta.topic_name, -- this tries to go into nT
ttisg.samplegroup_name, -- this tries to go into nY
TIP.subtopic, -- this tries to go into sigmaX
COUNT(CTR.item_dbkey), -- Number of exams which had this item_dbkey
COUNT(CASE WHEN iop.weightage >= 1 THEN CTR.item_dbkey END) C, -- How many times was this answered correctly
SUM (CTAP.raw_score ) AS totalscore, -- Sum of all scores (scaled or raw)
SUM (SQUARE( CTAP.raw_score ) ) AS totalscore2, -- Sum of the Square of all scores (scaled or raw)
SUM (CASE WHEN iop.weightage >= 1 THEN ( CTAP.raw_score ) END ) AS totalscorecorr -- When answered correctly, sum the scores (scaled or raw)
topic_name, for example, cannot be inserted into nT column!

Tally Table in SQL

I want to create a bunch of data with Tally table in SQL (sql2008) and definitely need help.
First of all, I have this table which contains 2 columns.
{
AcctNum (nchar(30), null),
DataInfo (nchar(745), null)
}
While I don't care the data in the DataInfo column, I do want to add about 10k of row into the table with unique AcctNum on each row.
The problem though is I need to keep the length of the data in both column. For example, AcctNum column looks like "400000000000001 ". how do I increment the number while keep the "blank space"?
Not sure if I make much sense here, but please let me know and I will try to explain more, thanks!
Using a recursive common table expression :
-- set up a table variable for demo purpose
declare #t table (AcctNum nchar(30) null, DataInfo nchar(745) null);
-- insert the starting value
insert #t values ('400000000000001', null);
-- run the cte to generate the sequence
with cte (acctnum, num) as (
select acctnum, cast(acctnum as bigint) + 1 num -- starting value
from #t
union all
select acctnum, num+1 from cte
where num < cast(acctnum as bigint) + 10000 -- stopping value
)
-- insert data sequence into the table
insert #t (AcctNum, DataInfo)
select num, null from cte
option (maxrecursion 10000);
select * from #t;
The table variable #t will now contain acctnum 400000000000001 -> 400000000010001 as a contiguous sequence.

Sum of Columns which Contain Differennt Data Type

I am trying to sum the cost value but the problem I believe is in my cost column there are not set and null values beside actual values. So Sum(t.cost) gives me wrong sum and coalesce(t.cost) gives me the wrong sum. What needs to be done to get the right sum for each student?
I presume that your "cost" column is not numeric ?!? Whatever...
declare #t table (id int, cost varchar(10) null)
insert into #t values (1,'123.456')
insert into #t values (2,'456.789')
insert into #t values (3,null)
insert into #t values (3,'notset')
select sum(cast(nullif(cost,'notset') as money)) from #t