Changing SQL Server query to pure ANSI SQL query - ansi-sql

I am working on a database system that uses SQL syntax. However I am unable to use the cross apply in the code below. Is there a way to rewrite this without applies?
declare #rsBuildDetails table(dt datetime, build varchar(255), val varchar(255));
insert into #rsBuildDetails (dt, build, val)
values ('20100101', '1', 'pass')
,('20100102', '2', 'fail')
,('20100103', '3', 'pass')
,('20100104', '4', 'fail')
,('20100105', '5', 'fail')
,('20100106', '6', 'fail')
,('20100107', '7', 'pass')
,('20100108', '8', 'pass')
,('20100109', '9', 'pass')
,('20100110', '10', 'fail');
with passed as
(
select *
from #rsBuildDetails
where val='pass'
)
select distinct
preFail.dt AS FailedDt,
postFail.dt AS SecondFailedDt
from
passed
cross apply
(select top 1
pre.*
from
#rsBuildDetails as pre
where
pre.dt < passed.dt
and pre.val = 'fail'
order by
pre.dt desc) as preFail
cross apply
(select top 1
post.*
from
#rsBuildDetails as post
where
post.dt > passed.dt
and post.val = 'fail'
order by
post.dt asc) as postFail

You might try to transfer the CTE and all applies to inlined sub-selects:
declare #rsBuildDetails table(dt datetime, build varchar(255), val varchar(255));
insert into #rsBuildDetails (dt, build, val) values
('20100101', '1', 'pass')
,('20100102', '2', 'fail')
,('20100103', '3', 'pass')
,('20100104', '4', 'fail')
,('20100105', '5', 'fail')
,('20100106', '6', 'fail')
,('20100107', '7', 'pass')
,('20100108', '8', 'pass')
,('20100109', '9', 'pass')
,('20100110', '10', 'fail');
select *
from
(
select distinct
(
select top 1 pre.Dt
from #rsBuildDetails as pre
where pre.dt<passed.dt
and pre.val='fail'
order by pre.dt desc
) as FailedDt
,(
select top 1 post.Dt
from #rsBuildDetails as post
where post.dt>passed.dt
and post.val='fail'
order by post.dt asc
) AS SecondFailedDt
from
(
select *
from #rsBuildDetails
where val='pass'
) AS passed
) AS tbl
where tbl.FailedDt IS NOT NULL
AND tbl.SecondFailedDt IS NOT NULL

Related

Need to pull by date, that a value appears from different tables

