ORA-00905 Missing Keywords View Script Error - sql

This view script is missing a right parenthesis and I cant figure out where it is. I'd appreciate any help
I've tried debugging for hours and can't figure it out. I've even tried online sql checkers I'd appreciate any help or suggestions thank you for your time. Is there an online website that can determine the syntax error faster? :
CREATE OR REPLACE VIEW MY_VIEW_SCRIPT AS
SELECT * FROM
(
SELECT
EFFECTIVE_DT,
SOURCE_CD,
VIEW_CD,
SDEX_TRAN_CODE TRANSACTION_CD,
'missing' TRANSACTION_DESC,
'missing' TRANSACTION_CATEGORY_TXT,
'Y' TRANSACTION_CODE_ACTIVE_IND,
Trunc(SYSDATE) TRANSACTION_CODE_START_DT,
NULL TRANSACTION_REVERSAL_CD,
NULL PRINCIPLE_MULTIPLICATION_NBR,
NULL INCOME_MULTIPLICATION_NBR,
NULL SHARES_MULTIPLICATION_NBR,
NULL COST_MULTIPLICATION_NBR,
'INVEST1' ACCOUNTING_SYSTEM_CD
FROM TRD_FPCMS_TRANS_FULL_01_STG
WHERE SDEX_TRAN_CODE IS NOT NULL
AND SOURCE_CD != 'INSTIO'
UNION
SELECT
EFFECTIVE_DT,
SOURCE_CD,
VIEW_CD,
SDEX_TRAN_CODE TRANSACTION_CD,
'missing' TRANSACTION_DESC,
'missing' TRANSACTION_CATEGORY_TXT,
'Y' TRANSACTION_CODE_ACTIVE_IND,
Trunc(SYSDATE) TRANSACTION_CODE_START_DT,
NULL TRANSACTION_REVERSAL_CD,
NULL PRINCIPLE_MULTIPLICATION_NBR,
NULL INCOME_MULTIPLICATION_NBR,
NULL SHARES_MULTIPLICATION_NBR,
NULL COST_MULTIPLICATION_NBR,
'INVEST1' ACCOUNTING_SYSTEM_CD
FROM TRD_FPCMS_TRANS_FULL_01_STG
WHERE SDEX_TRAN_CODE IS NOT NULL
) stg
WHERE NOT EXISTS (
SELECT 1
FROM accounting_transaction_code tgt
WHERE tgt.TRANSACTION_CD = stg.TRANSACTION_CD)
UNION
SELECT
businessdt effective_dt,
holding_source_cd source_cd,
holding_view_cd view_cd,
txn.transaction_cd,
txn.tran_desc transaction_desc,
'missing' transaction_category_txt,
'Y' transaction_code_active_ind,
TRUNC(sysdate) transaction_code_start_dt,
NULL transaction_reversal_cd,
NULL principle_multiplication_nbr,
NULL income_multiplication_nbr,
NULL shares_multiplication_nbr,
NULL cost_multiplication_nbr,
'GENEVA' ACCOUNTING_SYSTEM_CD
FROM transactions_gva_kfk_stg tks, (
SELECT DISTINCT
CASE WHEN LEAD(transaction_cd) OVER (ORDER BY transaction_cd) = transaction_cd
THEN substr(transaction_cd,1,6)||substr(sys_guid(),6,6)
ELSE transaction_cd END transaction_cd,
tran_desc
FROM (
SELECT
CASE WHEN (
SELECT COUNT(1)
FROM accounting_transaction_code
WHERE transaction_cd = UPPER(substr(trxcd,1,12))
) = 0
THEN UPPER(substr(trxcd,1,12))
WHEN (
SELECT COUNT(1)
FROM accounting_transaction_code
WHERE transaction_cd = UPPER(substr(trxcd,1,6)||substr(trxcd,-6))
) = 0
THEN UPPER(substr(trxcd,1,6)||substr(trxcd,-6))
ELSE substr(trxcd,1,6)||substr(sys_guid(),6,6)
END transaction_cd, tran_desc
FROM (
SELECT DISTINCT
gva.trxcd tran_desc,
regexp_replace(UPPER(trxcd),'[^A-Z0-9]') trxcd
FROM transactions_gva_kfk_stg gva
WHERE NOT EXISTS
(SELECT 1
FROM accounting_transaction_code tgt
WHERE tgt.transaction_desc = gva.trxcd)
) ORDER BY transaction_cd)) txn
WHERE txn.tran_desc = tks.trxcd
AND NOT EXISTS
(SELECT 1
FROM accounting_transaction_code tgt
WHERE tgt.transaction_desc = tks.trxcd);

