HIVE Create Table from SubQuery - sql

this is my code:
create table sw_tmp6_gar_crm as
SELECT * FROM(
select as_fid_x_gara.dat_fine_perio as dat_fine_perio,
as_fid_x_gara.cod_soc as cod_soc,
as_fid_x_gara.cod_kto_gar as cod_kto_gar,
as_fid_x_gara.cod_fido as cod_fido,
fid.dat_delib as dat_delib,
fid.dat_scad as dat_scad
from it_soc_x_fv,
as_fid_x_gara ,
rt_fidi
where it_soc_x_fv.flg_tp_soc in ('C','N')
and as_fid_x_gara.dat_fine_perio = 2008-03-06
and as_fid_x_gara.cod_soc = it_soc_x_fv.cod_soc
and rt_fidi.dat_fine_perio = as_fid_x_gara.dat_fine_perio
and rt_fidi.cod_soc = as_fid_x_gara.cod_soc
and rt_fidi.cod_fido_tecnico = as_fid_x_gara.cod_fido
)
;
I receive the following error:
error while compiling statement: failed: parseexception line 10:9
cannot recognize input near 'it_soc_x_fv' ',' 'as_fid_x_gara' in from
source
Can you help me in that?

You need to give an alias name for the sub-query. The below one should work.
create table sw_tmp6_gar_crm as
SELECT * FROM(
select as_fid_x_gara.dat_fine_perio as dat_fine_perio,
as_fid_x_gara.cod_soc as cod_soc,
as_fid_x_gara.cod_kto_gar as cod_kto_gar,
as_fid_x_gara.cod_fido as cod_fido,
fid.dat_delib as dat_delib,
fid.dat_scad as dat_scad
from it_soc_x_fv,
as_fid_x_gara ,
rt_fidi
where it_soc_x_fv.flg_tp_soc in ('C','N')
and as_fid_x_gara.dat_fine_perio = 2008-03-06
and as_fid_x_gara.cod_soc = it_soc_x_fv.cod_soc
and rt_fidi.dat_fine_perio = as_fid_x_gara.dat_fine_perio
and rt_fidi.cod_soc = as_fid_x_gara.cod_soc
and rt_fidi.cod_fido_tecnico = as_fid_x_gara.cod_fido
) tmp
;
But as mentioned in the comments, you don't need a sub-query.

You haven't aliased the tables correctly. In your from statement you haven't mentioned any aliases for tables and in your select columns you are saying fid.dat_delib, fid.dat_scad.
But there is no fid table or alias in your query.
As mentioned earlier, there is no need of sub query, you can directly write a query with out sub query.

Related

I am trying to create a table in teradata and it doesn't work

I am trying to create a table in teradata with sql, but I keep getting the following error:
CREATE TABLE FAILED. [3707] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword or a 'SELECT' keyword or '(' between '(' and the 'WITH' keyword
My goal is to create a table that takes the maximum data, named "verwerkingdatum" in my code for every "contract_nr". Without the create table statement it worked just fine. Now I'm trying to create a table out of this. But I get the error above.
Here is my code:
create table mi_temp.beslagrek_saldo as
(SEL * FROM( WITH x AS
(
SELECT geld_contract_event_id, contract_nr, contract_soort_code,
contract_hergebruik_volgnr,
verwerking_datum,
event_dat,
valuta_code,
saldo_na_muteren_orig,
saldo_na_muteren_eur,
saldo_na_muteren_dc_ind,
valuta_datum,
geld_transactie_soort_code,
tegenrekening_nr,
tegenrekening_naam,
boek_datum,
storno_ind,
mutatie_bedrag_orig,
mutatie_bedrag_eur,
mutatie_bedrag_dc_ind,
soort_overboeking,
tegenrekening_nr_num,
automaat_transactie_type,
automaat_id,
automaat_datum,
automaat_tijd,
ROW_NUMBER() OVER (PARTITION BY contract_nr ORDER BY
verwerking_datum DESC) AS RowNum
FROM MI_VM_Ldm.vgeld_contract_event
WHERE verwerking_datum >= 1181201 AND verwerking_datum <= 1181231
)
SELECT geld_contract_event_id, contract_nr, contract_soort_code,
contract_hergebruik_volgnr,
verwerking_datum,
event_dat,
valuta_code,
saldo_na_muteren_orig,
saldo_na_muteren_eur,
saldo_na_muteren_dc_ind,
valuta_datum,
geld_transactie_soort_code,
tegenrekening_nr,
tegenrekening_naam,
boek_datum,
storno_ind,
mutatie_bedrag_orig,
mutatie_bedrag_eur,
mutatie_bedrag_dc_ind,
soort_overboeking,
tegenrekening_nr_num,
automaat_transactie_type,
automaat_id,
automaat_datum,
automaat_tijd
FROM X
WHERE RowNum = 1))
If I'm reading your post correctly, are you are trying to is filter your select so that your rownum = 1. You can just use qualify to accomplish that.
create table foo as (
SELECT geld_contract_event_id, contract_nr, contract_soort_code,
contract_hergebruik_volgnr,
verwerking_datum,
event_dat,
valuta_code,
saldo_na_muteren_orig,
saldo_na_muteren_eur,
saldo_na_muteren_dc_ind,
valuta_datum,
geld_transactie_soort_code,
tegenrekening_nr,
tegenrekening_naam,
boek_datum,
storno_ind,
mutatie_bedrag_orig,
mutatie_bedrag_eur,
mutatie_bedrag_dc_ind,
soort_overboeking,
tegenrekening_nr_num,
automaat_transactie_type,
automaat_id,
automaat_datum,
automaat_tijd
from
MI_VM_Ldm.vgeld_contract_event
WHERE verwerking_datum >= 1181201 AND verwerking_datum <= 1181231
qualify ROW_NUMBER() OVER (PARTITION BY contract_nr ORDER BY verwerking_datum DESC) =1
) with data;