I have tried unions, joins, distinct and minimum functions with some success, but still slightly off.
Example of the tables are below
ID date
1 01-02-2020
2 02-02-2020
3 04-06-2020
4 01-03-2019
ID date
1 01-03-2018
2 07-02-2019
3 10-02-2020
4 09-02-2020
What I want is
ID date
1 01-02-2020
2 01-03-2019
3 01-03-2018
4 07-02-2019
5 10-02-2020
It is not pretty
But a Full OUT JOIN which MySQL doesn't have, of both tables did the trick
CREATE TABLE tab1 (
`ID` INTEGER,
`Value_ID` INTEGER,
`date` VARCHAR(10)
);
INSERT INTO tab1
(`ID`, `Value_ID`, `date`)
VALUES
('1', '1', '01-02-2020'),
('2', '2', '02-02-2020'),
('3', '3', '04-06-2020'),
('4', '2', '01-03-2019');
CREATE TABLE tab2 (
`ID` INTEGER,
`Value_ID` INTEGER,
`date` VARCHAR(10)
);
INSERT INTO tab2
(`ID`, `Value_ID`, `date`)
VALUES
('1', '3', '01-03-2018'),
('2', '4', '07-02-2019'),
('3', '5', '10-02-2020'),
('4', '4', '09-02-2020');
SELECT COALESCE(t2.`Value_ID`,t1.`Value_ID`) as `Value_ID`,IF (IFNULL(t1.`date`,'31-12-9999') <= t2.`date`,t1.`date`,t2.`date`)
FROM (SELECT `Value_ID`, MIN(`date`) as `date` FROM tab1 GROUP BY `Value_ID`) t1
right JOIN (SELECT `Value_ID`, MIN(`date`) as `date` FROM tab2 GROUP BY `Value_ID`) t2
ON t1.`Value_ID` = t2.`Value_ID`
UNION
SELECT COALESCE(t1.`Value_ID`,t2.`Value_ID`),IF (t1.`date` <= IFNULL(t2.`date`,'31-12-9999'),t1.`date`,t2.`date`)
FROM (SELECT `Value_ID`, MIN(`date`) as `date` FROM tab1 GROUP BY `Value_ID`) t1
LEFT JOIN (SELECT `Value_ID`, MIN(`date`) as `date` FROM tab2 GROUP BY `Value_ID`) t2
ON t1.`Value_ID` = t2.`Value_ID`
ORDER BY `Value_ID`
Value_ID | IF (IFNULL(t1.`date`,'31-12-9999') <= t2.`date`,t1.`date`,t2.`date`)
-------: | :-------------------------------------------------------------------
1 | 01-02-2020
2 | 01-03-2019
3 | 01-03-2018
4 | 07-02-2019
5 | 10-02-2020
db<>fiddle here
You can use Window functions
Schema (MySQL v8.0)
CREATE TABLE tab1 (
`ID` INTEGER,
`Value_ID` INTEGER,
`date` VARCHAR(10)
);
INSERT INTO tab1
(`ID`, `Value_ID`, `date`)
VALUES
('1', '1', '01-02-2020'),
('2', '2', '02-02-2020'),
('3', '3', '04-06-2020'),
('4', '2', '01-03-2019');
CREATE TABLE tab2 (
`ID` INTEGER,
`Value_ID` INTEGER,
`date` VARCHAR(10)
);
INSERT INTO tab2
(`ID`, `Value_ID`, `date`)
VALUES
('1', '3', '01-03-2018'),
('2', '4', '07-02-2019'),
('3', '5', '10-02-2020'),
('4', '4', '09-02-2020');
Query #1
SELECT
`Value_ID`, `date`
FROM
(SELECT
`Value_ID`, `date`
, ROW_NUMBER() OVER (PARTITION BY `Value_ID` ORDER BY `date` ASC) rn
FROM
(SELECT `Value_ID`, MIN(`date`) as `date` FROM tab1 GROUP BY `Value_ID`
UNION
SELECT `Value_ID`, MIN(`date`) as `date` FROM tab2 GROUP BY `Value_ID`) t1
) t3
WHERE rn = 1;
Value_ID
date
1
01-02-2020
2
01-03-2019
3
01-03-2018
4
07-02-2019
5
10-02-2020
View on DB Fiddle

Create View but get Warning: compiled but with compilation error in oracle

