issue in converting oracle join query to ansi syntax - sql

I am trying to convert the below mentioned query to ansi format
oracle query :
SELECT *
FROM table_1 a,
table_2 b,
table_2 c
WHERE c.dt(+) = a.dt
AND c.ap_no(+) = a.ap_no
AND c.jy_no(+) = a.jy_no
AND c.tn(+) = a.tn
AND b.dt(+) = a.dt
AND b.ap_no(+) = a.ap_no
AND b.jy_no(+) = a.jy_no
AND ( b.t_no(+) + 1 ) = a.t_no --> unable to understand this part
AND ( b.type_cd(+) = 100
AND c.type_cd(+) = 200);
ansi format :
select * from
table2 c right outer join table1 a on
c.dt = a.dt
and c.ap_no = a.ap_no
and c.jy_no = a.jy_no
and c.tn = a.tn
left outer join table2 b on
a.dt = b.dt
and a.ap_no = b.ap_no
and a.jy_no = b.jy_no
and a.t_no = b.t_no ------------------> wrongly implemented it
where b.type_cd = 100 and c.type_cd = 200;
I am not understanding, how to convert the +1 found in the "unable to understand this part" of the Oracle query to the ansi format. Please advise.
Edit : this is not fully similar to the problem explained here . I am just trying to understand the +1 part of the query.

Related

UPDATE statement with JOIN in SQL Server Not Working as Expected

I'm attempting to update the LAST_INSPECTION_FW field for all records in the VEHICLES_FW table with the last JOB_DATE_FW for records with the REASON_CODE_FW = 35. However, what's happening is that once the below code is executed, it's not taking into consideration the WHERE clause. This causes all of the records to update when it should just be updating those with the REASON_CODE_FW = 35.
Is there a way to restructure this code to get it working correctly? Please help, thanks!
UPDATE VEHICLES_FW
SET VEHICLES_FW.LAST_INSPECTION_FW = JOB_HEADERS_FW.FIELD2MAX
FROM VEHICLES_FW
INNER JOIN (SELECT VEHICLE_ID_FW, MAX(JOB_DATE_FW) AS FIELD2MAX
FROM JOB_HEADERS_FW
GROUP BY VEHICLE_ID_FW) AS JOB_HEADERS_FW
ON VEHICLES_FW.VEHICLE_ID_FW = JOB_HEADERS_FW.VEHICLE_ID_FW
INNER JOIN JOB_DETAILS_FW
ON JOB_NUMBER_FW = JOB_NUMBER_FW
WHERE REASON_CODE_FW = '35'
Common Table Expressions are your friend here. SQL Server's strange UPDATE ... FROM syntax is not. EG
with JOB_HEADERS_FW_BY_VEHICLE_ID as
(
SELECT VEHICLE_ID_FW, MAX(JOB_DATE_FW) AS FIELD2MAX
FROM JOB_HEADERS_FW
GROUP BY VEHICLE_ID_FW
), q as
(
Select VEHICLES_FW.LAST_INSPECTION_FW, JOB_HEADERS_FW_BY_VEHICLE_ID.FIELD2MAX NEW_LAST_INSPECTION_FW
FROM VEHICLES_FW
INNER JOIN JOB_HEADERS_FW_BY_VEHICLE_ID
ON VEHICLES_FW.VEHICLE_ID_FW = JOB_HEADERS_FW_BY_VEHICLE_ID.VEHICLE_ID_FW
INNER JOIN JOB_DETAILS_FW
ON JOB_NUMBER_FW = JOB_NUMBER_FW
WHERE REASON_CODE_FW = '35'
)
UPDATE q set LAST_INSPECTION_FW = NEW_LAST_INSPECTION_FW
I suspect this does what you want:
update v
set last_inspection_fw = (
select max(j.job_date_fw)
from job_headers_fw j
inner join job_details_fw jd on jd.job_number_fw = j.job_number_fw
where j.vehicle_id_fw = v.vehicle_id_fw and jd.reason_code_fw = 35
)
from vehicles_fw v

TypeORM subqueries

