I have a WITH clause that gives me the desired result, and i'm trying to put it in a variable. I have omitted code for simplicity. The last line of code is most relevant.
WITH ALL_VE_ERRORS AS (
SELECT *
FROM ASN.AN_VALIDATION_ERRORS
WHERE ...
), FILTER_STATUS AS (
SELECT *
FROM ALL_VE_ERRORS
WHERE ...
) SELECT UNIT_DISCREPANCY FROM FILTER_STATUS INTO W_UNIT_DISCREPANCY; <-- like this
But this doesn't work, the compiler doesn't like this. I also tried putting it first like this:
SELECT INTO W_UNIT_DISCREPANCY <-- or like this
WITH ALL_VE_ERRORS AS (...
Anyone know the proper syntax to do something like this?
If you have no reasons I can't see, you don't need two tables in your WITH clause; you could simplify it this way:
WITH ALL_VE_ERRORS AS (
SELECT *
FROM ASN.AN_VALIDATION_ERRORS
WHERE ...
)
SELECT UNIT_DISCREPANCY
INTO W_UNIT_DISCREPANCY
FROM ALL_VE_ERRORS
WHERE ...
Otherwise, you can use:
WITH ALL_VE_ERRORS AS (
SELECT *
FROM ASN.AN_VALIDATION_ERRORS
WHERE ...
), FILTER_STATUS AS (
SELECT *
FROM ALL_VE_ERRORS
WHERE ...
)
SELECT UNIT_DISCREPANCY
INTO W_UNIT_DISCREPANCY
FROM FILTER_STATUS
Related
I have a query something Like this
Set of columns : { user_id , user_details,date }
I have set of conditional values like : { 1 , AAA , 09-03-2021 } , { 2 , BBB , 08-02-2021 }
I am trying to add the conditions in same select query as I need to get the data at a same time ,
Have tried below query
SELECT * FROM USERS WHERE ( user_id , user_details , date ) in ( select 1 , AAA , 09-03-2021 from dual ) ;
The above was working properly , when I use the below , I couldnt fetch Data , Can someone please if there is any provision to fetch data for all the set of conditons .
SELECT * FROM USERS WHERE ( user_id , user_details , date ) in (( select 1 , AAA , 09-03-2021 from dual ) , ( select 2 , BBB , 08-02-2021 from dual ) );
You are using the wrong syntax, even for a single tuple to compare to. The two cases should look like this:
where (user_id, user_details, date) in ( (1 , 'AAA' , to_date('09-03-2021')) )
and
where (user_id, user_details, date) in ( (1 , 'AAA' , to_date('09-03-2021')),
(2 , 'BBB' , to_date('08-02-2021')) )
There is no need to select anything from DUAL in the IN lists. The IN condition can also be used when the values on the right-hand side come from a table (in which case you would select three columns from that table, not from DUAL), but that's not what you are using here - here you are using hard-coded values, and the syntax doesn't require SELECT in the IN list.
Note that in your question you use values like 1, AAA, 09-03-2021. I hope you know that's wrong. 1 is fine, it's a number. AAA not enclosed in single quotes is not a string - it will be interpreted as a column name, and an error will be thrown because your table doesn't have a column named AAA. And 09-03-2021 is seen as a simple arithmetic expression involving three numbers, with minus signs (subtractions) between them. If this confuses you, that's a big problem.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have code to create a view, for one column (TERMINAL_DEGREE) I am trying to cast the value of a query which contains a WITH clause. Any help is greatly appreciated.
I get the following error:
[Error] Execution (306: 1): ORA-00936: missing expression
Here is my pseudo code (The query inside of the cast() for TERMINAL_DEGREE works perfectly fine outside of the create view statement):
CREATE OR REPLACE FORCE VIEW CURRENT_FSYR
( .....
TERMINAL_DEGREE,
.......)
BEQUEATH DEFINER
AS
WITH
src
AS
SELECT AS_OF_DATE
AS_OF_DATE,
PERSON_SKEY
PERSON_SKEY
........
FROM REPORT_DETAILS
GROUP BY AS_OF_DATE,
PERSON_SKEY
.........)
SELECT (
...., --OTHER COLUMNS
CAST (WITH HIGHEST_DEG_VALUE AS --THE COLUMN WITH THE ISSUE
(
....)
SELECT CASE
.....
END
FROM HIGHEST_DEG_VALUE
AS VARCHAR2(20))
TERMINAL_DEGREE,
...... --OTHER COLUMNS
FROM ...
;
Yes, you can:
CREATE OR REPLACE FORCE VIEW CURRENT_FSYR
( id, string_value )
BEQUEATH DEFINER
AS
WITH data AS ( -- need a bracket here
SELECT rownum AS id, dummy FROM DUAL
)
SELECT id,
CAST(
(
WITH src AS (
SELECT 1 AS value
FROM DUAL
)
SELECT CASE value
WHEN 1 THEN value || value
ELSE NULL
END
FROM src
)
AS VARCHAR2(20)
) AS string_value
FROM data
(You are missing most of your code so I made up the missing bits.)
Then:
SELECT * FROM current_fsyr
Outputs:
ID | STRING_VALUE
-: | :-----------
1 | 11
db<>fiddle here
In function of myVariable, this select is made from a different table.
select
*
from
table(decode(myVariable,1,sys.odcinumberlist(1,2),sys.odcinumberlist(3)))
result
if myVAriable =1 then 1,2
else 3
I would like to to make the same thing but instead of having sys.odcinumberlist, I would like to have the list of all integer inferior than 100.
I have tried a lot of way to do that. This for instance, but none has worked
select *
from
table(decode(myvariable,1,table(select level from dual connect by level<100),sys.odcinumberlist(3)))
wanted result:
myvariable =1 then 1,2,3....99
else 3
You have to cast this subquery as odcinumberlist:
select * from
table(decode(myvariable, 1,
cast(multiset(
select level from dual connect by level<100)
as sys.odcinumberlist),
sys.odcinumberlist(3)))
How to loop Oracle query through the date? I have to put variable in 4 place. My query start with WITH AS, so I can't use Oracle SQL Loop through Date Range solution.
I also can't create temporary table.
Here is my attempt:
WITH d
AS (
SELECT DATE'2015-06-22' + LEVEL - 1 AS current_d
FROM dual
CONNECT BY DATE'2015-06-22' + LEVEL - 1 < DATE'2015-10-04'
),
OrderReserve
AS (
SELECT cvwarehouseid
,lproductid
,SUM(lqty) lqty
FROM ABBICS.iOrdPrdQtyDate
GROUP BY cvwarehouseid
,lproductid
)
SELECT
...
WHERE IORDREFILL.DNCONFIRMEDDELDATE < CAST(TO_CHAR(d.current_d , 'YYYYMMDD') AS NUMBER(38))
...
If I understand you correctly, you assume that you can only use 1 inline table per query. That is not true, you can use multiple inline tables and expand the existing WITH clause with another to loop through dates:
with OrderReserve as (
SELECT cvwarehouseid
,lproductid
,SUM(lqty) lqty
FROM ABBICS.iOrdPrdQtyDate
GROUP BY cvwarehouseid
,lproductid
), date_range as (
select sysdate+level
from dual
connect by level <= 30
)
select *
from OrderReserve, date_range
... -- expand with date_range as you see fit
;
The code I'm using is below. The issue is that the items I'm pivoting on go last in the naming convention. I.E null_pdiv_spend, null_pdiv_visits. I would like to have the naming convention be spend_null_pdiv to coincide with the rest of my tables. Please advise.
pivot(
count(x) as visits,
sum(y) as spend,
sum(c) as items,
sum(b) as mkdn
for pdiv_cd in (
'###' as null_pdiv,
'010' as hbags_acc,
'011' as fine_jewelry,
'070' as pdiv_70
)
)
You can add another level of subquery, and rename the columns with aliases:
select null_pdiv_spend as spend_null_pdiv,
null_pdiv_visits as visits_null_pdiv,
...
from (
select *
from (
...
)
pivot(count(x) as visits, sum(y) as spend, sum(c) as items, sum(b) as mkdn
for (pdiv_cd) in (
'###' as null_pdiv,
'010' as hbags_acc,
'011' as fine_jewelry,
'070' as pdiv_70
)
)
)
That lets you modify the order of the columns in the result set too, if that matters to you.