you seem to have added those 2 columns to only one of the SELECT statements, you need to add them to all the SELECT statements that are being UNIONed together. When you union select statements, each statement needs to return the same number of columns, with the same datatypes, in the same order

Related

Get different query back based on a condition in PostgreSQL

I'm having a hard time with sql and probably this will look stupid but it shows what I am trying to achieve.
SELECT
CASE WHEN ( ( SELECT 1 FROM table_1 WHERE = condition ) IS NULL ) THEN
SELECT 'No result'::varchar
ELSE
SELECT
val_1,
val_2,
val_3
FROM
table_1
END;
A null answer is not good in my situation, I can't just use the sub-query as the main. And even if I could that would still leave the question open if the two tables were NOT the same like:
SELECT
CASE WHEN ( ( SELECT 1 FROM table_1 WHERE = condition ) IS NULL ) THEN
SELECT 'No result'::varchar
ELSE
SELECT
val_1,
val_2,
val_3
FROM
table_2 --TABLE REPLACED!
END;
As CASE-WHEN only works for one column it would be horrifying to have 20 of them with the same condition. Any help is appreciated! Thanks!
So you want to SELECT the table_log and if the result is not NULL show it to the client and if it is NULL show a message?
I created a fake table for testing. What you are looking for is the last SELECT-statement:
DROP TABLE IF EXISTS table_log;
CREATE TEMP TABLE table_log (
id INTEGER
,log_info VARCHAR)
;
INSERT INTO table_log VALUES
(1, 'test_entry')
;
ANALYZE table_log;
SELECT
COALESCE(b.log_info, 'No changes done!') AS log_info
FROM
(SELECT 'Fake-Data') a
LEFT OUTER JOIN (SELECT * FROM table_log WHERE id = 1) b ON (1=1);
If the given id = 1, you get the result, if it is something else (because it is not in the test-table) the premade message is given.
Here is a link to the db<>fiddle.

Column Name ambiguously defined in code

