SQL syntax or database constrictions? - sql

I have 2 tables that have each a column MNR. I want to join them with this column.
The following two SQL statements fail. The last one shows that my date format is working (with changed session format). DB is Oracle.
Can someone please tell me what I'm doing wrong ? And what do I call this join ?
// fails
select a.CREATEDATE, a.BELEGNRRECH, a.MNR, a.UTNR, a.KTXT, b.ANR
from INFOR.RELFBR as a, INFOR.RELXDB as b
where (a.SAINT = '90') and (a.MNR = b.MNR) and (b.SAINT = '10')
and (a.CREATEDATE >= '01.01.2014 00:00:00')
order by a.CREATEDATE
// fails as well
select a.CREATEDATE, a.BELEGNRRECH, a.MNR, a.UTNR, a.KTXT, b.ANR
from INFOR.RELFBR as a, INFOR.RELXDB as b
where (a.SAINT = '90') and (a.MNR = b.MNR) and (b.SAINT = '10')
order by a.CREATEDATE
// all fine
select CREATEDATE, MNR
from INFOR.RELFBR
where (CREATEDATE >= '01.01.2014 00:00:00')
order by CREATEDATE
Failing error is ORA-00933: SQL command not properly ended
After removing "order by ..." same error occurs.

Exclude AS from table aliasing - AS can be used with selection list but not in FROM clause
select a.CREATEDATE, a.BELEGNRRECH, a.MNR, a.UTNR, a.KTXT, b.ANR
from INFOR.RELFBR a, INFOR.RELXDB b
where (a.SAINT = '90') and (a.MNR = b.MNR) and (b.SAINT = '10')
and (a.CREATEDATE >= '01.01.2014 00:00:00')
order by a.CREATEDATE
Example:
SQL> select * from t_dummy t;
X
-----------------------
9.0000
SQL> select * from t_dummy as t;
select * from t_dummy as t
*
error in line 1:
ORA-00933: SQL command not properly ended
Documentation:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm#i2126863

Related

how to run a different select statement based on condition in Hive SQL

I would like to know how to run a different select statement based on condition in Hive SQL.
The following query does not work but throws an error.
Error while compiling statement: FAILED: ParseException line 4:2
cannot recognize input near '(' 'SELECT' '1' in expression
specification
SELECT
CASE WHEN '${UN}'!= '' THEN
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in ('${UN}')
)
ELSE
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in (
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
)
)
END
Use UNION ALL with your queries + add conditions for switching corresponding query:
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' and '${D2}')
and t.un in ('${UN}')
and '${UN}'!= '' --switching condition
union all
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' AND '${D2}')
and t.un in
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
and '${UN}'= '' --switching condition

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

HIVE: Error in GROUP BY Key

hive -e "select a.EMP_ID,
count(distinct c.SERIAL_NBR) as NUM_CURRENT_EMP,
count(distinct c.SERIAL_NBR)/count(distinct a.SERIAL_NBR) as DISTINCT_EMP
from ORDERS_COMBINED_EMPLOYEES as a
inner join ORDERS_EMPLOYEE_STATS as b
on a.CPP_ID = b.CPP_ID
left join ( select SERIAL_NBR, MIN(TRAN_DT) as TRAN_DT
from EMP_TXNS
group by SERIAL_NBR
) c
on c.SERIAL_NBR = a.SERIAL_NBR
where c.TRAN_DT > a.LAST_TXN_DT
group by a.EMP_ID
having (
(NUM_CURRENT_EMP >= 25 and DISTINCT_EMP > 0.01)
) ; " > EMPLOYEE_ORDERS.txt
Getting error message,
"FAILED: SemanticException [Error 10025]: Line 15:31 Expression not in GROUP BY key '0.01'".
When I ran the same query with just one condition in HAVING clause as NUM_CURRENT_EMP >= 25, the query ran fine without any issues. NUM_CURRENT_EMP is a int type and DISTINCT_EMP is float in the table where I am trying to insert the results. Breaking my head.
Any help is appreciated.
What happens if you replace the aliases in the having with the expressions that define them?
having count(distinct c.SERIAL_NBR) >= 25 and
count(distinct c.SERIAL_NBR)/count(distinct a.SERIAL_NBR) > 0.01