Based on typeORM docs on using subqueries, there are explained how to create subqueries.
Example:
const qb = await getRepository(Post).createQueryBuilder("post");
const posts = qb
.where("post.title IN " + qb.subQuery().select("user.name").from(User, "user").where("user.registered = :registered").getQuery())
.setParameter("registered", true)
.getMany();
But there is no equivalent as to what the SQL would be.
Supposed that I have the query which contains subqueries like the following:
SELECT a, TO_CHAR (MAX (jointable.f), 'MON YYYY') as f,
t3.c, t3.d, t1.e
FROM table1 t1
LEFT JOIN table2 t2 ON t2.e = t1.e
JOIN table3 t3 ON t3.d = t2.d
JOIN
(SELECT f, t4.g, t5.e, t6.h
FROM table4 t4
JOIN table5 t5 ON t4.g = t5.g
JOIN table6 t6 ON t6.g = t4.g
AND (t6.i = 2
OR (t6.i = 1 AND j = 1)
)
WHERE t4.k = 4
) jointable ON t1.e = jointable.e
WHERE jointable.h = :h
AND(:d = 3 OR
t3."d" = :d
)
GROUP BY a, t3.c, t3.d, t1.e
ORDER BY a ASC
How should I use the typeORM query builder function for the SQL query above?
Assuming that I had created entities related to all table being used on the query above.
I hope this answer could help others to use TypeORM subquery.
const subquery = await getManager()
.createQueryBuilder(table4, 't4')
.select('"t4".f')
.addSelect('"t4".g')
.addSelect('"t5".e')
.addSelect('"t6".h')
.innerJoin(table5, 't5', '"t4".g = "t5".g')
.innerJoin(table6, 't6', '"t6".g = "t4".g')
.where('"t4".k = 4 AND ("t6".i = 2 OR ("t6".i = 1 AND "t6".j = 1))');
model = await getManager()
.createQueryBuilder(table1, 't1')
.select('"t1".a')
.addSelect("TO_CHAR (MAX (jointable.f), 'MON YYYY')", 'f')
.addSelect('"t3".c')
.addSelect('"t3".d')
.addSelect('"t1".e')
.leftJoin('table2', 't2', '"t2".e = "t1".e')
.innerJoin(table3, 't3', '"t3".d = "t2".d')
.innerJoin('('+subquery.getQuery()+')', 'jointable', '"t1".e = jointable.e')
.where('jointable.h = :h AND (:d = 3 OR "t3".d = :d)',
{ h: h, d: d })
.groupBy('"t1".a, "t3".c, "t3".d, "t1".e')
.orderBy('"t1".a', 'ASC')
.getRawMany();
I was using '('+subquery.getQuery()+')' for getting the subquery select query as an equivalent to
(SELECT f, t4.g, t5.e, t6.h ....
......
.... ) jointable ON t1.e = jointable.e
Based on what I understand:
Join actually is equal to inner join
.select is and equivalent of select in SQL. You could also add
aliases (as in SQL).
.addSelect is similar to , in select
There are two types of results you can get using select query
builder: entities or raw results. To describe whether you want
the data as entities (getOne and getMany) or what it
is(getRawOne and getRawMany).

Error Position: 27 Return: 1730 - ORA-01730: invalid number of column names specified