I getting an error in the following code stating that column is ambiguously defined but i have already changed the name of the columns within each sub query. I am not sure where the error is coming from, if i run every sub query separately it works fine but can't seem to find the specific error line.
With tskq as
(
Select SFDC_D2C_TASKS.ACCOUNTID,
Count(SFDC_D2C_TASKS.ID) Total_Tasks,
(Case when Sum(Case when SFDC_D2C_TASKS.CREATED_BY_ROLE__C like '%OE%' then 1 End)>1 then 'True' else 'False' end) OE_Task,
(Case when Sum(Case when SFDC_D2C_TASKS.CREATED_BY_ROLE__C like '%CLOS%' then 1 End)>1 then 'True' else 'False' end) CLOS_Task
From SFDC_D2C_TASKS
Group by SFDC_D2C_TASKS.Accountid
Order by SFDC_D2C_TASKS.Accountid Desc
),
discntq as
(
Select
ACCOUNT__C as discnt_ACCOUNT,
Override_PRICE__C as discnt_Override_Price,
PARENT_PRODUCT_FAMILY__C as discntP_Prod_Fam,
PRODUCT_FAMILY__C as discnt_Prod_Fam,
ORDER_PRODUCT_TYPE__C as discnt_Ord_Prod_type,
QUANTITY__C as discnt_Qty,
PRICE__C as discnt_Price
from SFDC_D2C_ORDER_PRODUCT
where PRODUCT_FAMILY__C like '%Discount%'
),
mgrq as
(
Select
SFDC_D2C_ACCOUNT_OBJECT.OSM__C,
SFDC_D2C_USER.ID as USER_ID,
SFDC_D2C_USER.Name
from SFDC_D2C_ACCOUNT_OBJECT
Left Join SFDC_D2C_USER on SFDC_D2C_ACCOUNT_OBJECT.OSM__C= SFDC_D2C_USER.ID
),
accntq as
(
Select
SFDC_D2C_ACCOUNT_OBJECT.ID as ACCOUNT_ID,
SFDC_D2C_ACCOUNT_OBJECT.CREATEDDATE as Account_Created_Date,
SFDC_D2C_ACCOUNT_OBJECT.BOOKING_DATE__C,
NVL(SFDC_D2C_ACCOUNT_OBJECT.ACCT_NUMBER__C,ACCOUNTNUMBER) as BAN,
SFDC_D2C_ACCOUNT_OBJECT.OWNERID,
SFDC_D2C_ACCOUNT_OBJECT.WEB_ID__C,
SFDC_D2C_ACCOUNT_OBJECT.ICI_CODE__C as Store_Code,
SFDC_D2C_ACCOUNT_OBJECT.COID__C,
SFDC_D2C_ACCOUNT_OBJECT.NAME as Customer_Name,
SFDC_D2C_ACCOUNT_OBJECT.OSM__C as OSM_ID,
SFDC_D2C_ACCOUNT_OBJECT.PROJECT_NAME__PC,
SFDC_D2C_ACCOUNT_OBJECT.TELUS_MANAGER__PC,
SFDC_D2C_ACCOUNT_OBJECT.D2C_ROLE__PC,
SFDC_D2C_ACCOUNT_OBJECT.SALESFORCE_USER__PC,
SFDC_D2C_ACCOUNT_OBJECT.OWNERID as Rep_ID,
(Case
when SFDC_D2C_ACCOUNT_OBJECT.SALES_ACTIVITY__C in ('RMP', 'EDGE Collateral' , 'TAP' , 'MDU Lobby Event' , ' CityPlace') then 'MDU'
else (
case when SFDC_D2C_ACCOUNT_OBJECT.SALES_ACTIVITY__C in('Door Knocking' , 'Referral' , 'Mall Event' , 'Back to School' , 'Falcon') then ' D2C' end)
end) as D2C_Channel,
SFDC_D2C_ACCOUNT_OBJECT.RTCA_COMPLETE__C,
SFDC_D2C_ACCOUNT_OBJECT.SALES_ACTIVITY__C,
SFDC_D2C_ACCOUNT_OBJECT.ACTIVITY_DETAIL__C,
SFDC_D2C_ACCOUNT_OBJECT.SPECIALEVENT__C,
SFDC_D2C_ACCOUNT_OBJECT.GIFT_PRODUCT_NAME__C,
SFDC_D2C_ACCOUNT_OBJECT.ORDER_STATUS__C,
SFDC_D2C_ACCOUNT_OBJECT.BOOKED__C,
SFDC_D2C_ACCOUNT_OBJECT.CANCEL_ALL_PRODUCTS__C,
SFDC_D2C_ACCOUNT_OBJECT.LIVE_ORDER_ENTRY_STATUS__C,
SFDC_D2C_ACCOUNT_OBJECT.TABLET_SUBMITTED_ORDER__C,
(Case when SFDC_D2C_ACCOUNT_OBJECT.BOOKING_DATE__C is not null then 'True' else 'False' end) as Softbooked
from SFDC_D2C_ACCOUNT_OBJECT
),
ordprdq as
(
Select
SFDC_D2C_ORDER_PRODUCT.ACCOUNT__C as ordpr_ACCOUNT,
SFDC_D2C_ORDER_PRODUCT.CREATEDDATE as Order_Product_Created_Date,
SFDC_D2C_ORDER_PRODUCT.BOOKED_DATE__C,
SFDC_D2C_ORDER_PRODUCT.PRODUCT__C,
SFDC_D2C_ORDER_PRODUCT.ORDER_PRODUCT_TYPE__C,
SFDC_D2C_ORDER_PRODUCT.PARENT_PRODUCT_FAMILY__C,
SFDC_D2C_ORDER_PRODUCT.PRODUCT_FAMILY__C,
SFDC_D2C_ORDER_PRODUCT.PRODUCT__C,
SFDC_D2C_ORDER_PRODUCT.QUANTITY__C,
SFDC_D2C_ORDER_PRODUCT.STATUS__C,
SFDC_D2C_ORDER_PRODUCT.STATUS_REASON__C,
SFDC_D2C_ORDER_PRODUCT.MIGRATION__C
From SFDC_D2C_ORDER_PRODUCT
),
prdctq as
(
Select
SFDC_D2C_PRODUCTS.ID as Product_ID,
SFDC_D2C_PRODUCTS.ACTIVE__C,
SFDC_D2C_PRODUCTS.PRICE__C
From SFDC_D2C_PRODUCTS
)
Select
accntq.ACCOUNT_ID
From accntq
Left Join ordprdq on accntq.ACCOUNT_ID =ordprdq.ordpr_ACCOUNT
Left Join ordprdq on prdctq.Product_ID=ordprdq.PRODUCT__C
Left Join mgrq on mgrq.ACCOUNT_ID=accntq.OSM__C
Left Join tskq on taskq.ACCOUNT__C= accntq.ACCOUNT_ID
Your ordprdq CTE is including SFDC_D2C_ORDER_PRODUCT.PRODUCT__C twice in its select list.

Subquery within SubQuery in SQL - DB2