Unable to fetch data from a view that accesses data over db link

I have a query to fetch data from two tables over a db link as below:
SELECT a.ID, a.NAME, b.address
FROM table1#dblink a, table2#dblink b
WHERE a.ID = b.ID;
This works perfectly fine. Then I create a view for this as below:
CREATE VIEW myview
AS
SELECT a.ID, a.NAME, b.address
FROM table1#dblink a, table2#dblink b
WHERE a.ID = b.ID;
View is created successfully. But when I select data from the view as below:
SELECT *
FROM myview
I get error like:
ORA-00942: table or view does not exist
ORA-02063: preceding line from MYLINK
What could probably be the issue?
EDIT:
Tried different methods and ended up getting different error. Here I am posting the exact query and the latest error:
CREATE OR REPLACE VIEW plan_view
AS
WITH plan_name AS
(SELECT fcr.argument1 AS plan_name, fcr.request_id AS request_id
FROM apps.fnd_concurrent_requests#dblink fcr
WHERE argument1 IN
('E10', 'E20', 'E40', 'E60L', 'EDC', 'PS1', 'S')
CONNECT BY PRIOR fcr.request_id = fcr.parent_request_id
START WITH request_id = -- '58043920'
(SELECT MAX (request_id) AS request_id
FROM apps.fnd_concurrent_requests#dblink
WHERE description = 'Mail Program'
AND actual_start_date >=
TO_DATE ( TO_CHAR (TRUNC (SYSDATE - 1),
'mm-dd-yyyy'
)
|| '05:00:00 PM',
'MM-DD-YYYY HH:MI:SS PM'
)
AND actual_start_date < SYSDATE)),
e10 AS
(SELECT TRIM
(BOTH ' ' FROM (SELECT meaning
FROM apps.fnd_lookup_values#dblink
WHERE lookup_type = 'CP_STATUS_CODE'
AND lookup_code = fcr.status_code
AND view_application_id = 0)
) status,
TRIM
(BOTH ' ' FROM (SELECT meaning
FROM apps.fnd_lookup_values#dblink
WHERE lookup_type = 'CP_PHASE_CODE'
AND lookup_code = fcr.phase_code
AND view_application_id = 0)
) phase,
fcr.request_id AS rid,
fcr.actual_start_date AS start_date,
fcr.actual_completion_date AS completion_date
FROM apps.fnd_concurrent_requests#dblink fcr
CONNECT BY PRIOR fcr.request_id = fcr.parent_request_id
START WITH request_id = (SELECT request_id
FROM plan_name
WHERE plan_name IN ('E10')))
SELECT 'E10' "PLAN_NAME", (SELECT MIN (start_date)
FROM e10) "START_DATE",
(SELECT COUNT (rid)
FROM e10)
FROM DUAL
When I try selecting directly from the query I get proper output. But after I create the view and try select * from plan_view I get the below error:
ORA-00604: error occurred at recursive SQL level 1
ORA-00904: "FCR"."REQUEST_ID": invalid identifier
This sounds like the old problem with granting privileges. For ad hoc DML (select, insert, etc) we can use privileges granted through a role. But to build permanent objects - views, stored procedures, etc - we must have privileges granted to the user directly.
So, the most common explanation for the phenomenon you describe is that the tables' owner has granted rights on those tables to a role not to you.

Joining two SELECT statements using Outer Join with multiple alias