'<EOF>' in subquery source in Hive query

I am running a query on Hive similar to:
SELECT *
FROM (SELECT a
FROM b
WHERE
sex = 'M'
AND degree = 'Bs'
AND age = 15
AND name LIKE 'L%'
);
the error is:
cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in subquery source
Adding a table alias for your subquery is necessary for Hive. Below I use 't1' as the alias:
SELECT *
FROM (SELECT a
FROM b
WHERE
sex = 'M'
AND degree = 'Bs'
AND age = 15
AND name LIKE 'L%'
) t1 ;
All the down-votes are unjustified. Hive often does not produce correct error and throws lazy "EOF" at you. In this case you just need to specify table alias for your sub-query. SELECT * FROM (.....) tbl_alias

Invalid SQL Syntax CLI0118E

I am trying to execute the below query and getting Invalid SQL Syntax error
. [IBM][CLI Driver] CLI0118E Invalid SQL syntax. SQLSTATE=37000(37000,-99999). Is it anything to do with driver upgrade? It was working fine a while ago. Please advice. Thanks in advance.
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as
statusDesc
from task_log a,(
select id_bus_procss, name_task , id_run,
max(dt_lst_updt) as dt_lst_updt from task_log
where id_run = '1'
and id_bus_procss = '14'
and name_task in ({0})
and dt_lst_updt >= (
select dt_evnt_sts from
sf_evntflow_sts
where id_run = '1' and
id_evntflow ='15'
and cd_evnt_sts in (''CLN'',''RTY'' )
)
group by id_bus_procss, name_task, id_run)
X
where a.dt_lst_updt = X.dt_lst_updt
if you are executing this on stored procedure , Try this may help, Remove the new line character and separate the parameters by
spaces .
Or otherwise try upgrade.......
if you try this query its better?
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as statusDesc
from task_log a,
( select b.id_bus_procss, b.name_task , b.id_run, max(b.dt_lst_updt) as dt_lst_updt
from task_log b inner join sf_evntflow_sts c on b.id_run=c.id_run and c.id_evntflow ='15'
and c.cd_evnt_sts in (''CLN'',''RTY'' ) and b.dt_lst_updt >=c.dt_evnt_sts
where b.id_run = '1' and b.id_bus_procss = '14' and b.name_task in ({0})
group by b.id_bus_procss, b.name_task, b.id_run
) as X
where a.dt_lst_updt = X.dt_lst_updt

Oracle SQL - Unable to Update when using WITH statement

I am trying to update a column based on virtual dataset created via a WITH statement. I have simplified the statement as much as possible to get to the root issue. It appears that UPDATE does not work when using a WITH statement, but I can't believe this is accurate. The error I am getting is
ORA-00928: missing SELECT keyword
Here is my SQL statement.
with TEMP1 as (
select NN_NAME
from SMB.ACCOUNTS
)
update SALES_PLAY_MATRIX_WORKING
set FY16_FOCUS = 'Y' where NN_NAME in ( select TEMP1.NN_NAME from TEMP1)
If I convert the 2nd portion of the statement to just a pure SELECT, it works...
with TEMP1 as (
select NN_NAME
from SMB.ACCOUNTS
)
(
select TEMP1.NN_NAME
from TEMP1)
In Oracle, with does not go just at the beginning of a query. It can go before any select. So:
update SALES_PLAY_MATRIX_WORKING
set FY16_FOCUS = 'Y'
where NN_NAME in (
with TEMP1 as (
select NN_NAME
from SMB.ACCOUNTS
)
select TEMP1.NN_NAME
from TEMP1
);
I will suggest go for nested query that may solve your issue
UPDATE SALES_PLAY_MATRIX_WORKING
SET FY16_FOCUS = 'Y'
WHERE NN_NAME IN ( SELECT NN_NAME FROM SMB.ACCOUNTS)
You could use a merge statement:
MERGE INTO SALES_PLAY_MATRIX_WORKING dst
USING ( SELECT NN_NAME FROM SMB.ACCOUNTS ) src
ON ( dst.NN_NAME = src.NN_NAME )
WHEN MATCHED THEN
UPDATE SET FY16_FOCUS = 'Y';
or more simply:
MERGE INTO SALES_PLAY_MATRIX_WORKING dst
USING SMB.ACCOUNTS src
ON ( dst.NN_NAME = src.NN_NAME )
WHEN MATCHED THEN
UPDATE SET FY16_FOCUS = 'Y';

Oracle SQL syntax error (missing right parenthesis)

I don't understand why this provokes a syntax error (missing right parenthesis):
UPDATE table
SET doc =
(SELECT 'table-2844-doc' || SUBSTR(doc_file, INSTR(doc_file, '.', -1))
FROM docvers
WHERE (docvers.table_name = 'other_table'
AND docvers.field_name = 'doc')
AND ROWNUM = 1
ORDER BY VERSION DESC)
WHERE table_id = 2844
This looks right to me, does get executed correctly in SQL Server, and is similar to requests found, for example, in Oracle SQL: Update a table with data from another table.
Any tip?
Do it like this:
UPDATE table
SET doc = (
select r.myval
from (
SELECT 'table-2844-doc' || SUBSTR(doc_file, INSTR(doc_file, '.', -1)) myval, ROWNUM RN
FROM docvers
WHERE docvers.table_name = 'other_table'
AND docvers.field_name = 'doc'
ORDER BY VERSION DESC
) r
where r.RN = 1
)
WHERE table_id = 2844
Select the data set first including the ROWNUM, then select from that data set the first row.