I need help in creating a view in PeopleSoft using PeopleTools Application Designer. When running the view SQL below in ORACLE SQL Developer, it selects perfectly fine but when I create it as view in App Designer I get the error below:
Error: UM_7902_VW - SQL Error. Error Position: 27 Return: 1730 - ORA-01730: invalid number of column names specified.
Here is my sql:
SELECT A.EMPLID, B.NAME, A.ADM_APPL_NBR, C.ADMIN_FUNCTION, A.STDNT_CAR_NBR,
A.ACAD_CAREER, D.APPL_PROG_NBR, D.ADMIT_TERM, C.CHECKLIST_CD, E.DESCRSHORT,
C.CHECKLIST_STATUS, C.STATUS_DT, C.DUE_DT AS C_DUE_DT, C.COMM_COMMENTS,
C.SEQ_3C, F.CHKLST_ITEM_CD,
G.DESCR, F.ITEM_STATUS, F.STATUS_DT, F.DUE_DT AS F_DUE_DT, F.RESPONSIBLE_ID
FROM PS_ADM_APPL_DATA A
JOIN PS_PERSON_NAME B ON B.EMPLID = A.EMPLID
JOIN PS_PERSON_CHECKLST C ON C.COMMON_ID = A.EMPLID
JOIN PS_ADM_APPL_PROG D ON D.EMPLID = A.EMPLID
AND D.EMPLID = A.EMPLID
AND D.ACAD_CAREER= A.ACAD_CAREER
AND D.STDNT_CAR_NBR= A.STDNT_CAR_NBR
AND D.ADM_APPL_NBR= A.ADM_APPL_NBR
AND D.EFFDT =
(SELECT MAX(D2.EFFDT) FROM PS_ADM_APPL_PROG D2
WHERE D.EMPLID = D2.EMPLID
AND D.ACAD_CAREER = D2.ACAD_CAREER
AND D.STDNT_CAR_NBR = D2.STDNT_CAR_NBR
AND D.ADM_APPL_NBR = D2.ADM_APPL_NBR
AND D.APPL_PROG_NBR = D2.APPL_PROG_NBR
AND D2.EFFDT <= SYSDATE)
AND D.EFFSEQ =
(SELECT MAX(D3.EFFSEQ) FROM PS_ADM_APPL_PROG D3
WHERE D.EMPLID = D3.EMPLID
AND D.ACAD_CAREER = D3.ACAD_CAREER
AND D.STDNT_CAR_NBR = D3.STDNT_CAR_NBR
AND D.ADM_APPL_NBR = D3.ADM_APPL_NBR
AND D.APPL_PROG_NBR = D3.APPL_PROG_NBR
AND D.EFFDT <= D3.EFFDT)
JOIN PS_CS_CHKLST_TBL E ON E.CHECKLIST_CD = C.CHECKLIST_CD
AND E.EFFDT = (SELECT MAX(E2.EFFDT) FROM PS_CS_CHKLST_TBL E2 WHERE
E2.INSTITUTION = E.INSTITUTION AND E2.CHECKLIST_CD = E.CHECKLIST_CD)
AND E.EFF_STATUS = 'A'
JOIN PS_PERSON_CHK_ITEM F ON F.COMMON_ID = C.COMMON_ID
AND F.SEQ_3C = C.SEQ_3C
JOIN PS_SCC_CKLSITM_TBL G ON G.CHKLST_ITEM_CD = F.CHKLST_ITEM_CD
AND G.EFF_STATUS = 'A'
AND G.EFFDT = (SELECT MAX(G2.EFFDT) FROM PS_SCC_CKLSITM_TBL G2 WHERE
G2.CHKLST_ITEM_CD = G.CHKLST_ITEM_CD)
JOIN PS_TERM_TBL H ON H.INSTITUTION = E.INSTITUTION
AND H.ACAD_CAREER = A.ACAD_CAREER
I would appreciate any help i can get. Thanks!
In App Designer when modeling the record: do you have an equal amount of fields in the record definition as you have in your select SQL?
These must match, otherwise this error appears when building in App Designer.
It looks as if you took create view some_view (col1, col2, ..., coln) as from one of its previous incarnations, modified the select statement you posted (added or removed some columns) and - once you stick it together - number of columns doesn't match any more.
The simplest way out of it is to let Oracle do it, i.e. don't name columns:
create view some_view as
select <your select goes here>
Though, pay attention to column names select returns; you can't have something like
select max(some_value), --> this - must have an alias
my_name
because columns query returns must have their aliases.

Teradata 3706, expected something between DISTINCT and LEFT keyword