When i create a view I am getting this warning in Toad:
Warning: compiled but with compilation error in oracle
This is my script:
CREATE OR REPLACE FORCE VIEW APPS.KMI_OKP_TRACK_V
(
NO_OKP,
LINE,
FBD_PSD,
FBD_ASD,
FBD_PCD,
FBD_ACD,
FBD_DD,
FBD_BCD,
MIX_PSD,
MIX_ASD,
MIX_PCD,
MIX_ACD,
MIX_DD,
MIX_BCD,
BPR_PSD,
BPR_ASD,
BPR_PCD,
BPR_ACD,
BPR_DD,
BPR_BCD,
BLD_PSD,
BLD_ASD,
BLD_PCD,
BLD_ACD,
BLD_DD,
BLD_BCD,
CAN_PSD,
CAN_ASD,
CAN_PCD,
CAN_ACD,
CAN_DD,
CAN_BCD,
BAG_PSD,
BAG_ASD,
BAG_PCD,
BAG_ACD,
BAG_DD,
BAG_BCD,
DPG_PSD,
DPG_ASD,
DPG_PCD,
DPG_ACD,
DPG_DD,
DPG_BCD,
SAC_PSD,
SAC_ASD,
SAC_PCD,
SAC_ACD,
SAC_DD,
SAC_BCD,
BPE_PSD,
BPE_ASD,
BPE_PCD,
BPE_ACD,
BPE_DD,
BPE_BCD
)
AS
SELECT DISTINCT XX.NO_OKP,
XX.LINE --,XX.CREATION_DATE
,
FBD.FBD_PSD,
FBD.FBD_ASD,
FBD.FBD_PCD,
FBD.FBD_ACD,
FBD.FBD_DD,
FBD.FBD_BCD,
MIX.MIX_PSD,
MIX.MIX_ASD,
MIX.MIX_PCD,
MIX.MIX_ACD,
MIX.MIX_DD,
MIX.MIX_BCD,
BPR.BPR_PSD,
BPR.BPR_ASD,
BPR.BPR_PCD,
BPR.BPR_ACD,
BPR.BPR_DD,
BPR.BPR_BCD,
BLD.BLD_PSD,
BLD.BLD_ASD,
BLD.BLD_PCD,
BLD.BLD_ACD,
BLD.BLD_DD,
BLD.BLD_BCD,
CAN.CAN_PSD,
CAN.CAN_ASD,
CAN.CAN_PCD,
CAN.CAN_ACD,
CAN.CAN_DD,
CAN.CAN_BCD,
BAG.BAG_PSD,
BAG.BAG_ASD,
BAG.BAG_PCD,
BAG.BAG_ACD,
BAG.BAG_DD,
BAG.BAG_BCD,
DPG.DPG_PSD,
DPG.DPG_ASD,
DPG.DPG_PCD,
DPG.DPG_ACD,
DPG.DPG_DD,
DPG.DPG_BCD,
SAC.SAC_PSD,
SAC.SAC_ASD,
SAC.SAC_PCD,
SAC.SAC_ACD,
SAC.SAC_DD,
SAC.SAC_BCD,
BPE.BPE_PSD,
BPE.BPE_ASD,
BPE.BPE_PCD,
BPE.BPE_ACD,
BPE.BPE_DD,
BPE.BPE_BCD
FROM ( SELECT DISTINCT
RTRIM (
SUBSTR (BATCH_NO, 4, 4),
'A'
)
no_okp,
ATTRIBUTE1 LINE --,CREATION_DATE
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date,
'MM/DD/YYYY') IN
(TO_CHAR (
SYSDATE - 7,
'MM/DD/YYYY'
),
TO_CHAR (
SYSDATE - 8,
'MM/DD/YYYY'
))
ORDER BY CREATION_DATE DESC) XX
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (
SUBSTR (BATCH_NO, 4, 4),
'A'
)
no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE FBD_PSD,
ACTUAL_START_DATE FBD_ASD,
PLAN_CMPLT_DATE FBD_PCD,
ACTUAL_CMPLT_DATE FBD_ACD,
DUE_DATE FBD_DD,
BATCH_CLOSE_DATE FBD_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date,
'MM/DD/YYYY') IN
(TO_CHAR (
SYSDATE - 7,
'MM/DD/YYYY'
),
TO_CHAR (
SYSDATE - 8,
'MM/DD/YYYY'
))
AND attribute1 = 'FAT BLEND'
ORDER BY CREATION_DATE DESC) FBD
ON XX.NO_OKP = FBD.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4),
'A')
no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE MIX_PSD,
ACTUAL_START_DATE MIX_ASD,
PLAN_CMPLT_DATE MIX_PCD,
ACTUAL_CMPLT_DATE MIX_ACD,
DUE_DATE MIX_DD,
BATCH_CLOSE_DATE MIX_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date,
'MM/DD/YYYY') IN
(TO_CHAR (
SYSDATE - 7,
'MM/DD/YYYY'
),
TO_CHAR (
SYSDATE - 8,
'MM/DD/YYYY'
))
AND attribute1 = 'MIX'
ORDER BY CREATION_DATE DESC) MIX
ON XX.NO_OKP = MIX.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4),
'A')
no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE BPR_PSD,
ACTUAL_START_DATE BPR_ASD,
PLAN_CMPLT_DATE BPR_PCD,
ACTUAL_CMPLT_DATE BPR_ACD,
DUE_DATE BPR_DD,
BATCH_CLOSE_DATE BPR_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date,
'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7,
'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8,
'MM/DD/YYYY'))
AND attribute1 = 'BASE POWDER'
ORDER BY CREATION_DATE DESC) BPR
ON XX.NO_OKP = BPR.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4), 'A')
no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE BLD_PSD,
ACTUAL_START_DATE BLD_ASD,
PLAN_CMPLT_DATE BLD_PCD,
ACTUAL_CMPLT_DATE BLD_ACD,
DUE_DATE BLD_DD,
BATCH_CLOSE_DATE BLD_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date,
'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7,
'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8,
'MM/DD/YYYY'))
AND attribute1 = 'BLEND'
ORDER BY CREATION_DATE DESC) BLD
ON XX.NO_OKP = BLD.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4), 'A')
no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE CAN_PSD,
ACTUAL_START_DATE CAN_ASD,
PLAN_CMPLT_DATE CAN_PCD,
ACTUAL_CMPLT_DATE CAN_ACD,
DUE_DATE CAN_DD,
BATCH_CLOSE_DATE CAN_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date,
'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7,
'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8,
'MM/DD/YYYY'))
AND attribute1 = 'CANNING'
ORDER BY CREATION_DATE DESC) CAN
ON XX.NO_OKP = CAN.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4), 'A') no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE BAG_PSD,
ACTUAL_START_DATE BAG_ASD,
PLAN_CMPLT_DATE BAG_PCD,
ACTUAL_CMPLT_DATE BAG_ACD,
DUE_DATE BAG_DD,
BATCH_CLOSE_DATE BAG_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date, 'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7,
'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8,
'MM/DD/YYYY'))
AND attribute1 = 'BAG'
ORDER BY CREATION_DATE DESC) BAG
ON XX.NO_OKP = BAG.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4), 'A') no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE DPG_PSD,
ACTUAL_START_DATE DPG_ASD,
PLAN_CMPLT_DATE DPG_PCD,
ACTUAL_CMPLT_DATE DPG_ACD,
DUE_DATE DPG_DD,
BATCH_CLOSE_DATE DPG_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date, 'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7, 'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8, 'MM/DD/YYYY'))
AND attribute1 = 'DUMPING'
ORDER BY CREATION_DATE DESC) DPG
ON XX.NO_OKP = DPG.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4), 'A') no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE SAC_PSD,
ACTUAL_START_DATE SAC_ASD,
PLAN_CMPLT_DATE SAC_PCD,
ACTUAL_CMPLT_DATE SAC_ACD,
DUE_DATE SAC_DD,
BATCH_CLOSE_DATE SAC_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date, 'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7, 'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8, 'MM/DD/YYYY'))
AND attribute1 IN ('SACHET D', 'SACHET A')
ORDER BY CREATION_DATE DESC) SAC
ON XX.NO_OKP = SAC.NO_OKP
LEFT OUTER JOIN
( SELECT DISTINCT
RTRIM (SUBSTR (BATCH_NO, 4, 4), 'A') no_okp,
attribute1 Line,
DECODE (batch_status,
'1', 'Inspect',
'2', 'Release',
'3', 'Complete',
'Close')
Batch_status,
CREATION_DATE,
PLAN_START_DATE BPE_PSD,
ACTUAL_START_DATE BPE_ASD,
PLAN_CMPLT_DATE BPE_PCD,
ACTUAL_CMPLT_DATE BPE_ACD,
DUE_DATE BPE_DD,
BATCH_CLOSE_DATE BPE_BCD
FROM GME_BATCH_HEADER
WHERE ORGANIZATION_ID = 84
AND TO_CHAR (creation_date, 'MM/DD/YYYY') IN
(TO_CHAR (SYSDATE - 7, 'MM/DD/YYYY'),
TO_CHAR (SYSDATE - 8, 'MM/DD/YYYY'))
AND attribute1 = 'SACHET E'
ORDER BY CREATION_DATE DESC) BPE
ON XX.NO_OKP = BPE.NO_OKP
ORDER BY xx.no_okp ASC;
Where is the fault ?how to get the specific error code ?
To find the specific errors, either:
Test it without the force keyword.
Test it without the create view ... as part, i.e. test the select part on its own.
In SQL*Plus or compatible tools, execute the following command while connected as the APPS user: show errors view kmi_okp_track_v (or include the schema if executing from a DBA account)
Query user_errors while connected as the APPS user (or dba_errors if executing from a DBA account).
Example:
SQL> create or replace force view demo_view as
2 select nosuchcolumn from dual;
Warning: View created with compilation errors.
Test without the force keyword:
SQL> create or replace view demo_view as
2 select nosuchcolumn from dual;
select nosuchcolumn from dual
*
ERROR at line 2:
ORA-00904: "NOSUCHCOLUMN": invalid identifier
Testing the select statement on its own:
SQL> select nosuchcolumn from dual;
select nosuchcolumn from dual
*
ERROR at line 1:
ORA-00904: "NOSUCHCOLUMN": invalid identifier
Using show errors:
SQL> show errors view demo_view
Errors for VIEW DEMO_VIEW:
LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 ORA-00904: "NOSUCHCOLUMN": invalid identifier
Querying user_errors view:
SQL> col text format a70
SQL> select text from user_errors e
2 where e.name = 'DEMO_VIEW'
3 order by e.sequence;
TEXT
----------------------------------------------------------------------
ORA-00904: "NOSUCHCOLUMN": invalid identifier
For deployment scripts, I would include the force keyword and show errors at the end.
There's no problem with your syntax, and you may create this view in your current user, provided the schema prefix APPS. is removed from CREATE OR REPLACE FORCE VIEW APPS.KMI_OKP_TRACK_V.
If you need to create this view from your current schema, you need more prefixes for GME_BATCH_HEADER and the creation privilege should be granted for APPS schema.
As Barbaros said your query look good.
But here a problem i ran into - perhaps it's your problem, too.
-- This will work
CREATE OR REPLACE FORCE VIEW MySchema.MyView
(
MyColumn
)
AS
SELECT "MyColumn" FROM myTable;
-- This won't - because of the delimiter (';') in our variable myquery
DECLARE
myquery VARCHAR2(2000) :=
'CREATE OR REPLACE FORCE VIEW MySchema.MyView
(
MyColumn
)
AS
SELECT "MyColumn" FROM myTable;'
;
BEGIN
EXECUTE IMMEDIATE myquery;
END;
If you EXECUTE IMMEDIATE a query, it must not have a delimiter - neither ; nor /. This is not allowed because you always have to run a single statement, not a list of statement seperated by a delimiter.
The correct version is this:
-- This won't - because of the delimiter (';') in our variable myquery
DECLARE
myquery VARCHAR2(2000) :=
'CREATE OR REPLACE FORCE VIEW MySchema.MyView
(
MyColumn
)
AS
SELECT "MyColumn" FROM myTable' -- no ';' here
;
BEGIN
EXECUTE IMMEDIATE myquery;
END;
Fun-fact: Your View will be created and will work after a recompile.

