Subquery help
Hi there,
I have a subquery and based on the result of one of the columns in subquery i need to call a function.if function returns true then i need to call another query else null.
With target_subquery
(select a column1,b column2,count(c) column3
from table_1,table_2,table_3
where table_1.pk=table_2.pk and table2_.pk=table_3.pk
group by a,b,c
order by c)
case when is_parameter_gt_10(column1) then
select count(d) into l_cnt from table_4,target_subquery where d=column2 and e=column1 and column3 > 12
else
l_cnt=0;
end case;Thanks
Edited by: user469956 on Dec 19, 2008 2:28 AM
Firstly SQL Does not support BOOLEAN data type. Only PL/SQL does. So you must change your function to return some thing else like 1 or 0.
And about the query you can do something like this
With target_subquery
SELECT a column1,b column2,count(c) column3
FROM table_1,table_2,table_3
WHERE table_1.pk=table_2.pk and table2_.pk=table_3.pk
GROUP by a,b,c
ORDER by c
SELECT CASE WHEN is_parameter_gt_10(column1) = 1
THEN count(d)
ELSE count(d)-count(d)
END
INTO l_cnt
FROM table_4,target_subquery
WHERE d=column2
AND e=column1
AND column3 > 12
Similar Messages
-
Correlatd Subquery help needed for 8i
Hi Everyone,
I am struggling with a correlated subquery, the individual parts of which work fine, but not together! I am guessing that this is something to do with the SQL implementation in Oracle 8i and the way that it handles table references, but I am not sure how to approach this for a workable solution. Any help would be greatly appreciated.
Here is my SQL:
select
TA.JAHR as yr,
TB.WOCHE as week,
(select count(*) from (
SELECT T2.JAHR
, T2.KUNDEN_ID
, CDW.PRODUKTE.EAN_VBE
, T3.WOCHE
, Sum(T2.ANZ_VSE)
FROM CDW.RECHNUNGSPOSITIONEN T2
JOIN CDW.PRODUKTE ON CDW.PRODUKTE.ID = T2.PRODUKT_ID
JOIN CDW.KALENDER T3 ON T3.DATUM = T2.RECHNUNG_DATUM
WHERE T2.JAHR = TA.JAHR
AND CDW.PRODUKTE.SEGMENT Like 'PET%'
AND T3.WOCHE = TB.WOCHE
AND T2.AUFTRAGS_ART_ID NOT IN (39,48)
AND CDW.PRODUKTE.EAN_VBE <> 0
GROUP BY T2.JAHR
, T2.KUNDEN_ID
, T3.WOCHE
, CDW.PRODUKTE.EAN_VBE
)) as qty
FROM
CDW.RECHNUNGSPOSITIONEN TA
JOIN
CDW.KALENDER TB ON TB.DATUM = TA.RECHNUNG_DATUM
WHERE
TA.JAHR >= 2005
GROUP BY
TA.JAHR,
TB.WOCHE
It looks as though the inner query cannot use the table references in the outer query. Am I missing something?
Cheers and thanks in advance
The FrogThe SQL Standard defines correlation variables at only one level deep. In your case you were trying to reference a table (TA and TB) at the 2nd level. In this case it may be easier to remove this inline subquery altogether. I tried to simplify your query. This is just a guess and it's not tested:
SELECT TA.JAHR as yr
, TB.WOCHE as week
, COUNT(*) as qty
FROM CDW.RECHNUNGSPOSITIONEN TA
JOIN CDW.KALENDER TB ON TB.DATUM = TA.RECHNUNG_DATUM
JOIN CDW.RECHNUNGSPOSITIONEN T2 ON TA.JAHR = T2.JAHR
JOIN CDW.PRODUKTE ON CDW.PRODUKTE.ID = T2.PRODUKT_ID
JOIN CDW.KALENDER T3 ON T3.DATUM = T2.RECHNUNG_DATUM
AND T3.WOCHE = TB.WOCHE
WHERE CDW.PRODUKTE.SEGMENT Like 'PET%'
AND T2.AUFTRAGS_ART_ID NOT IN (39,48)
AND CDW.PRODUKTE.EAN_VBE != 0
AND TA.JAHR >= 2005
GROUP BY TA.JAHR
, TB.WOCHEIt's always helpful to post the following:
1. FULL Oracle version (SELECT * FROM V$VERSION)
2. Sample data in the form of CREATE / INSERT scripts.
3. Expected output.
4. Explanation on how to achieve expected output.
5. Please use \ tags to surround anything that requires fixed-width spacing like SQL statements. -
Union All in a Subquery - Help
Hello All,
I am trying to create a Union All query but when i put a parameter in the where clause, i get an error. The Query is mentioned below:
SELECT T0.U_Srv_TechName, T0.ItemCode, T0.Dscription, SUM(T0.Quantity) AS [Bal Qty]
FROM (SELECT dbo.OWTR.U_Srv_TechName, dbo.WTR1.ItemCode, dbo.WTR1.Dscription, SUM(dbo.WTR1.Quantity) AS Quantity
FROM dbo.OWTR INNER JOIN
dbo.WTR1 ON dbo.OWTR.DocEntry = dbo.WTR1.DocEntry
GROUP BY dbo.OWTR.U_Srv_TechName, dbo.WTR1.ItemCode, dbo.WTR1.Dscription
UNION ALL
SELECT dbo.ODLN.U_Srv_TechName, dbo.DLN1.ItemCode, dbo.DLN1.Dscription, SUM(dbo.DLN1.Quantity * - 1) AS Quantity
FROM dbo.ODLN INNER JOIN
dbo.DLN1 ON dbo.ODLN.DocEntry = dbo.DLN1.DocEntry
GROUP BY dbo.ODLN.U_Srv_TechName, dbo.DLN1.ItemCode, dbo.DLN1.Dscription) T0
GROUP BY T0.U_Srv_TechName, T0.ItemCode, T0.Dscription
HAVING T0.U_Srv_TechName = [%0]
It gives me the error
1). [Microsoft][SQL Native Client][SQL Server]Must specify table to select from.
2). [Microsoft][SQL Native Client][SQL Server]Statement 'Service Contracts' (OCTR) (s) could not be prepared.
If I enter the name of the Technician (HAVING T0.U_Srv_TechName = 'xyz') it works fine. Also, if I execute the query in SQL Server with or without a prompt, it works fine. It's only on the SAP Query Manager that i'm having this problem. Please Help!
RezaReza,
Try this modification of the previous code, see if it works for you.
declare @stn varchar(20)
set @stn=/*SELECT T0.[DocNum] FROM OWTR T0 WHERE T0.[U_Srv_TechName] =*/'[%0]'
SELECT T0.U_Srv_TechName, T0.ItemCode, T0.Dscription, SUM(T0.Quantity) AS [Bal Qty]
FROM (SELECT T0.U_Srv_TechName, T1.ItemCode, T1.Dscription, SUM(T1.Quantity) AS Quantity
FROM OWTR T0 INNER JOIN
WTR1 T1 ON T0.DocEntry = T1.DocEntry
GROUP BY T0.U_Srv_TechName, T1.ItemCode, T1.Dscription
UNION ALL
SELECT T2.U_Srv_TechName, T3.ItemCode, T3.Dscription, SUM(T3.Quantity * - 1) AS Quantity
FROM ODLN T2 INNER JOIN
DLN1 T3 ON T2.DocEntry = T3.DocEntry
GROUP BY T2.U_Srv_TechName, T3.ItemCode, T3.Dscription) T0
GROUP BY T0.U_Srv_TechName, T0.ItemCode, T0.Dscription
HAVING T0.U_Srv_TechName =@stn
Hope this helps,
Lucas -
Hi All ,
I have one scnario like following ...
i have some records . So i want ids
SID SNO S.No2
123 1
1 321 4
789 8
456 9
456 0
789 2
2 456 7
741 3
452 5
789 3
458 5
3 784 4
745 9
852 0
852 1
In Above 3 cases Consider the SNO2 numbers...
SId (1) have some different no combination of S.No2.
SId (2) have some different no combinations of S.No2.
But SId (3) have Both the combination of SId(1) and SId(2)
I need SId no like 3 which have the combinations
Is there any SQL Select Query .... ? Plese help me on this...
Thanks,
SriHi All ,
I have one scnario like following ...
i have some records . So i want ids
SID SNO S.No2
123 1
1 321 4
789 8
456 9
456 0
789 2
2 456 7
741 3
452 5
789 3
458 5
3 784 4
745 9
852 0
852 1
In Above 3 cases Consider the SNO2 numbers...
SId (1) have some different no combination of S.No2.
SId (2) have some different no combinations of S.No2.
But SId (3) have Both the combination of SId(1) and SId(2)
I need SId no like 3 which have the combinations
Is there any SQL Select Query .... ? Plese help me on this...
Thanks,
Sri -
Hi I've the following tables and the requirement
create table test1 (id number,rk varchar2(11),rk_cd varchar2(11))
create table test2 (id number,rk1 varchar2(11),rk_cd1 varchar2(11))
insert into test1 values(1,'I+','LG')
insert into test1 values(2,'I-','SH')
insert into test2 values(1,'I+','LG')
insert into test2 values(1,'I+','LG')for a single id in TEST1 Table there could be multiple rows in TABLE2, so i need to get the matching records in both tables
I tried the folloiwng query to achieve the same
select * from test1
where (rk,rk_cd) = all (select NVL(rk1,'X'),NVL(rk_cd1,'X')
from test2
where TEST1.ID=test2.id The above query is returning me even the id=2 , where id=2 doesn't exist in TEST2
Could you hint me the changes i need to do in subquery.
Expected output
Only id=1 record Thank youHi,
Perhaps you meant to say ANY instead of ALL:
SELECT *
FROM test1
WHERE (rk, rk_cd) = ANY (
SELECT NVL (rk1, 'X')
, NVL (rk_cd1, 'X')
FROM test2
WHERE test1.id = test2.id
;Most people would use "IN" rather than "= ANY".
Do you really mean what you're saying with NVL? If that's important, then include some examples in the sample data and results where it matters. For example:
INSERT INTO test1 (id, rk, rk_cd) VALUES (3, 'I+', 'X');
INSERT INTO test2 (id, rk1, rk_cd1) VALUES (3, 'I+', NULL);Edited by: Frank Kulash on Jul 9, 2012 6:15 AM -
Discoverer Subquery Help needed
Hi everyone,
I have a problem creating a nested subquery in Discoverer 4.
I want to create three sheets: A, B and C. Sheet A is a query on the database, retrieving all Id's that meet a certain condition. Sheet B needs to referr to sheet A and query the database for those Id's from A that meet another set of conditions. Its not possible to create a condition that combines the two, because it would require two loops over the database.
Now, here is the problem: sheet C needs to referr to the results of both sheets A and B to find a certain result in the database.
The problem is that Discoverer doesn't allow me to referr to two different subqueries in the same condition.
For example:
C_Id = A_Id (Subquery sheet A)
AND
C=Id <> B_Id (Subquery sheet B)
Does anyone know how to create those three sheets and make sheet C able to referr to both sheets A and B as a subquery?Hi Rod,
Thanks for the reply.
I could find out that it was a view that was created when i had scheduled a workbook and it became invalid because we had changed the schema of the views being used the report and dropped the views from the earlier schema.
I again have one question, when the report is scheduled there are two objects formed in the backend :
EUL5_B090912142833Q1V1
EUL5_B090912142833Q1R1
The only difference between the two is that one is a view(with V1 suffixed) and the other is a table (with R1 suffixed).
What is the significance of these two objects and does the view becoming invalid impact anything as the scheduled report fetches data from the table EUL5_B090912142833Q1R1.
Regards,
Shruti -
I can't use this query on 10G, but can use on 9i
I have ever used bellow query and no error
SQL>select so.* from bsowner.sales_orders so left join bsowner.relation_address rla on (so.customerid = rla.relationid and so.delivery_addressid = rla.addressid ) and rla.addresstype = (select code from bsowner.tab_addtyp where config1=3)
When I use on 10G, I can't query
1* select so.* from bsowner.sales_orders so left join bsowner.relation_address rla on (so.customerid = rla.relationid and so.delivery_addressid = rla.addressid ) and rla.addresstype = (select code from bsowner.tab_addtyp where config1=3)
SQL> /
select so.* from bsowner.sales_orders so left join bsowner.relation_address rla on (so.customerid = rla.relationid and so.delivery_addressid = rla.addressid ) and rla.addresstype = (select code from bsowner.tab_addtyp where config1=3)
ERROR at line 1:
ORA-01799: a column may not be outer-joined to a subquery
Help me,Please
Why I can't use this on 10G?
when i change = to in , i can use
but i don't want to change
help me for the reasonI don't this gonna work on 9i either.
Chang your query to
select so.* from bsowner.sales_orders so left join bsowner.relation_address rla on (so.customerid = rla.relationid and so.delivery_addressid = rla.addressid )
Where
rla.addresstype = (select code from bsowner.tab_addtyp where config1=3) -
Need Help in creating a subquery using JOIN operation
Hi,
I am new to SQL and trying to write a query. Please help me in this.
The Database Schema looks like this:
The database scheme consists of four tables:
Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)
The table "Product" includes information about the maker, model number, and type ('PC', 'Laptop', or 'Printer'). It is assumed that model numbers in the Product table are unique for all the makers and product types. Each PC uniquely specifying by a code in the table "PC" is characterized by model (foreign key referencing to Product table), speed (of the processor in MHz), total amount of RAM - ram (in Mb), hard disk drive capacity - hd (in Gb), CD ROM speed - cd (for example, '4x'), and the price. The table "Laptop" is similar to that one of PCs except for the CD ROM speed, which is replaced by the screen size - screen (in inches). For each printer in the table "Printer" it is told whether the printer is color or not (color attribute is 'y' for color printers; otherwise it is 'n'), printer type (laser, jet, or matrix), and the price.
Alternatively you can refer here for the schema (Computer Firm)
http://www.sql-ex.ru/help/select13.php#db_1
Now my query is :
Find the makers of the cheapest color printers.
Result set: maker, price.
and I have written the query as:
SELECT prod.maker, prin.* FROM (SELECT MIN(price) AS price FROM printer WHERE color = 'y') AS prin INNER JOIN (SELECT maker, model FROM product) AS prod ON prin.model = prod.model
Which is giving the error "Invalid column name 'model'.". Please help!
Thanks!
FaazHello,
Your query does not work because your subquery prin does not return a column named model.
Analytical functions like Vivek L suggests are the best way to do this, but this one is a correction of your query and should work too (not tested, unfortunately).
SELECT prod.maker, prin2.*
FROM (SELECT MIN(price) AS min_price FROM printer WHERE color = 'y') AS prin
INNER JOIN (SELECT model, price FROM printer WHERE color='y' ) AS prin2 ON prin.min_price = prin2.price
INNER JOIN (SELECT maker, model FROM product) AS prod ON prin2.model = prod.model;Regards,
Sylvie -
Help With Passing Values to a Subquery
Hello,
I am new to the SQL programming language. I have a fairly simple query that reads as follows:
SELECT Utility_Type,Instance_ID, WINS_Current_Amount/
(SELECT AVG
(CASE WHEN (Corrected_Usage_Standardized IS NULL AND Extrapolated_Usage IS NOT NULL)
THEN (WINS_Current_Amount/Extrapolated_Usage)
WHEN (Corrected_Usage_Standardized IS NOT NULL AND Extrapolated_Usage IS NULL)
THEN (WINS_Current_Amount/Corrected_Usage_Standardized)
END)
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
WHERE WINS_Account_Number = '021202000'
AND (Corrected_Usage_Standardized IS NOT NULL OR Extrapolated_Usage IS NOT NULL)
AND (('10-01-2013' <= WINS_Invoice_Date) AND (WINS_Invoice_Date <= '09-30-2014')))
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
WHERE WINS_Account_Number = '021202000'
AND (Corrected_Usage_Standardized IS NULL AND Extrapolated_Usage IS NULL)
AND (('10-01-2013' <= WINS_Invoice_Date) AND (WINS_Invoice_Date <= '09-30-2014'))
All the data I am concerned with is coming from a single table with a long-winded name (All_Utility_Data_Standardized_With_Extrapolated_Values). Essentially, the subquery takes the average of the calculated unit cost and applies that unit cost to where values
for the Current_Amount are not corrected or extrapolated. This query works great when I manually input the account number i.e. ('021202000'). But now I want to pass it 3,283 other accounts which I obtain from the following query using the same table:
SELECT DISTINCT(WINS_Account_Number)
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
WHERE Corrected_Usage_Standardized IS NULL
AND Extrapolated_Usage IS NULL
AND ((WINS_Invoice_Date > '10-01-2013') AND (WINS_Invoice_Date < '09-30-2014'))
I tried to put this query in place of '021202000' but it says that Subquery returned more than 1 value which I understand why, but I'm not sure how to fix. All I want to do is one-by-one place a new account number in the bold statement in the first query
and update a table using the calculated values. Each account may have any number of results (multiple averaged results), but I need to make sure the averaging only occurs using values specific to the account entered in bold. If that makes any sense. Anyways,
any help would be much appreciated as I am just starting to learn SQL. Thanks.
KevinHi Kevin,
To fix the subquery error you just need to change "=" to "IN".
However, the query will probably run a lot faster if you use an inner join instead, like so:
SELECT Utility_Type,Instance_ID, WINS_Current_Amount/
(SELECT AVG
(CASE WHEN (Corrected_Usage_Standardized IS NULL AND Extrapolated_Usage IS NOT NULL)
THEN (WINS_Current_Amount/Extrapolated_Usage)
WHEN (Corrected_Usage_Standardized IS NOT NULL AND Extrapolated_Usage IS NULL)
THEN (WINS_Current_Amount/Corrected_Usage_Standardized)
END)
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
INNER JOIN
SELECT DISTINCT(WINS_Account_Number)
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
WHERE Corrected_Usage_Standardized IS NULL
AND Extrapolated_Usage IS NULL
AND ((WINS_Invoice_Date > '10-01-2013') AND (WINS_Invoice_Date < '09-30-2014'))
) Accts ON All_Utility_Data_Standardized_With_Extrapolated_Values.WINS_Account_Number = Accts.WINS_Account_Number
AND (Corrected_Usage_Standardized IS NOT NULL OR Extrapolated_Usage IS NOT NULL)
AND (('10-01-2013' <= WINS_Invoice_Date) AND (WINS_Invoice_Date <= '09-30-2014')))
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
INNER JOIN
SELECT DISTINCT(WINS_Account_Number)
FROM All_Utility_Data_Standardized_With_Extrapolated_Values
WHERE Corrected_Usage_Standardized IS NULL
AND Extrapolated_Usage IS NULL
AND ((WINS_Invoice_Date > '10-01-2013') AND (WINS_Invoice_Date < '09-30-2014'))
) Accts ON All_Utility_Data_Standardized_With_Extrapolated_Values.WINS_Account_Number = Accts.WINS_Account_Number
AND (Corrected_Usage_Standardized IS NULL AND Extrapolated_Usage IS NULL)
AND (('10-01-2013' <= WINS_Invoice_Date) AND (WINS_Invoice_Date <= '09-30-2014'))
Cheers
Lucas
LucasF -
Hi All,
I need help on a subquery to return multiple rows. I included this subquery in the select statement. Can someone help on the query
select a.total from TableA a where a.ID = b.ID and a.type = 'R' or a.total = 0
Thanks
VaniHere is the example. My req is to calculate the amount and reduced amount from the amount column based on the status
Cust_no
Status
Amount
12345
Regular
$50.26
12345
Discount
$12
22222
Regular
$233
22222
Discount
$2
23334
DEFG
0
23333
ABCD
0
I used CASE statement to the below output but the result was in multiple rows
(Case when a.status <> ‘Discount’ then i.total) Amount
(Case when a.status = ‘Regular’ then i.total) Reduced_Amount
Cust_no
Status
Amount
Reduced Amount
12345
Regular
$50.26
12345
Discount
$12
22222
Regular
$233
22222
Discount
$2
So I wrote a subquery to get the below output
select a.amount from TableA a where a.ID = b.ID and a.Status = 'R' and a.amount != 0
Cust_no
Amount
Reduced Amount
12345
$50.26
$12
22222
$233
$2
Now my req is to the get multiple rows from the subquery. How can i achieve that.
select a.amount from TableA a where a.ID = b.ID and a.Status <> ‘Discount’ OR a.amount = 0
Thanks,
Vani -
Subquery returns 0 rows-----HELP
I need to understand something.
please follow my points:-
1-If we try to make a NOT IN condition, and one value is NULL, the main query return no result, because the NOT IN condition evaluates to FALSE "NULL" "UNKNOWN".
example:-
SELECT 'True' FROM employees
WHERE department_id NOT IN (10, 20, NULL);
this query returns no rows, simply because the condition is parsed like this
department_id != 10 AND department_id != 20 AND department_id != null
^^^I have no question regarding this point and it is quite obvious.
2-If the subquery returns 0 rows, then the value of the scalar subquery expression is NULL.
example:-
create table subq_null_test
num number(4),
val varchar2(7)
insert into subq_null_test
values (1,'one');
insert into subq_null_test
values (2, (select 'two' from dual where 2=1));
insert into subq_null_test
values (3, 'three');
commit;
and by
select * from subq_null_test;
we would see a NULL inserted as value for number 2
NUM VAL
1 one
2 (null)
3 three
so far so good, indeed the 0 row subquery returned NULL
^^^Also I CAN'T have a point here.
============================================
but lets look at these 3 queries
-------->FIRST
select department_id, last_name
from employees
where department_id not in (10,20,null)
/*no rows selected*/
--------->SECOND
select department_id, last_name
from employees
where department_id not in (10,20,(select 10 from dual where 2=1))
/*no rows selected*/
-------->THIRD
select department_id, last_name
from employees
where department_id not in (select 10 from dual where 2=1)
/*ROWS returned*/
my question is:-
WHY FIRST and SECOND queries behaved as expected, while the THIRD didn't ???
-I had a look at the execution plan, and it didn't helped me "am a beginner anyways"
-I know its something related to the process or parsing the conditions, but am totally unable to locate it...
Any help would be so much appreciated,
Thanks for all.
Ghazal.Hi again
Yes; while I was writing my reply, you corrected it.Now it seems like you're making the same mistake again.I believe I had clarified it enough.
Once again, I was only talking about the situation where department_id is NULL.Of course "x IN y" can be TRUE, FALSE or UNKNOWN, depending of what x and y are. All I was saying is that "NULL IN y" can be FALSE (depending on y), even though "NULL = q" is always UNKNOWN, regardless of what q is, and I find that a little inconsistent.-now we are on the same side "I hope :)...jk"...well yea we are talking about the department_id "left-hand" operand is NULL.
-Regarding YOUR point, I can't agree with you Sir.
I see it pretty much consistent that when the left-hand operand is NULL,,,it is always UNKNOWN even if the right-side operand is a null.
I would even go further and say if there is a using of word "regardless", it wold be like this
if the left-hand operand is NULL the condition evaluates to UNKNOWN no matter what is in the right-hand operand REGARDLESS what condition we are using IN or NOT IN.
so
"x IN y" can be TRUE, FALSE or UNKNOWN, depending of what x and y are. All I was saying is that so far so good
"NULL IN y" can be FALSE (depending on y), even thoughNo, it is ALWAYS UNKNOWN 'you can call is FALSE'.
"NULL = q" is always UNKNOWN, regardless of what q is, and I find that a little inconsistent.I find it okay.
-"NULL = q" is always UNKNOWN regardless of what q is, is perfect and consistent indeed.
-"NULL in (q, p, d)" is always UNKNOWN.
-"NULL NOT in (q, p, d)" is always UNKNOWN.
I would again dare and go further and say
-whenever we have NULL on the left-hand operand
result of the condition is only 2 cases and can never be 3
1-UNKNOWN :- if we use "=, !=, IN, NOT IN, or anything may exists"
2-TRUE :- if we use "IS"
-this case of NULL as our left-hand operand can never have a FALSE case...
ORACLE does do this for us by her own, and thanks ORACLE for that...alot.
select 'BANG' from dual where null is null;
select 'BANG' from dual where (select department_id from employees where last_name = 'kokolala') in (10,20, null);
select 'BANG' from dual where (select department_id from employees where last_name = 'kokolala') is null ;This all is perfect, consistent, logical and cute.
Solomon Yakobson : Think about the dirrerence between empty list and list containing a null element.This is cool, correct. this is the actual solution to the matter of having a null in our right-hand operand "BY JUST IGNORING THE NULLS, and is the list have only nulls it becomes an empty list", but in our case we do NOT have an EMPTY list, we have a list with a NULL indeed.
-ORACLE just under the hood did us a favor of giving us an EMPTY list by ignoring the nulls using "LNNVL", which is doing no good..let him experience it, let him taste it, let him avoid it, him him him design it. and do not give me an inconsistent mechanism. it is good to sell that ORACLE is treating results as lists but that shouldn't intervene with our system-level language, am boooring guy, if its designed to work like this, no am sorry it should be stopped now.
My point is
-This theory is great regarding the matter when we are talking about dealing with our result as a LIST of elements, we would take in consideration our NULL values, which what SHOULD be implemented by the coder.
-BUT the issue of oracle implements "LNNVL" while parsing our codes INCASE and ONLY in the case of subquery as our right-hand operand, I am not buying it and i see it inconsistent.
They should implements it in all cases, or not at all "not even in one case as an exception", this is logic and it cant be divided or argued.Am still hoping someone have an explanation to the matter, I wouldn't like it at all to know that am 100% correct in my analysis of this case, because if I am I would like to see them changing it.
Edited by: Ghazal-OCA on Jan 8, 2013 7:04 AM
Edited by: Ghazal-OCA on Jan 8, 2013 7:10 AM
Edited by: Ghazal-OCA on Jan 8, 2013 7:19 AM
Edited by: Ghazal-OCA on Jan 8, 2013 7:20 AM
Edited by: Ghazal-OCA on Jan 8, 2013 7:23 AM -
Getting Error - Single-row subquery returns more than 1 row (Help Needed)
I have the following SQL. It selects several rows of data. This data reflects changed records. I want to update PS_UNI_TEXTBK_SKEW for the values selected. Any ideas. For updating I used:
update ps_uni_textbk_skew s
set (s.ssr_txbdtl_title, s.ssr_txbdtl_isbn, s.ubs_skew_num, s.process_date, s.ubs_rec_type) = (
and then I included theselect listed below. (this selects does work). It always produces an error saying 'singl-row subjquery returns more than 1 row'.
Any help would be appreciated. I've worked on this for a week now trying many different ways. Thanks, Mary
SELECT NOW.SSR_TXBDTL_TITLE
,NOW.SSR_TXBDTL_ISBN
,0
,SUBSTR(SYSDATE,1,8)
,'C'
FROM
SELECT C.SUBJECT||C.CATALOG_NBR||C.CLASS_SECTION||C.STRM||B.SSR_TXBDTL_SEQNO AS TYINGKEEN
,C.CRSE_ID
,C.CRSE_OFFER_NBR
,C.STRM
,C.SUBJECT
,C.CATALOG_NBR
,C.CLASS_SECTION
,C.DESCR
,B.SSR_TXBDTL_SEQNO
,B.SSR_CRSEMAT_TYPE
,B.SSR_TXBDTL_STATUS
,B.SSR_TXBDTL_TITLE
,B.SSR_TXBDTL_ISBN
,B.SSR_TXBDTL_AUTHOR
,B.SSR_TXBDTL_PUBLISH
,B.SSR_TXBDTL_EDITION
,B.SSR_TXBDTL_PUBYEAR
,B.SSR_TXBDTL_NOTES
FROM PS_CLASS_TBL C,
PS_SSR_CLS_TXB_DTL B
WHERE C.CRSE_ID = B.CRSE_ID
AND C.CRSE_OFFER_NBR = B.CRSE_OFFER_NBR
AND C.STRM = B.STRM
AND C.CLASS_SECTION = B.CLASS_SECTION
) NOW,
SELECT SUBJECT||CATALOG_NBR||CLASS_SECTION||STRM||SSR_TXBDTL_SEQNO AS TYINGKEEL
,CRSE_ID
,CRSE_OFFER_NBR
,STRM
,SUBJECT
,CATALOG_NBR
,CLASS_SECTION
,DESCR
,SSR_TXBDTL_SEQNO
,SSR_CRSEMAT_TYPE
,SSR_TXBDTL_STATUS
,SSR_TXBDTL_TITLE
,SSR_TXBDTL_ISBN
,SSR_TXBDTL_AUTHOR
,SSR_TXBDTL_PUBLISH
,SSR_TXBDTL_EDITION
,SSR_TXBDTL_PUBYEAR
,SSR_TXBDTL_NOTES
FROM PS_UNI_TEXTBK_SKEW
) LAST
WHERE NOW.TYINGKEEN = LAST.TYINGKEEL
AND (NOW.SSR_TXBDTL_TITLE <> LAST.SSR_TXBDTL_TITLE
OR NOW.SSR_TXBDTL_ISBN <> LAST.SSR_TXBDTL_ISBN
OR NOW.SSR_TXBDTL_AUTHOR <> LAST.SSR_TXBDTL_AUTHOR
OR NOW.SSR_TXBDTL_PUBLISH <> LAST.SSR_TXBDTL_PUBLISH
OR NOW.SSR_TXBDTL_EDITION <> LAST.SSR_TXBDTL_EDITION
OR NOW.SSR_TXBDTL_PUBYEAR <> LAST.SSR_TXBDTL_PUBYEAR
OR NOW.SSR_TXBDTL_NOTES <> LAST.SSR_TXBDTL_NOTES
OR NOW.SSR_TXBDTL_STATUS <> LAST.SSR_TXBDTL_STATUS
OR NOW.SSR_CRSEMAT_TYPE <> LAST.SSR_CRSEMAT_TYPE
OR NOW.DESCR <> LAST.DESCR);1. Take your subqueries
2. Run those separately to see if they really can return more than one row. If needed modify the subquery by adding GROUP-BY and HAVING-COUNT>1 clauses to determien that.
3. If you see more than one row coming up from subqueries then the error message you named will arise.
This is what i would do.
But in first look you don't have subqueries, you have "inline-views". so i don't understand how the eroor could occur.
Edited by: CharlesRoos on 22.10.2010 16:38 -
Help regarding Materialized view ( subquery expression not allowed here )
Hi all,
while creating materialized view i got following error
ORA- 22818
subquery expression not allowed here
following is my query
CREATE MATERIALIZED VIEW MV_NAV_REC
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
as
select folio_no FOLIONO,CHKDIGIT as Check_Digit,sch_code SCHEMECODE, sysdate as FOLIODATE ,
(select case when count(distinct SUBBROKERCODE) =1 then to_char(max(SUBBROKERCODE)) else 'Multiple Broker' end from transaction_st
where folio_no = tst.folio_no
group by folio_no) ARN_Number ,
(select sum(case when tran_type in ('PURCHASE','SWITCH IN') then UNITS else 0 - UNITS end ) from transaction_st
where folio_no = tst.folio_no AND SCH_CODE = tst.sch_code
group by folio_no,sch_code) NUM_UNITS_NEW ,
--SUM (case when tran_type NOT in ('REDEMPTION','SWITCH OUT') THEN UNITS ELSE 0 - UNITS END ) AS Num_Units,
(select sum(case when tran_type in ('PURCHASE','SWITCH IN') then AMOUNT else 0 - AMOUNT end ) from transaction_st
where folio_no = tst.folio_no AND SCH_CODE = tst.sch_code
group by folio_no,SCH_CODE) as NUM_AMOUNT_NEW ,
--SUM (case when tran_type NOT in ('REDEMPTION','SWITCH OUT') THEN AMOUNT ELSE 0 -AMOUNT END ) AS Scheme_Amount,
sum( CASE WHEN upper(tran_type) NOT in ('REDEMPTION','SWITCH OUT') THEN
units * (select nav_rs from nav_rec where nav_rec.sch_code = tst.sch_code and nav_rec."Date" = /*trunc(sysdate)*/to_date('23/03/2009','dd/mm/yyyy'))
ELSE (0 - units) * (select nav_rs from nav_rec where nav_rec.sch_code = tst.sch_code and nav_rec."Date" = /*trunc(sysdate)*/to_date('23/03/2009','dd/mm/yyyy')) END ) Scheme_Valuation ,
null as SCHEMEPHRASEID ,
null as "Prefered Mode of SOA",
( select sum( case when upper(tran_type) NOT in ('REDEMPTION','SWITCH OUT') THEN AMOUNT ELSE 0 - AMOUNT END ) from transaction_st t where t.folio_no = tst.folio_no group by folio_no ) as Folio_Amount,
select sum(CASE when upper(tran_type) NOT in ('REDEMPTION','SWITCH OUT') THEN
units * ( select nav_rs from nav_rec where nav_rec.sch_code = ts.sch_code and nav_rec."Date" = /*trunc(sysdate)*/to_date('23/03/2009','dd/mm/yyyy'))
ELSE
(0 - units) * ( select nav_rs from nav_rec where nav_rec.sch_code = ts.sch_code and nav_rec."Date" = /*trunc(sysdate)*/to_date('23/03/2009','dd/mm/yyyy')) END
) from transaction_st ts where ts.folio_no =tst.folio_no group by ts.folio_no
) as Folio_Valuation
from transaction_st tst
group by folio_no ,sch_code,CHKDIGIT
order by folio_no , SCH_CODE
please help meHi,
You cannot use scalar subqueries in a materialized view.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4110947624538#22981269011674
edit
From 10g doc:
ORA-22818: subquery expressions not allowed here
Cause: An attempt was made to use a subquery expression where these are not supported.
Action: Rewrite the statement without the subquery expression.
(http://download.oracle.com/docs/cd/B19306_01/server.102/b14219/e19400.htm#sthref6020)
Edited by: hoek on Jun 10, 2009 7:56 AM -
ORA-01427: single-row subquery returns more than one row HELP
I need to update baemployee.Stock_nbr field with select substr(C.CHECK_DIGIT, 3, 10)
from EMP_CHECK_DIG c where C.EMPLOYEE = e.EMPLOYEE
Please help.
update baemployee e
set Stock_nbr = (select substr(C.CHECK_DIGIT, 3, 10)
from EMP_CHECK_DIG c where C.EMPLOYEE = e.EMPLOYEE)
where exists
(select C.CHECK_DIGIT
from EMP_CHECK_DIG c where C.EMPLOYEE = e.EMPLOYEE)
and exists (select 1 from EMPLOYEE ee where ee.employee = e.employee and ee.emp_status like 'A%');
ORA-01427: single-row subquery returns more than one rowHi,
Welcome to the forum!
Whenever you have a question, please post some sample data, so that people can re-create the problem and test their solutions.
CREATE TABLE and INSERT statements, like the ones below, are great:
CREATE TABLE baemployee
( employee NUMBER (4)
, stock_nbr VARCHAR2 (10)
INSERT INTO baemployee (employee, stock_nbr) VALUES (1234, 'FUBAR');
CREATE TABLE employee
( employee NUMBER (4)
, emp_status VARCHAR2 (10)
INSERT INTO employee (employee, emp_status) VALUES (1234, 'ACTIVE');CREATE TABLE AS is good, too:
CREATE TABLE emp_check_dig
AS SELECT 1234 AS employee, 'AA1234567890ZZZ' AS check_digit FROM dual
UNION ALL SELECT 1234, 'AA2121212121ZZZ' FROM dual
;Also post the results you want from that data. In this case, the results would be the contents of the baemployee table after you run the UPDATE.
Would you want:
employee stock_nbr
1234 1234567890or would you want
employee stock_nbr
1234 2121212121If you run the UPDATE statement you posted with the data above, you'll get the "ORA-01427: single-row subquery returns more than one row" error, and you can see what causes it: there is more than one row from emp_check_dig that could be used to UPDATE the same row of baemployee. Say what you want to do in this situation (and why), and someone will help you find a way to do it. -
Subquery results need help with output
Requirements:
There is request to dump every days data from one table into text file.
Tables:INSPECTION_RESULTS
NJAS
Primary Key: RES_SYS_NO
Parent table: INSPECTION_RESULTS
Child table: NJAS
Purpose:
1. obtain output results for the MIN and MAX RES_SYS_NO from INSPECTION_RESULTS table for yesterdays date.
2. Create comma delimited file for following columns using above table data output.
Script thus far:
SELECT NJA_RES_SYS_NO||','||NJA_TEST_REC_NO||','||NJA_LIC_ST_ID||','||NJA_ETS_ID||','||NJA_SW_VER||','||NJA_TECH_ID||','||NJA_VIN||','||NJA_LIC_NO||','
||NJA_LIC_JUR||','||NJA_LIC_SRC||','||NJA_MODEL_YR||','||NJA_MAKE||','||NJA_MODEL||','||NJA_VHCL_TYPE||','||NJA_BODY_STYLE||','||NJA_CERT_CLASS||','||
NJA_GVWR||','||NJA_ASM_ETW||','||NJA_ETW_SRC||','||NJA_NO_OF_CYL||','||NJA_ENG_SIZE||','||NJA_TRANS_TYPE||','||NJA_DUAL_EXH||','||NJA_FUEL_CD||','
|| NJA_VID_SYS_NO||','|| NJA_VRT_REC_NO||','|| NJA_ARMSTDS_REC_NO||','|| NJA_RSN_F_N_TESTABLE||','|| NJA_DYNO_TESTABLE||','||
NJA_CURR_ODO_RDNG||','|| NJA_PREV_ODO_RDNG||','|| NJA_PREV_TEST_DT||','|| NJA_TEST_TYPE||','||
NJA_TEST_START_DT||','|| NJA_TEST_END_TIME||','|| NJA_EMISS_TEST_TYPE||','|| NJA_TEST_SEQ_NO||','
||NJA_LOW_MILE_EXEMP||','||NJA_TIRE_DRY||','|| NJA_REL_HUMID||','|| NJA_AMB_TEMP||','||NJA_BAR_PRESS||','|| NJA_HCF||','||
NJA_GAS_CAP_ADAP_AVL||','|| NJA_GAS_CAP_REPL||','|| NJA_OVERALL_GAS_CAP_RES
FROM NJAS
WHERE NJA_RES_SYS_NO IN (SELECT MIN(NJA_RES_SYS_NO) FROM INSPECTION_RESULTS WHERE NJA_RES_SYS_NO IN
(SELECT MAX(NJA_RES_SYS_NO) FROM INSPECTION_RESULTS, (SELECT SYSDATE-1 FROM DUAL))
It works but not sure if I am getting accurate results. Can anyone help me fine tune this subquery script? Thanks!
ScottDuplicate post
See my answer in your other posting here Re: need help with script
Maybe you are looking for
-
My iPhone 3G is no longer being "found" by iTunes - please help
I am an advanced user, so this makes it even more baffling. I am running Windows 7 Ultimate (x64) and I have the iPhone 3G (OS 4.1)running with iTunes 10.0.1 (x64). I have also now updated to the latest iTunes 10.1 but I am still having the same prob
-
Inclusion of planned cost to the material while Invoicing
Hi Everyone, We are facing a problem, kindly guide me to solve it: We are purchasing 'X' Material (split valuated) through a Purchase Order in which we did not provide any planned cost. But after GR we came to know that planned cost should also have
-
I do not have a 'Manage Pre-Order' tab on my accounts page, can anyone help?
Hi, have tried following instructions to manage pre-orders but cannot find the 'Manage Pre-orders' tab. Can anyone help please. PD
-
Time machine does not seem to be backing up pictures, music or videos on WD My Book live
We puchased a Western Digital My Book Live to use as an external hard drive. After setting it up with Time Machine and going through the first back up it doesn not look like any of our photos, vidoes or music has been backed up. Is there any way sp
-
Sudden problems with importing/editing audio files
I podcast my lectures, which run about 1 hr 15 mins. My SOP was to drag and drop an mp3 or AAC version of the original audio capture into Garageband and then split the file into two separate podcast episodes. Since the big Apple Software upgrade, how