I am here trying to use UPDATE query with JOIN and SELECT, But I am facing 3706 error.
here is the error line -
UPDATE A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A2
FROM
(SELECT A.REP_CD, A.GEO_CD,A.MONTH_ID, B.IC_PRD AS IC_PAYOUT_FLAG,
COALESCE(CASE
WHEN UPPER(B.IC_PRD) = 'SEMESTERLY' THEN A.CURR_SEMESTER_FLAG
WHEN UPPER(B.IC_PRD) = 'QUARTERLY' THEN A. CURR_QUARTER_FLAG
ELSE '0'
END,'0') IC_PAYOUT_FLAG
FROM
A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A,
(
SELECT DISTINCT LEFT(B. REP_CD, POSITION('-' IN B.REP_CD)-1) AS REP_CD,C.GEO_CD,IC_PRD FROM
A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD A, A0619IL3549_D00_IC_MAIN.ICDM_DIM_REP B,A0619IL3549_D00_IC_MAIN.ICDM_DIM_GEO_HIER C
WHERE
A.REP_SK=B.REP_SK
AND A.GEO_SK=C.GEO_SK
AND A.RUN_ID = (SELECT MAX(RUN_ID) FROM A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD)
) B
WHERE
A.REP_CD = B.REP_CD
AND
A.GEO_CD = B.GEO_CD)A1
SET A2.IC_PAYOUT_FALG = A1.IC_PAYOUT_FLAG
WHERE
A1.REP_CD = A2.REP_CD
AND
A1.MONTH_ID = A2.MONTH_ID
AND
A.GEO_CD = A2.GEO_CD
LEFT is not a valid Teradata function before TD15.10, it's ODBC SQL, which is automatically translated, but only for SELECT.
Use valid syntax instead:
SUBSTRING(B. REP_CD FROM 1 FOR POSITION('-' IN B.REP_CD)-1)

How to Use Case statement in Left outer Join using Operator +?

Query:
select /*+ full(c)parallel(c,4) */ a.*
from ioct_inv_item_all a,
(select udac_group_name, function_order , db_region, workflow_type, change_level
from ioct_function_target
where udac_group_name = 'Banner'
and function_name = 'emptyParam'
and workflow_type = 'CHGFCLTR'
and ('emptyParam' = 'emptyParam' or ('CHGFCLTR' != 'NEWITM' AND change_level = 'emptyParam') or ('CHGFCLTR' = 'NEWITM' and change_level = 'Simple') )
and sysdate between effective_from_date and effective_to_date ) b , item_ca c ,product_ca d,assignment_ca ac ,branches b
where a.udac_group_name = 'Banner'
and a.workflow_type = 'CHGFCLTR'
and a.DB_REGION = b.db_region (+)
and a.workflow_type = b.workflow_type (+)
and a.udac_group_name = b.udac_group_name (+)
and a.change_level = b.change_level (+)
and a.product_code = c.product_code(+)
and a.product_issue_num = c.product_issue_num(+)
and a.item_id = c.item_id(+)
and a.customer_id = c.customer_id(+)
and c.product_code = d.product_code(+)
and c.product_issue_num = d.product_issue_num(+)
and c.product_issue_year = d.product_issue_year(+)
and c.customer_id = d.customer_id(+)
and (case when c.contract_assignment_id IS NOT NULL
AND c.contract_assignment_id > 0
THEN c.contract_assignment_id
ELSE d.regular_assign_id
END) = ac.assignment_id(+)
and ('emptyParam' = 'emptyParam' OR ('CHGFCLTR' = 'NEWITM' AND a.eue_normal_ind = 'emptyParam') OR ('CHGFCLTR' != 'NEWITM' AND a.change_level = 'emptyParam') )
and (('emptyParam') IN ('emptyParam') OR a.region IN ('emptyParam'))
and ('emptyParam' = 'emptyParam'
OR (b.function_order is not null and a.assignee_group_seq = b.function_order)
OR (b.function_order is null and 'emptyParam' = 'emptyParam')
Error:
error:
ORA-01417: a table may be outer joined to at most one other table
01417. 00000 - "a table may be outer joined to at most one other table"
*Cause: a.b (+) = b.b and a.c (+) = c.c is not allowed
*Action: Check that this is really what you want, then join b and c first
in a view.
Error at Line: 28 Column: 20
Does any body suggest any alternative to do same thing without using keyword LEFT OUTER JOIN because i do not want to change the structure of sql query already Written?
The basic idea is that you start with a driving table and then outer join it to other tables. If a is your driving table, then
select x
from a,b,c
where a.x = b.x(+)
and a.y = c.y(+)
is the sort of query oracle expects in the join.
If you really want outer join a table with multiple "driving" tables, you can to use a syntax like this, as pointed out in the error description
Assuming you want to outer join "a" to both "b" and "c"
select *
from a,
(select *
from a,c
where c.y = a.y(+)
)
where b.x = a.x(+)
;