ORA-32033: unsupported column aliasing

I have a query that is working on oracle 12c but not in 10g. I am not sure what the problem is:
My query is:
WITH tab1(rn,begin_chq_num,chq_lvs_stat,chq_num_of_lvs,tes) AS
(SELECT 1 rn,
begin_chq_num,
chq_lvs_stat,
chq_num_of_lvs,
SUBSTR(chq_lvs_stat,1,1) tes
FROM tbaadm.chq_book_table
WHERE del_flg != 'Y'
AND acid IN
(SELECT acid
FROM tbaadm.GENERAL_ACCT_MAST_TABLE
WHERE foracid = '01411110171546'
)
UNION ALL
SELECT rn + 1 rn,
begin_chq_num,
chq_lvs_stat,
chq_num_of_lvs,
SUBSTR(chq_lvs_stat,rn + 1,1) tes
FROM tab1
WHERE rn < chq_num_of_lvs
)
SELECT
CASE
WHEN begin_chq_num = 1
THEN rn
ELSE begin_chq_num +(rn-1)
END cheque_num,
begin_chq_num
||'-'
||(chq_num_of_lvs+begin_chq_num-1) cheque_range,
DECODE(tes, 'I', 'Issued', 'P', 'Cleared', 'U', 'Unused', 'S', 'Stopped', 'C', 'Cautioned', 'D', 'Destroyed', 'R', 'Returned Paid', 'T', 'Transfered') status
FROM tab1
ORDER BY chq_lvs_stat,
rn;
The error is :
ORA-32033: unsupported column aliasing
32033. 00000 - "unsupported column aliasing"
*Cause: column aliasing in WITH clause is not supported yet
*Action: specify aliasing in defintion subquery and retry
What should I do different?
In Oracle 10g, the sub-query factoring clause does not support column aliases or recursive sub-queries. The syntax you are using appears in 11gR2.
You need to change:
WITH tab1(rn,begin_chq_num,chq_lvs_stat,chq_num_of_lvs,tes) AS
To:
WITH tab1 AS
And find a different solution that does not use a recursive subquery factoring clause.
I think you could do this in Oracle 10g:
WITH tab1 AS (
SELECT l.COLUMN_VALUE rn,
begin_chq_num,
chq_lvs_stat,
chq_num_of_lvs,
SUBSTR(chq_lvs_stat,l.COLUMN_VALUE,1) tes
FROM tbaadm.chq_book_table t,
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= LENGTH( t.chq_lvs_stat )
)
AS SYS.ODCINUMBERLIST
)
) l
WHERE del_flg != 'Y'
AND acid IN ( SELECT acid
FROM tbaadm.GENERAL_ACCT_MAST_TABLE
WHERE foracid = '01411110171546'
)
)
SELECT CASE
WHEN begin_chq_num = 1
THEN rn
ELSE begin_chq_num +(rn-1)
END cheque_num,
begin_chq_num
||'-'
||(chq_num_of_lvs+begin_chq_num-1) cheque_range,
DECODE(
tes,
'I', 'Issued',
'P', 'Cleared',
'U', 'Unused',
'S', 'Stopped',
'C', 'Cautioned',
'D', 'Destroyed',
'R', 'Returned Paid',
'T', 'Transfered'
) status
FROM tab1
ORDER BY chq_lvs_stat, rn;