I am having issue when trying to make a the sub query shown in the first filter dynamically based on one of the results returned from the query. Can someone please tell me what I am doing wrong. In the first subquery it worked.
( SELECT
MAX( MAX_DATE - MIN_DATE ) AS NUM_CONS_DAYS
FROM
(
SELECT
MIN(TMP.D_DAT_INDEX_DATE) AS MIN_DATE,
MAX(TMP.D_DAT_INDEX_DATE) AS MAX_DATE,
SUM(INDEX_COUNT) AS SUM_INDEX
FROM
(
SELECT
D_DAT_INDEX_DATE,
INDEX_COUNT,
D_DAT_INDEX_DATE - (DENSE_RANK() OVER(ORDER BY D_DAT_INDEX_DATE)) DAYS AS G
FROM
DWH.MQT_SUMMARY_WATER_READINGS
WHERE
N_COD_METER_CNTX_KEY = 79094
) AS TMP
GROUP BY
TMP.G
ORDER BY
1
) ) AS MAX_NUM_CONS_DAYS
Above is the subquery I am trying to replace 123456 with CTXTKEY or CTXT.N_COD_METER_CNTX_KEY from query. Below is the full code. Please note than in the subquery before "MAX_NUM_CONS_DAYS" it worked. However, it was only one subquery down.
SELECT
N_COD_WM_DWH_KEY,
V_COD_WM_SN_2,
N_COD_SP_ID,
CTXKEY,
V_COD_MIU_SN,
N_COD_POD,
MIU_CAT,
V_COD_SITR_ASSOCIATED,
WO_INST_DATE,
WO_MIU_CAT,
DAYSRECEIVED3,
MAX_NUM_CONS_DAYS,
( CASE WHEN ( DAYSRECEIVED3 = 3 ) THEN 'Y' ELSE 'N' END ) AS GREEN,
( CASE WHEN ( DAYSRECEIVED3 < 3 AND DAYSRECEIVED3 > 0 ) THEN 'Y' ELSE 'N' END ) AS BLUE,
( CASE WHEN ( DAYSRECEIVED3 = 0 AND MAX_NUM_CONS_DAYS >= 5 ) THEN 'Y' ELSE 'N' END ) AS ORANGE,
( CASE WHEN ( DAYSRECEIVED3 = 0 AND MAX_NUM_CONS_DAYS BETWEEN 1 and 4 ) THEN 'Y' ELSE 'N' END ) AS RED
FROM
(
SELECT
WMETER.N_COD_WM_DWH_KEY,
WMETER.V_COD_WM_SN_2,
WMETER.N_COD_SP_ID,
CTXT.N_COD_METER_CNTX_KEY AS CTXKEY,
CTXT.V_COD_MIU_SN,
CTXT.N_COD_POD,
MIU.N_COD_MIU_CATEGORY AS MIU_CAT,
CTXT.V_COD_SITR_ASSOCIATED,
T1.D_DAT_PLAN_INST AS WO_INST_DATE,
T1.N_COD_MIU_CATEGORY AS WO_MIU_CAT,
( SELECT COUNT( DISTINCT D_DAT_INDEX_DATE ) FROM DWH.MQT_SUMMARY_WATER_READINGS WHERE ( N_COD_METER_CNTX_KEY = CTXT.N_COD_METER_CNTX_KEY ) AND D_DAT_INDEX_DATE BETWEEN ( '2013-07-10' ) AND ( '2013-07-12' ) ) AS DAYSRECEIVED3,
( SELECT
MAX( MAX_DATE - MIN_DATE ) AS NUM_CONS_DAYS
FROM
(
SELECT
MIN(TMP.D_DAT_INDEX_DATE) AS MIN_DATE,
MAX(TMP.D_DAT_INDEX_DATE) AS MAX_DATE,
SUM(INDEX_COUNT) AS SUM_INDEX
FROM
(
SELECT
D_DAT_INDEX_DATE,
INDEX_COUNT,
D_DAT_INDEX_DATE - (DENSE_RANK() OVER(ORDER BY D_DAT_INDEX_DATE)) DAYS AS G
FROM
DWH.MQT_SUMMARY_WATER_READINGS
WHERE
N_COD_METER_CNTX_KEY = 79094
) AS TMP
GROUP BY
TMP.G
ORDER BY
1
) ) AS MAX_NUM_CONS_DAYS
FROM DWH.DWH_WATER_METER AS WMETER
LEFT JOIN DWH.DWH_WMETER_CONTEXT AS CTXT
ON WMETER.N_COD_WM_DWH_KEY = CTXT.N_COD_WM_DWH_KEY
LEFT JOIN DWH.DWH_MIU AS MIU
ON CTXT.V_COD_MIU_SN = MIU.V_COD_MIU_SN
LEFT JOIN
( SELECT V_COD_CORR_WAT_METER_SN, D_DAT_PLAN_INST, N_COD_MIU_CATEGORY
FROM DWH.DWH_ORDER_MANAGEMENT_FACT
JOIN DWH.DWH_MIU
ON DWH.DWH_ORDER_MANAGEMENT_FACT.V_COD_MIU_SN = DWH.DWH_MIU.V_COD_MIU_SN
) AS T1
ON WMETER.V_COD_WM_SN_2 = T1.V_COD_CORR_WAT_METER_SN
WHERE
( V_COD_SITR_ASSOCIATED = 'X' )
AND ( ( MIU.N_COD_MIU_CATEGORY <> 4 ) OR ( ( MIU.N_COD_MIU_CATEGORY IS NULL ) AND ( ( T1.N_COD_MIU_CATEGORY <> 4 ) OR ( T1.N_COD_MIU_CATEGORY IS NULL ) ) ) )
)
Error I am getting is:
Error Code: -204, SQL State: 42704
I would say that a good option here would be to use a CTE, or Common Table Expression. You can do something similar to the following:
WITH CTE_X AS(
SELECT VAL_A
,VAL_B
FROM TABLE_A)
,CTE_Y AS(
SELECT VAL_C
,VAL_B
FROM TABLE_B)
SELECT VAL_A
,VAL_B
FROM CTE_X X
JOIN CTE_Y Y
ON X.VAL_A = Y.VAL_C;
While this isn't specific to your example, it does show that CTE's create a sort of temporary "in memory" table that you can access in a subsequent query. This should allow you to issue your inner two subselects as a CTE, and then use the CTE in the "SELECT MAX( MAX_DATE - MIN_DATE ) AS NUM_CONS_DAYS" query.
You cannot reference columns from the outer select in the subselect, no more than 1 level deep anyway. If I correctly understand what you're doing, you'll probably need to join DWH.MQT_SUMMARY_WATER_READINGS and DWH.DWH_WMETER_CONTEXT in the outer select.

