I am using Oracle SQL
This works:
SELECT
CAST(null as NUMBER(17)) AS COL1
FROM TABLE_A
UNION ALL
SELECT
TABLE_B.COL1 AS COL1
FROM TABLE_B
But this doesn't:
SELECT
CAST(null as NUMBER(17,5)) AS COL1
FROM TABLE_A
UNION ALL
SELECT
TABLE_B.COL1 AS COL1
FROM TABLE_B
It throws the following error:
Error(s) parsing SQL:
Unexpected token near *!* in the following:
CAST(null as NUMBER(17*!*,5)) AS COL1
Can I not convert to a NUMBER(17,5)?
NB: COL1 's type is NUMBER(17,5)
1) But what stops you to write this way :
SELECT
TABLE_B.COL1 AS COL1
FROM TABLE_B
UNION ALL
SELECT
CAST(null as NUMBER(17)) AS COL1
FROM TABLE_A
2)
Error(s) parsing SQL: Unexpected token near ! in the following:
Is not an Oracle error.
3)
SELECT
CAST(null as NUMBER(17,5)) AS COL1
FROM dual;
works fine in my environment
Related
I have a complex query that creates a master CTE_Table form other CTE_Tables. I want to insert the results of the master CTE_Table into a physical table. I'm using Teradata version 15.10.04.03
SELECT Failed. [3707] Syntax error, expected something like a 'SELECT' keyword or '(' or a 'TRANSACTIONTIME' keyword or a 'VALIDTIME' keyword between ')' and the 'INSERT' keyword.
DROP TABLE dbname.physicalTablename ;
CREATE MULTISET TABLE dbname.physicalTablename ,
NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
col1 INTEGER,
col2 INTEGER,
col3 INTEGER
)
NO PRIMARY INDEX ;
WITH
cteTable3 AS
( SELECT A.colA, A.colB, A.colC, B.col1, B.col2, B.col3
FROM cteTable1 A INNER JOIN cteTable2 ON (blah blah blah) ),
cteTable2 AS
( SELECT col1, col2, col3 FROM SourceTableB ),
cteTable1 AS
( SELECT colA, colB, colC FROM SourceTableA )
INSERT INTO dbname.physicalTablename
( col1, col2, col3, col4, col5, col6 )
SELECT
(C3.colA, C3.colB, C3.colC, C3.col1, C3.col2, C3.col3)
FROM cteTable3 C3 ;
While you are missing the INSERT portion of the question, I think the following might clear things up. The correct format for using a CTE in an INSERT is:
INSERT INTO <tablename>
WITH <cte> AS (SELECT...)
SELECT <fields> FROM <cte>
Consider the following:
CREATE MULTISET VOLATILE TABLE tmp AS (SELECT 'bobby' as firstname) WITH DATA ON COMMIT PRESERVE ROWS;
INSERT INTO tmp
WITH cte AS (select 'carol' as firstname)
SELECT * FROM cte;
SELECT * FROM tmp;
DROP TABLE tmp;
Have 2 tables with same columns and want to generate the difference between the tables and want to show the difference listing all columns from both tables
example:
select a.*,b.* from (
(
select a.col1,a.col2 from
(select col1, col2 from table1 minus select col1, col2 from table2) as a
)
union
(
select b.col1, b.col2 from
(select col1, col2 from table2 minus select col1, col2 from table2) as b
)
)
The result should be
a.col1 a.col2 b.col1 b.col2
a.FName a.ZipCode b.FName b.ZipCode
John <same value> Jane <same value as A>
Alpha 1234 Beta 2345
My query returns exception that it is missing R parenthesis after the 1st minus keyword
I think you are trying to find rows from table a which are missing in table b and rows in table b which are missing from table a. However, there is no point in joining these two sets. Try the following query and see if it works for you.
SELECT col1, col2, 'Missing from table 2' title
FROM
(
SELECT col1,
col2
FROM table1
MINUS
SELECT col1,
col2
FROM table2
)
UNION ALL
SELECT col1, col2, 'Missing from table 1' title
FROM
(
SELECT col1,
col2
FROM table2
MINUS
SELECT col1,
col2
FROM table1
)
I have the following query which works fine.
SELECT A.*
FROM
(
SELECT 'ABC','DEF' FROM DUMMY
UNION ALL
SELECT col1,col2 FROM DUMMY
) A;
I want to apply the like key word on the first column returned by the nested query. I tried the following:
SELECT col1, col2
FROM
(
SELECT 'ABC','DEF' FROM DUMMY
UNION ALL
SELECT col1,col2 FROM DUMMY
) A
WHERE COL1 LIKE 'A%';
But the above query did not work. I'm getting the error: ORA-00904: "COL1": invalid identifier.
The fiddle link for the same is as follows: http://sqlfiddle.com/#!4/1c54b/7
Please could anybody guide me on how I can achieve the same?
Edit: I'm also trying to get the columns from the existing table as well.
Thanks in advance.
When using union, column names or aliases have to be matched on both tables.
You can call already defined column names and aliases.
Do not forget define column alias('ABC' as col1)
SELECT col1, col2
FROM
(
SELECT 'ABC' as col1,'DEF' as col2 FROM DUMMY
UNION ALL
SELECT 'GHI' as col1,'JKL' as col2 FROM DUMMY
) A
WHERE COL1 LIKE 'A%';
I think you are trying something like;
SELECT col1, col2
FROM
(
SELECT col1,col2 FROM DUMMY
UNION ALL
SELECT col1,col2 FROM DUMMY2
) A
WHERE col1 LIKE 'A%';
Have a look at another sample from me on here
I have two tables looking something like:
TABLE_1
COL_A (int), COL_B (float), COL_C (float)
TABLE_2
COL_A (int), COL_B (float)
My query is using a UNION to put the results of these tables together, but where TABLE_2 doesn't have a COL_C, I'm looking to put something like '0'.
But I just get a 'ORA-00918: column ambiguously defined' error
How can I get around this?
You can try this
SELECT COL_A, COL_B, COL_C FROM Table1
UNION
SELECT COL_A, COL_B, 0 As COL_C FROM Table2
SELECT COL_A,COL_B,COL_C FROM TABLE_1
UNION
SELECT COL_A,COL_B,'0' AS COL_C FROM TABLE_2
You may also be able to get away with
SELECT COL_A,COL_B,COL_C FROM TABLE_1
UNION
SELECT COL_A,COL_B,'0' FROM TABLE_2
The basic idea is that both must have same number of columns of the same corresponding data types.
Suppose one table contains 3 columns and I render a query like this:
SELECT col1, col2, col3, null as col4 from table;
In this case null values are not properlly getting reflected. I got error in UNION operations stated "Runtime NULL exception". Please help on this
What distribution/version of Hadoop you are using this . I tried this on IDH and it just worked fine. Can you also paste complete error message
hive (default)> describe table4;
OK
fld1 string
fld2 string
hive (default)> select fld1, fld2, null as fld3 from table4;
A 1 NULL
B 1 NULL
C 1 NULL
Adding below as per questioner's comment,
I tried this cast , it describe now as string. you can cast as per your need
create table table6 as select fld1, fld2, cast(null as string) fld3 from table4;
hive (default)> describe table6;
OK
fld1 string
fld2 string
fld3 string
I tried this on some sample tables and it worked for me without any errors. Example query:
select * from
(select col1, NULL as col2 from table1 LIMIT 10
UNION ALL
select col1, col2 from table2LIMIT 10) q1