In hive Decimal(10,4) is rounding off on insert into select - hive

When i do insert into a table1 select * from table2 ,it rounds up the values. Can anyone help ?

Related

Getting 0 instead of decimal when dividing a int by 100 in SQL

I have a column Column1 of an int type in myTable with a value of, let's say, 25.
When inserting cast(Column1 as decimal)/100 into a temp table #tempTbl as following
create table #tempTbl
(
Column1 decimal
)
Insert into #tempTbl
select cast(Column1 as decimal)/100
from myTable
I see 0 in the Column1 of a temp table instead of 0.25
When I try select cast(25 as decimal)/100, I'm getting 0.25
What am I doing wrong?
I would suggest:
select Column1 / 100.0
SQL Server does integer division (as you have discovered). Just including a decimal place in a constant or multiplying by 1.0 usually resolves the issue.
Try
select cast(Column1 as decimal)/100.0
If execute
select 25/100.0;
It returns
(No column name)
0.250000
Please see this code. The decimel length was specified in the DDL of the temp table
create table #tempTbl
(
Column1 decimal(8,2)
)
Insert into #tempTbl
select 25
select Column1/100.0 from #tempTbl;
Output
0.2500000

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

UNIONALL Performance

The UNIONALL is taking more time, below is example of query. as mentioned below table T1 having 4 records only, but in my query the T1 having almost 1.5 million records.
Is there any way to tune below query means instead unionall can we use any condition. Thanks!
CREATE TABLE T1 (ID INT, FROM_KEY INT,TO_KEY INT, IS_STATUS BIT)
INSERT INTO T1 VALUES(1, 50001,50002, 1)
INSERT INTO T1 VALUES(2, 50003,50004, 1)
INSERT INTO T1 VALUES(3, 50005,50006, 1)
INSERT INTO T1 VALUES(4, 50007,50008, 1)
DECLARE #KEY INT = 50002
SELECT TO_KEY FROM T1 WHERE TO_KEY=#KEY AND IS_STATUS=1
UNION ALL
SELECT FROM_KEY FROM T1 WHERE FROM_KEY=#KEY AND IS_STATUS=1
Can be simplified as:
SELECT #KEY
FROM T1
WHERE #KEY IN (TO_KEY, FROM_KEY)
AND IS_STATUS = 1
(Will only return the same row once if both to_key and from_key are equal to #key at the same time, while the UNION ALL query would return that row twice.)
Single pass query could be more optimal:
SELECT CASE WHEN N=0 THEN FROM_KEY ELSE TO_KEY END
FROM (SELECT * FROM T1 WHERE #KEY in (TO_KEY, FROM_KEY) AND IS_STATUS=1) AS A
JOIN (SELECT 0 N UNION ALL SELECT 1) AS B
ON (N=0 AND TO_KEY=#KEY) OR (N=1 AND FROM_KEY=#KEY)
But in general, if you wish to get help with query optimization, you have to provide much more information about sql server brand, version, data size, indexes and so on.

Get the wrong line ID in ms sql

I have an INSERT statment wich inserts large amount of data into tableA from tableB.
Here is a very simple code example:
INSERT [dbo].[tableA]
SELECT field1 [field_1]
FROM [dbo].[tableB]
WHERE [codeID] IN (SELECT [codeID] FROM #tempTable WHERE RecordMarker = 1)
There is a temporary table wich holds codeIDs (at least 1 or more) needed to insert to tableA.
But there would be incorrent data in tableB what cannot be inserted into tableA. For example an numberic(30,2) field cannot map to numeric(13,2). In this case I get an excetpion and the statement has been terminated.
How can I get the CodeID or the wrong line number in tableB if I get an error? Now I have just the error message but no line number.
For example:
Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting numeric to data type numeric.
The statement has been terminated.
EDIT: There are more than one field in the table with different field types. So the numeric type is just an example.
Please try the following:
INSERT [dbo].[tableA]
SELECT field1 [field_1]
FROM [dbo].[tableB]
WHERE [codeID] IN (SELECT [codeID] FROM #tempTable WHERE RecordMarker = 1)
AND [codeID] <= 9999999999999.99;
INSERT ErrorLog
SELECT *
FROM [dbo].[tableB]
WHERE [codeID] > 9999999999999.99;
If you know the type of the destination field you're having the issue with, in this case a numeric of (13,2) precision, you can run a SELECT with a TRY_CONVERT on the potential problem field against your temp table and filter for NULL results. You could add a WHERE clause to your insert statement if you wanted to ensure that it would run successfully and not try to insert those "bad" rows.
CREATE TABLE #t (x NUMERIC(30,2),field2 varchar(10))
INSERT INTO #t
SELECT 123456789.23,'x'
UNION
SELECT 12345678901212343.23,'y'
UNION
SELECT 12345678923523523235.23,'z'
UNION
SELECT 42.0, 'a'
SELECT *, TRY_CONVERT(NUMERIC(13,2),x,1) [Converted to numeric(13,2)] FROM #t
Reference: http://msdn.microsoft.com/en-us/library/hh230993.aspx

SQL query construction issue

There two tables:
Table1
field1 | field2
Table2
field1
“string1”
“string2”
I need to insert concatenation of table2.field1 values into table1, so it looks like
insert into table1(field1, field2) values (1, “string1string2”);
How can I do it? Is there any SQL-standard way to do it?
PS: string1 and string2 are values of the field1 column.
PPS: the main subtask of my question is, how can I get the result of select query into one row? All examples I've seen just use concatenation, but in all your examples SELECT subquery does not return string concatenation for all values of the table2.field1 column.
There is no ANSI standard SQL way to do this.
But in MySQL you can use GROUP_CONCAT
insert into table1 ( field1, field2 )
select 1, group_concat(field1) from table2
In SQL Server 2005 and later you can use XML PATH,
insert into table1 ( field1, field2 )
select 1, (select field1 from table2
for xml path(''), type).value('.','nvarchar(max)')
In Oracle, you can refer to Stack Overflow question How can I combine multiple rows into a comma-delimited list in Oracle?.
INSERT INTO TABLE1 (FIELD1, FILED2) VALUES (1, CONCAT("string1", "string2"))
try this :
insert into table1(field1, field2)
select table2.field1, table2.string1 || table2.string2 from table2;
You can add a where clause to the query to select only some entries from table2 :
insert into table1(field1, field2)
select table2.field1, table2.string1 || table2.string2 from table2
where table2.field = 'whatever';
I'd try with
insert table1 select field1, string1+string2 from table2
tested with MSSQL Server 2008
create table #t1 (n int, s varchar(200))
create table #t2 (n int, s1 varchar(100), s2 varchar(100))
insert #t2 values (1, 'one', 'two') -- worked without into ???
insert #t2 values (2, 'three', 'four') -- worked without into ???
insert #t1 select n, s1+s2 from #t2 -- worked without into ???
select * from #t1
drop table #t1
drop table #t2
After the edit:
No, if you have no way to identify the lines in table2 and sort them the way you want it is impossible. Remember that, in the absence of a order by in the SQL statement, lines can be returned in any order whatsoever
Assuming this is SQL server,
Insert into table1 (field1, field2)
select field1, string1 + string2
from table2
In oracle you will do it as -
Insert into table1 (field1, field2)
select field1, string1 || string2
from table2