I have a complicated select statement that when it is executed, I give it a date or date range I want and the output comes out. The problem is I don't know how to join the same SQL statement with the first Statement having 1 date range and the second statement having another data range. Example below:
When I execute the select statement, I choose for the Month of November:
EMPLID NAME Current_Gross_Hours(November)
When I execute the select statement again, I choose from January to November:
EMPLID NAME Year_To_Date_Hours(January - November)
What I want:
EMPLID NAME Current_Gross_Hours(November) Year_To_Date_Hours(January - November)
The SQL Select statement runs correctly if execute by themselves. But I don't know how to join them.
Here is the SQL code that I want to write, but I don't know how to write the SQL statement correctly. Any help or direction is greatly appreciated.
(SELECT DISTINCT
SUM("PSA"."AL_HOURS") AS "Current Gross Hours", "PSJ"."EMPLID","PSP"."NAME"
FROM
"PS_JOB" "PSJ", "PS_EMPLOYMENT" "PSE", "PS_PERSONAL_DATA" "PSP", "PS_AL_CHK_HRS_ERN" "PSA"
WHERE
((("PSA"."CHECK_DT" = TO_DATE('2011-11-01', 'YYYY-MM-DD')) AND
("PSJ"."PAYGROUP" = 'SK2') AND
(("PSJ"."EFFSEQ"= (
SELECT MAX("INNERALIAS"."EFFSEQ")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" = "PSJ"."EFFDT")
AND
"PSJ"."EFFDT" = (
SELECT MAX("INNERALIAS"."EFFDT")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" <= SYSDATE)))))
AND
("PSJ"."EMPLID" = "PSE"."EMPLID" ) AND ("PSJ"."EMPLID" = "PSP"."EMPLID" ) AND ("PSJ"."FILE_NBR" = "PSA"."FILE_NBR" ) AND ("PSJ"."PAYGROUP" = "PSA"."PAYGROUP" ) AND ("PSE"."EMPLID" = "PSP"."EMPLID" )
GROUP BY
"PSJ"."EMPLID", "PSP"."NAME"
) AS "Q1"
LEFT JOIN
(SELECT DISTINCT
SUM("PSA"."AL_HOURS") AS "YEAR_TO_DATE Gross Hours", "PSJ"."EMPLID"
FROM
"PS_JOB" "PSJ", "PS_EMPLOYMENT" "PSE", "PS_PERSONAL_DATA" "PSP", "PS_AL_CHK_HRS_ERN" "PSA"
WHERE
((("PSA"."CHECK_DT" BETWEEN TO_DATE('2011-01-01', 'YYYY-MM-DD') AND TO_DATE('2011-11-01', 'YYYY-MM-DD')) AND
("PSJ"."PAYGROUP" = 'SK2') AND
(("PSJ"."EFFSEQ"= (
SELECT MAX("INNERALIAS"."EFFSEQ")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" = "PSJ"."EFFDT")
AND
"PSJ"."EFFDT" = (
SELECT MAX("INNERALIAS"."EFFDT")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" <= SYSDATE)))))
AND
("PSJ"."EMPLID" = "PSE"."EMPLID" ) AND ("PSJ"."EMPLID" = "PSP"."EMPLID" ) AND ("PSJ"."FILE_NBR" = "PSA"."FILE_NBR" ) AND ("PSJ"."PAYGROUP" = "PSA"."PAYGROUP" ) AND ("PSE"."EMPLID" = "PSP"."EMPLID" )
GROUP BY
"PSJ"."EMPLID"
) AS "Q2"
ON "Q1"."EMPLID"="Q2"."EMPLID"
ORDER BY
"Q1"."NAME"
You are missing a SELECT ... FROM at the start. The AS keyword only works when creating column aliases, not query block aliases. Quotation marks are only necessary for case-sensitive column names - they don't look incorrect in your example but they frequently cause mistakes.
SELECT Q1.NAME, ...
FROM
(
SELECT ...
) Q1
JOIN
(
SELECT ...
) Q2
ON Q1.EMPLID=Q2.EMPLID
ORDER BY Q1.NAME