How to check existence of data in a table from a where clause in sql server 2008?

Suppose I have a table with columns user_id, name and the table contains data like this:
user_id name
------- -----
sou souhardya
cha chanchal
swa swapan
ari arindam
ran ranadeep
If I want to know these users (sou, cha, ana, agn, swa) exists in this table or not then I want output like this:
user_id it exists or not
------- -----------------
sou y
cha y
ana n
agn n
swa y
As ana and aga do not exist in the table it must show "n" (like the above output).
Assuming your existing checklist is not on the database, you will have to assemble a query containing those. There are many ways of doing it. Using CTEs, it would look like this:
with cte as
(
select 'sou' user_id
union all
select 'cha'
union all
select 'ana'
union all
select 'agn'
union all
select 'swa'
)
select
cte.user_id,
case when yt.user_id is null then 'n' else 'y' end
from cte
left join YourTable yt on cte.user_id = yt.user_id
This also assumes user_id is unique.
Here is the SQLFiddle with the proof of concept: http://sqlfiddle.com/#!3/e023a0/4
Assuming you're just testing this manually:
DECLARE #Users TABLE
(
[user_id] VARCHAR(50)
)
INSERT INTO #Users
SELECT 'sou'
UNION SELECT 'cha'
UNION SELECT 'ana'
UNION SELECT 'agn'
UNION SELECT 'swa'
SELECT a.[user_id]
, [name]
, CASE
WHEN b.[user_id] IS NULL THEN 'N'
ELSE 'Y'
END AS [exists_or_not]
FROM [your_table] a
LEFT JOIN #Users b
ON a.[user_id] = b.[user_id]
You didn't provide quite enough information to provide a working example, but this should get you close:
select tbl1.user_id, case tbl2.user_id is null then 'n' else 'y' end
from tbl1 left outer join tbl2 on tbl1.user_id = tbl2.user_id
;with usersToCheck as
(
select 'sou' as userid
union select 'cha'
union select 'ana'
union select 'agn'
union select 'swa'
)
select utc.userid,
(case when exists ( select * from usersTable as ut where ut.user_id = utc.userid) then 'y' else 'n' end)
from usersToCheck as utc

Erroneous ORA-01427: single-row subquery returns more than one row