Adding additional columns to my Oracle SQL result

I have a SQL query built that returns a singular column of figures. Sample is shown below. I need to enhance this in a singular query and retrieve a 2nd set of figures using similar filters/parameters (instead of status=E I'd look for category=E). This query has gotten fairly complex for my knowledge though and I'm a bit stumped on how to approach adding this additional SELECT statement to get a 2nd column. Appreciate if anyone can point me in the right direction.
SELECT Count(eqnbr) EQUIP,
imp_exp,
'TITLE' TITLE
FROM (SELECT cy.container EQNbr,
cy.location_position "POS",
Substr(cy.location_position, 1, 1) Row_Nbr,
CASE
WHEN Substr(cy.location_position, 1, 3) IN (
'M01', 'N03', 'N04', 'N05',
'N06', 'N07', 'N08', 'N09' ) THEN
'Rail'
WHEN Substr(cy.location_position, 1, 1) IN ( 'F', 'G', 'H', 'J',
'K', 'L', 'M', 'P',
'R', 'S' ) THEN
'Locals'
WHEN Substr(cy.location_position, 1, 1) IN ( 'B', 'D' ) THEN
'Export'
ELSE Concat ('Row ', Substr(cy.location_position, 1, 1))
END IMP_EXP
FROM current_yard cy
WHERE eq_class = 'CTR'
AND location_position NOT LIKE 'CT%'
AND using_line NOT IN ( 'TEST', 'UNK', 'TST', 'B125',
'BO77', 'BR75' )
AND using_line IS NOT NULL
AND ( container NOT LIKE 'TEST%'
OR container NOT LIKE 'KENT%' )
AND Length(container) >= 10
AND status = 'E'
AND Substr(cy.location_position, 1, 1) NOT IN
( '1', '2', '3', '4',
'5', '6', '7', '8',
'9', '0', 'A', 'C',
'E', 'U', 'V', 'W', 'X' )) a
GROUP BY imp_exp
ORDER BY 2 ASC

count from current row to last row

I have a requirement to pull the data in the below fashion.
progress_CMC_status progress_cmc_status_numbers cmc_status_numbers
Mature Draft 2 5
Review 0 3
Final Draft 0 3
Final Comment 0 3
Final Document 2 3
Archived Document 1 1
but i am getting output in different
progress_CMC_status progress_cmc_status_numbers cmc_status_numbers
Mature Draft 2 5
Review 0 0
Final Draft 0 0
Final Comment 0 0
Final Document 2 3
Archived Document 1 1
Tried the below code:
WITH sta AS
(SELECT 'Mature' "status1"
FROM DUAL
UNION
SELECT 'Review'
FROM DUAL
UNION
SELECT 'Final Draft'
FROM DUAL
UNION
SELECT 'Final Comment'
FROM DUAL
UNION
SELECT 'Final Document'
FROM DUAL
UNION
SELECT 'Archived'
FROM DUAL)
SELECT DECODE ("stat",
'Mature', 'Mature Draft',
'Review', 'Review',
'Final Draft', 'Final Draft',
'Final Comment', 'Final Comment',
'Final Document', 'Final Document',
'Archived', 'Archived Document'
) AS "progress_CMC_status",
NVL
(TO_CHAR ("progress_cmc_status_numbers"),
0
) AS "progress_cmc_status_numbers",
NVL (TO_CHAR ("cmc_status_numbers"), 0) AS "cmc_status_numbers"
FROM (SELECT "status1" AS "stat",
NVL
(TO_CHAR (NULL),
"progress_cmc_status_numbers"
) AS "progress_cmc_status_numbers",
NVL (TO_CHAR (NULL),
"cmc_status_numbers"
) AS "cmc_status_numbers"
FROM sta
LEFT JOIN
(SELECT VALUE AS "progress_CMC_status",
COUNT (*) AS "progress_cmc_status_numbers",
SUM (COUNT (*)) OVER (ORDER BY DECODE
(VALUE,
'Mature', 0,
'Review', 1,
'Final Draft', 2,
'Final Comment', 3,
'Final Document', 4,
'Archived', 5,
6
) DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
AS "cmc_status_numbers"
FROM ATTRIBUTES
WHERE attribute_type_id IN (
SELECT attribute_type_id
FROM attribute_types
WHERE name_display_code =
'RD')
AND VALUE IN
('Mature', 'Review', 'Final Draft',
'Final Comment', 'Final Document', 'Archived')
AND object_id IN (SELECT obj_id
FROM docs)
GROUP BY VALUE) a1 ON sta."status1" =
a1."progress_CMC_status"
)
ORDER BY DECODE ("progress_CMC_status",
'Mature Draft', 0,
'Review', 1,
'Final Draft', 2,
'Final Comment', 3,
'Final Document', 4,
'Archived Document', 5,
6
)
sample data
CREATE TABLE ATTRIBUTES
(
object_id NUMBER(4),
attribute_type_id NUMBER(4),
name_display_code VARCHAR2(5),
VALUE VARCHAR2(25)
);
begin
insert into attributes values (101,201,'RMD','A100');
insert into attributes values (102,201,'RMD','A200');
insert into attributes values (103,201,'RMD','A300');
insert into attributes values (104,200,'RD','Mature');
insert into attributes values (105,200,'RD','Mature');
insert into attributes values (106,200,'RD','Mature');
insert into attributes values (107,200,'RD','Mature');
insert into attributes values (108,200,'RD','Mature');
insert into attributes values (109,200,'RD','Archived');
insert into attributes values (110,200,'RD','Archived');
insert into attributes values (111,200,'RD','Archived');
insert into attributes values (112,200,'RD','Archived');
insert into attributes values (113,200,'RD','Mature');
insert into attributes values (114,200,'RD','Mature');
insert into attributes values (115,200,'RD','Mature');
insert into attributes values (116,200,'RD','Mature');
insert into attributes values (117,200,'RD','Final Document');
insert into attributes values (118,200,'RD','Final Document');
insert into attributes values (119,201,'RMD','A400');
insert into attributes values (120,201,'RMD','A500');
end;
/
CREATE TABLE docs
(
obj_id NUMBER(4)
);
BEGIN
INSERT INTO docs
VALUES (100);
INSERT INTO docs
VALUES (104);
INSERT INTO docs
VALUES (109);
INSERT INTO docs
VALUES (117);
INSERT INTO docs
VALUES (118);
INSERT INTO docs
VALUES (119);
INSERT INTO docs
VALUES (120);
END;
/
CREATE TABLE attribute_types
(
attribute_type_id NUMBER(4),
name_display_code VARCHAR2(5)
);
INSERT INTO attribute_types
VALUES (200, 'RD');
INSERT INTO attribute_types
VALUES (201, 'RMD');
WITH sta AS
(SELECT 'Mature' "status1" FROM DUAL UNION
SELECT 'Review' FROM DUAL UNION
SELECT 'Final Draft' FROM DUAL UNION
SELECT 'Final Comment' FROM DUAL UNION
SELECT 'Final Document' FROM DUAL UNION
SELECT 'Archived' FROM DUAL)
SELECT
DECODE ("stat",
'Mature', 'Mature Draft',
'Review', 'Review',
'Final Draft', 'Final Draft',
'Final Comment', 'Final Comment',
'Final Document', 'Final Document',
'Archived', 'Archived Document'
) AS "progress_CMC_status",
NVL("progress_cmc_status_numbers", 0) AS "progress_cmc_status_numbers",
SUM (NVL("progress_cmc_status_numbers", 0)) OVER (ORDER BY
-DECODE ("stat",
'Mature', 0,
'Review', 1,
'Final Draft', 2,
'Final Comment', 3,
'Final Document', 4,
'Archived', 5,
6)
) AS "cmc_status_numbers"
FROM
(
SELECT
"status1" AS "stat",
"progress_cmc_status_numbers"
FROM
sta
LEFT JOIN (
SELECT
VALUE AS "progress_CMC_status",
COUNT (*) AS "progress_cmc_status_numbers"
FROM ATTRIBUTES
WHERE
attribute_type_id IN (
SELECT attribute_type_id
FROM attribute_types
WHERE name_display_code = 'RD'
)
AND VALUE IN ('Mature', 'Review', 'Final Draft',
'Final Comment', 'Final Document', 'Archived')
AND object_id IN (SELECT obj_id FROM docs)
GROUP BY VALUE
) a1 ON sta."status1" = a1."progress_CMC_status"
)
ORDER BY DECODE ("progress_CMC_status",
'Mature Draft', 0,
'Review', 1,
'Final Draft', 2,
'Final Comment', 3,
'Final Document', 4,
'Archived Document', 5,
6)
fiddle

Resources