I'm getting the error [ORA-01427: single-row subquery returns more than one row] when I execute a query. I have a query structured like so:
SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
It's breaking on the nested select. I know the logic both in the view and in this query is correct, and that there's no chance of the nested select returning more than one row. The CARS table's MODEL_ID is a unique field. If I execute the query without the nested select it doesn't return this error.
The LEDGER_VIEW is a view built on top of another view. Is it possible that these stacked views are buggy in Oracle 10g? I don't know how else to debug this problem.
I am aware I could change this particular query to a join rather than a nested select, but I'd like to know why this is happening because I use nested queries in other places where it is not so easily modifiable.
EDIT: Here's the really strange thing. The LEDGER_VIEW is, as I said, built on top of another view. As a test, I copied the nested view's SQL directly into the SQL of the SQL of LEDGER_VIEW, in place of the nested view, and it returned with no errors (as expected). This seems to confirm to me that there is some buggy behavior either with nested views or with the combination of nested views + database links.
Your subquery is returning multiple rows. Use the query below to find out which MODELID values in the Car table are duplicated:
select MODELID as CarsModelID, count(*) as Count
from cars
where MODELID in (
select MODEL_ID
from LEDGER_VIEW
WHERE LV.PRICE < 500
)
group by MODELID
having count(*) > 1
I am unable to recreate via a creation of a stacked view. (althoug RedFilters will find the culprit)
CREATE TABLE t1
(
t1_id NUMBER ,
txt VARCHAR2( 50 ),
CONSTRAINT t1_pk PRIMARY KEY( t1_id )
) ;
CREATE TABLE t2
(
t2_id NUMBER ,
t1_id NUMBER ,
price NUMBER( 10, 4 ) ,
CONSTRAINT t2_pk PRIMARY KEY( t2_id ),
CONSTRAINT t2_fk FOREIGN KEY( t1_id ) REFERENCES t1( t1_id )
);
insert into t1(t1_id, txt) values(1,'fit');
insert into t1(t1_id, txt) values(2,'focus');
insert into t1(t1_id, txt) values(3,'golf');
insert into t1(t1_id, txt) values(4,'explorer');
insert into t1(t1_id, txt) values(5,'corolla');
insert into t2(t2_id, t1_id, price) values(1,1,17000);
insert into t2(t2_id, t1_id, price) values(2,2,16000);
insert into t2(t2_id, t1_id, price) values(3,3,22000);
insert into t2(t2_id, t1_id, price) values(4,4,31000);
insert into t2(t2_id, t1_id, price) values(5,5,17000);
create view t1_view as select * from t1;
create view t2_view as select * from t2;
create view t_stacked_view as
select t1_view.txt ,
t2_view.price ,
t1_view.t1_id
from t1_view
left join
t2_view
on t1_view.t1_id = t2_view .t1_id
;
--stacked view test
select t1_view.txt ,
(select t_stacked_view.price
from t_stacked_view
where t1_view.t1_id = t_stacked_view .t1_id) price
from t1_view ;
--or better yet, just drop the row level query
select t1_view.txt ,
t2_view.price
from t1_view
left join
t2_view
on t1_view.t1_id = t2_view .t1_id
;
But that begs the question, why are you doing the row level query here? While 10g ought to optimize them the same, I have always found it easier to write queries as below, both for readability, maintainability, and to specifically avoid the error you are having (is it always, 3 years down the road, guaranteed by the application (both in the db and the calling app) that you cannot have a condition that will cause this error? One rouge statement gets in and your entire app dies?
SELECT LV.PRICE,
c.model_name
FROM LEDGER_VIEW LV
LEFT /* OR INNER */ JOIN CARS C
ON C.MODEL_ID = LV.MODEL_ID
WHERE LV.PRICE < 500
I suggest using RedFilter's answer to check whether there are multiple cars with a given MODEL_ID.
If you're absolutely certain that CARS.MODEL_ID is unique, then it implies that the error message is generated by selection from LEDGER_VIEW - so try running the equivalent query without the subquery on CARS, like so:
SELECT LV.PRICE
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
If you still see the same error (you should, if CARS.MODEL_ID is unique) you will need to debug LEDGER_VIEW - ie. check for sub-queries returning multiple rows in LEDGER_VIEW and the underlying views it is based on.
Creating views based on views is possible in most forms of SQL, but it is usually a bad idea - for this very reason.
Try forcing your subquery to return a single result by appending rownum = 1, like this:
SELECT LV.PRICE,
(SELECT C.MODEL_NAME FROM CARS C WHERE C.MODEL_ID = LV.MODEL_ID AND ROWNUM = 1) as MODEL_NAME
FROM LEDGER_VIEW LV
WHERE LV.PRICE < 500
It will probably work and if it does, you will know that your subquery returns multiple rows, which judging by the error code it should be. Of course this is not a solution so you might have to fix your data in cars table to actually solve the problem. Leaving and rownum = 1 will eliminate the error if model_id is duplicated again, preventing you from noticing the problem.
select
a.account_number,
a.party_id,
a.TRX_NUMBER,
a.trx_date,
a.order_number,
adv.unapplied_amt,
a.Finance,
a.customer_name,a.PARTY_NAME,
a.customer_number,a.contact_number,
a.name,
a.Aging,
a.transaction_type,
a.exec_name,
a.team_leader,
sum(a.O_SAmount),
(case when (trunc(sysdate) - trunc(a.trx_date)) <=:ag1 then sum(a.O_SAmount) else 0 end ) bucket1,--"<" || :ag1,
(case when (trunc(sysdate) - trunc(a.trx_date)) between :ag1+1 and :ag2 then sum(a.O_SAmount) else 0 end ) bucket2,--:ag1+1 || "to" || :ag2,
(case when (trunc(sysdate) - trunc(a.trx_date)) between :ag2+1 and :ag3 then sum(a.O_SAmount) else 0 end ) bucket3,--:ag2+1 || "to" || :ag3,
(case when (trunc(sysdate) - trunc(a.trx_date)) >:ag3 then sum(a.O_SAmount) else 0 end ) bucket4,
:AS_ON_date
from
(select distinct hca.account_number,hp.party_id,--rcta.CUSTOMER_TRX_ID,
--rcta.trx_number,rcta.trx_date,apsa.due_date,
(
select distinct
--ooha.order_number,
rcta.trx_number
--to_char(rcta.trx_date,'DD-MON-YYYY') trx_date
from
ra_customer_trx_all rcta,
oe_order_headers_all ooh,
oe_order_lines_all oola,
--ra_customer_trx_all rcta,
ra_customer_trx_lines_all rctla,
ra_cust_trx_types_all rctta
--ra_customer_trx_lines_all rctl
where 1=1
AND ooh.header_id = oola.header_id
--AND ooh.order_number = '111111010101698'
AND ooh.order_number=oohA.order_number
AND TO_CHAR (ooh.order_number) = rcta.ct_reference
AND rcta.customer_trx_id = rctla.customer_trx_id
AND rctla.inventory_item_id = oola.inventory_item_id
and rcta.CUST_TRX_TYPE_ID = rctta.cust_trx_type_id
and rcta.org_id = rctta.org_id
and rctta.type like 'INV'
and oola.ordered_item LIKE 'MV%'
AND oola.attribute3 = 'Y'
AND ooh.flow_status_code <> 'ENTERED'
AND oola.flow_status_code <> 'CANCELLED'
)TRX_NUMBER,
(select distinct
--ooha.order_number,
--rcta.trx_number
rcta.trx_date
from
ra_customer_trx_all rcta,
oe_order_headers_all ooh,
oe_order_lines_all oola,
--ra_customer_trx_all rcta,
ra_customer_trx_lines_all rctla,
ra_cust_trx_types_all rctta
--ra_customer_trx_lines_all rctl
where 1=1
AND ooh.header_id = oola.header_id
--AND ooh.order_number = '111111010101698'
AND ooh.order_number=oohA.order_number
AND TO_CHAR (ooh.order_number) = rcta.ct_reference
AND rcta.customer_trx_id = rctla.customer_trx_id
AND rctla.inventory_item_id = oola.inventory_item_id
and rcta.CUST_TRX_TYPE_ID = rctta.cust_trx_type_id
and rcta.org_id = rctta.org_id
and rctta.type like 'INV'
and oola.ordered_item LIKE 'MV%'
AND oola.attribute3 = 'Y'
AND ooh.flow_status_code <> 'ENTERED'
AND oola.flow_status_code <> 'CANCELLED'
)TRX_Date,
rcta.INTERFACE_HEADER_ATTRIBUTE1 order_number,
ooha.attribute10 Finance,
f.customer_name,HP.PARTY_NAME,
TO_NUMBER(f.customer_number)customer_number,hp.primary_phone_number contact_number,--csi.incident_number,
--cii.instance_number,
haou.name,
--sum(acr.amount) Advance,--rcta.CUST_TRX_TYPE_ID,--acr.cash_receipt_id,
--sum(abs((apsa.AMOUNT_DUE_REMAINING-nvl(acr.amount,0)))) "O_SAmount",
apsa.AMOUNT_DUE_REMAINING O_SAmount,
--sum(abs((apsa.AMOUNT_DUE_REMAINING))) "O_SAmount",
round(months_between(sysdate,rcta.trx_date)*30) Aging,
--(case when ((round(months_between(sysdate,rcta.trx_date)*30)>=0) or (round(months_between(sysdate,rcta.trx_date)*30)<:aging1)) then apsa.AMOUNT_DUE_REMAINING end) "0 TO 30"
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) <=:ag1 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket1,--"<" || :ag1,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) between :ag1+1 and :ag2 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket2,--:ag1+1 || "to" || :ag2,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) between :ag2+1 and :ag3 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket3,--:ag2+1 || "to" || :ag3,
--(case when (trunc(sysdate) - trunc(apsa.Due_Date)) >:ag3 then apsa.AMOUNT_DUE_REMAINING else 0 end ) bucket4,
--apsa.amount_due_original,
--TO_NUMBER(apsa.AMOUNT_DUE_REMAINING)AMOUNT_DUE_REMAINING,
rctta.name transaction_type,
PAPF.full_name||'-'||PAPF.EMPLOYEE_NUMBER exec_name,
ooha.attribute9 team_leader,
:AS_ON_date
from ra_customer_trx_all rcta,
oe_order_headers_all ooha,
hz_cust_accounts hca,
hz_parties hp,
--cs_incidents_all_b csi,
--csi_item_instances cii,
hr_all_organization_units haou,
ar_cash_receipts_all acr,
ar_receivable_applications_all aaa,
ra_cust_trx_types_all RCTTA,
hr.per_all_people_f papf,
ar_customers f,
ar_payment_schedules_all apsa,
jtf.JTF_RS_SALESREPS jrs
where 1=1
--and INTERFACE_HEADER_ATTRIBUTE1 like '111111060100538'
--and INTERFACE_HEADER_ATTRIBUTE1 like '111111010105402'
--and INTERFACE_HEADER_ATTRIBUTE1 like '111111010102791'
and rcta.ct_reference(+)=TO_CHAR(ooha.order_number)
AND f.customer_id = (rcta.bill_to_customer_id)
and f.customer_id=hca.cust_account_id
and hca.party_id=hp.party_id
and haou.organization_id=rcta.INTERFACE_HEADER_ATTRIBUTE10
--and hp.party_id=cii.owner_party_id
--and csi.inventory_item_id=cii.inventory_item_id
--and csi.inv_organization_id=haou.organization_id
--and haou.organization_id=nvl(:location,haou.organization_id)
and ooha.SHIP_FROM_ORG_ID=nvl(:location,haou.organization_id)
AND RCTTA.NAME like :transaction_type||'%'
--decode(:org_id,null,null,(select name from ar_cash_receipts_all where organization_id = :org_id)) ||'%')
and rcta.trx_date<=to_date(:AS_ON_date)
--AND RCTTA.NAME=NVL(:TRANS_TYPE,RCTTA.NAME)
and rcta.org_id=nvl(:org_id,rcta.org_id)
--and f.customer_name like 'VIKAS SATAV'
and aaa.applied_customer_trx_id(+)=rcta.customer_trx_id
and aaa.cash_receipt_id=acr.cash_receipt_id(+)
and rcta.status_trx like 'OP'
and rcta.CUST_TRX_TYPE_ID=rctta.CUST_TRX_TYPE_ID
and apsa.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID
and TO_NUMBER(apsa.AMOUNT_DUE_REMAINING) >0
--and hp.party_id=papf.party_id(+)
and jrs.salesrep_id = ooha.SALESREP_ID
and jrs.ORG_ID = ooha.ORG_ID
and jrs.PERSON_ID = papf.PERSON_ID(+)
) a,
(
select
b.order_number,
sum(b.AMOUNT_APPLIED) unapplied_amt
from
(select distinct to_char(ooha.order_number) order_number,ara.* from
oe_order_headers_all ooha,
oe_payments oe,
ar_receivable_applications_all ara
where 1=1--ooha.order_number = :p_order_num
and oe.header_id=ooha.header_id
and ara.PAYMENT_SET_ID=oe.PAYMENT_SET_ID
and ara.DISPLAY='Y'
and (ara.STATUS like 'OTHER ACC' or ara.STATUS like 'UNAPP') --or ara.STATUS like 'ACC')
) b
group by b.order_number
) adv
where adv.order_number(+)=a.order_number
group by a.account_number,
a.party_id,
a.TRX_NUMBER,
a.trx_date,
a.order_number,
adv.unapplied_amt,
a.Finance,
a.customer_name,a.PARTY_NAME,
a.customer_number,a.contact_number,
a.name,
a.Aging,
a.transaction_type,
a.exec_name,
a.team_leader
order by a.Aging desc