SELECT in SELECT returning multiple columns ?
10.2.0.3 version.
I am trying to tune this query
SELECT A.EMPLID, A.EFFDT, A.ACAD_PROG,
(SELECT A1.DESCR FROM PS_ACAD_PROG_TBL A1
WHERE A1.INSTITUTION = 'AUAO1' AND A1.ACAD_PROG = A.ACAD_PROG AND A1.EFFDT = (SELECT MAX (EFFDT) FROM PS_ACAD_PROG_TBL WHERE INSTITUTION = A1.INSTITUTION AND ACAD_PROG = A1.ACAD_PROG),
(SELECT A2.DESCRSHORT FROM PS_ACAD_PROG_TBL A2
WHERE A2.INSTITUTION = 'AUAO1' AND A2.ACAD_PROG = A.ACAD_PROG AND A2.EFFDT = (SELECT MAX (EFFDT) FROM PS_ACAD_PROG_TBL WHERE INSTITUTION = A2.INSTITUTION AND ACAD_PROG = A2.ACAD_PROG),
as you can see it reading the table PS_ACAD_PROG_TBL two time to get two cloumns DESCR (alias A1) and DESCRSHORT (alias A2) exactly with the same where condition.
I am trying to replace it with just one select returning multiple columns like this
SELECT A.EMPLID, A.EFFDT, A.ACAD_PROG,
(SELECT A1.DESCR, A1.DESCRSHORT FROM PS_ACAD_PROG_TBL A1
WHERE A1.INSTITUTION = 'AUAO1' AND A1.ACAD_PROG = A.ACAD_PROG AND A1.EFFDT = (SELECT MAX (EFFDT) FROM PS_ACAD_PROG_TBL WHERE INSTITUTION = A1.INSTITUTION AND ACAD_PROG = A1.ACAD_PROG),
but I am getting ORA-00913: too many values.
Please help on how to get around this. Thanks in advance.
William,
On similar lines I am struggling with the below query based on your suggestions.
SQL*Plus: Release 9.2.0.1.0 - Production on Wed Aug 5 14:17:21 2009
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> WITH arch_dup AS
2 (SELECT a.file_id,
3 a.sale_start,
4 a.activity_start,
5 a.activity_end,
6 a.item_code_cust,
7 a.division,
8 (SELECT MAX (DISTINCT (NVL (b.advertised, 'No')))
9 FROM ah_cust_gis_arch_stg b
10 WHERE b.file_id = a.file_id
11 AND NVL (b.sale_start, 'NULL') = NVL (a.sale_start, 'NULL')
12 AND NVL (b.activity_start, 'NULL') = NVL (a.activity_start, 'NULL')
13 AND NVL (b.activity_end, 'NULL') = NVL (a.activity_end, 'NULL')
14 AND b.item_code_cust = a.item_code_cust
15 AND b.division = a.division) advertised,
16 COUNT (*)
17 FROM ah_cust_gis_arch_stg a
18 WHERE a.file_id = 209
19 AND a.status_id = 21
20 GROUP BY a.file_id, a.sale_start, a.activity_start, a.activity_end, a.item_code_cust, a.d
ivision
21 HAVING COUNT (*) > 1)
22 SELECT dup.*,
23 (SELECT activity_id
24 FROM (SELECT activity_id,
25 ROW_NUMBER () OVER (ORDER BY (activity_retail / activity_mult)) rnk
26 FROM ah_cust_gis_arch_stg
27 WHERE status_id = 21
28 AND NVL (sale_start, 'NULL') = NVL (dup.sale_start, 'NULL')
29 AND NVL (activity_start, 'NULL') = NVL (dup.activity_start, 'NULL')
30 AND NVL (activity_end, 'NULL') = NVL (dup.activity_end, 'NULL')
31 AND item_code_cust = dup.item_code_cust
32 AND division = dup.division
33 AND file_id = 209
34 AND UPPER (NVL (advertised, 'N')) = dup.advertised)
35 WHERE rnk = 1) activity_id
36 FROM arch_dup dup;
AND UPPER (NVL (advertised, 'N')) = dup.advertised)
ERROR at line 34:
ORA-00904: "DUP"."ADVERTISED": invalid identifierCan you throw some light on what to do to make the outer query columns available to inner query?
Thanks,
Raj.
Similar Messages
-
Select function that returns multiple columns.
Currently I have a function that will return 1 column and I can use that in a select statement. What I need is to return multiple columns from a table. In the function I have I return the street address from a table that holds information about dealers, I need to be able to also return the columns that contain the city, state, and zip code so it can be used in an sql select statement. How would I do this?
My function:
FUNCTION GET_ADDRESS(dealer_id IN number)
RETURN tbl_dealer_info.c_street%TYPE AS
v_rc tbl_dealer_info.c_street%TYPE;
CURSOR get_dealer_cur IS
SELECT c_street
FROM tbl_dealer_info
WHERE n_dealer_id = dealer_id;
BEGIN
v_rc := NULL;
OPEN get_dealer_cur;
FETCH get_dealer_cur INTO v_rc;
IF get_dealer_cur%NOTFOUND THEN
NULL;
END IF;
CLOSE get_dealer_cur;
RETURN v_rc;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END GET_ADDRESS;
My select statement:
select GET_ADDRESS(1205) Street, DI.n_inactive_flag, DI.n_onhold_flag
from tbl_dealer_info DI
where DI.n_dealer_id = 1205;
I would like to be able to select the street, city, state, and zip all in this select statement from the GET_ADDRESS function.
Thanks,
Lori Neirynck"The reality is you've probably already put your blinders on and you won't learn the best approach to solving your problem because you insist on asking a courtroom style question (just "yes" or "no" please)."
Actually, I asked this question because I was looking for the best approach to my problem. I have an SQL statement that correctly selects everything it needs to from all 15 tables. The thing of it is it is very long and difficult to read. I wanted to try using functions so that I could change 12 AND/OR statements into 3 IF statements so everything is easier to read. I have received a couple of different ways to do this from other forums that assumed I knew what I was doing. I have gotten one to work, the other I'm still working on. I'll post the one that worked for others that want to know.
SQL> insert into dealer_info values (1,'Me','13 success street', 'Wonder Town','Wonderland','1313');
SQL> commit;
SQL> create type t_address as object (
2 street varchar2(100),
3 city varchar2(30),
4 state varchar2(30),
5 zip varchar2(10));
6 /
Type created.
SQL> create or replace function get_address (p_id number) return t_address
2 is
3 ret t_address := t_address(null,null,null,null);
4 rec dealer_info%rowtype;
5 begin
6 select * into rec from dealer_info where id=p_id;
7 ret.street := rec.street;
8 ret.city := rec.city;
9 ret.state := rec.state;
10 ret.zip := rec.zip;
11 return ret;
12 end;
13 /
Function created.
SQL> col name format a10
SQL> col address format a70
SQL> select name, get_address(id) address from dealer_info;
NAME ADDRESS(STREET, CITY, STATE, ZIP)
Me T_ADDRESS('13 success street', 'Wonder Town', 'Wonderland', '1313')
1 row selected.
-Lori -
Stored procedure in package return multiple columns from multiple tables
Hi ,
Can a single stored procedure return multiple column values from different tables.
example:
tabA: col2, tabB:col3,tabC:col4 etc.
one more question:
if a stored procedure like to return 10 columns for a particular record from a single table do i need to define a TYPE statement for each colum like
TYPE col1 is TABLE of varchar
TYPE col2 is TABLE of varchar
here i want to return only one row, not many rows.
thanksYou can try one procedure with OUT or IN/OUT parameters that collect the values from one or more sql statements.
CREATE OR REPLACE PROCEDURE P1
(P_COD IN TABLE.COD%TYPE,
P_DESC1 OUT TABLE1.DESC1%TYPE,
P_DESC2 OUT TABLE2.DESC2%TYPE)
IS
BEGIN
SELECT table1.DESC1, table2.DESC2
INTO P_DESC1, P_DESC2
FROM TABLE1, table2 WHERE
table1.COD = P_COD and
table1.cod = table2.cod ;
END P1;
JP -
Selectively Format Groups with Multiple Columns
I have a bit of an obscure task I've been trying to hammer out to no avail. I've searched quite a bit but can't seem to find anyone else who has attempted this. I have two group by statements, one is a category and the other is a rank. What I'm trying to do is organize the report so that there are category rows and below each category row is a set of columns for each rank with their associated values as such:
category1
rank1 rank2 rank3 rank4
value1 value1 value1 value1
value2 value2 value2 value2
category2
rank1 rank2 rank3
value1 value1 value1
value2 value2 value2
I've tried using details>Section Expert>>Layout>Format Groups with multiple column. Unfortunately, this applies to both groups putting the categories into columns instead of rows, making a mess. Does anyone know of a way to selectively put groups into columns and ensure alignment? In the above example the user would be able to compare rank values from multiple categories since they all line up. Any help with this would be very much appreciated.Hi,
Have you tried using a crosstab? Give this a shot:
1) Group the report on Category
2) Create a new group header section. Group Header b
3) Place a crosstab in this section
4) The columns would be the Rank field, I'm not sure about what you would like to show in the rows
5) The summarized field would be the Value field ofcourse
Let me know how this goes.
-Abhilash -
Return multiple columns from an analytic function with a window
Hello,
Is it possible to obtain multiple columns from an analytic function with a window?
I have a table with 4 columns, an id, a test id, a date, and the result of a test. I'm using an analytic function to obtain, for each row, the current test value, and the maximum test value in the next 2 days like so:
select
id,
test_id,
date,
result,
MAX ( result ) over ( partition BY id, test_id order by date RANGE BETWEEN CURRENT ROW AND INTERVAL '2' DAY FOLLOWING ) AS max_result_next_two_day
from table
This is working fine, but I would like to also obtain the date when the max result occurs. I can see that this would be possible using a self join, but I'd like to know if there is a better way? I cannot use the FIRST_VALUE aggregate function and order by result, because the window function needs to be ordered by the date.
It would be a great help if you could provide any pointers/suggestions.
Thanks,
Dan
http://danieljamesscott.orgAssuming RESULT is a positive integer that has a maximum width of, say 10,
and assuming date has no time-component:
select
id
,test_id
,date
,result
,to_number(substr(max_result_with_date,1,10)) as max_result_next_two_day
,to_date(substr(max_result_with_date,11),'YYYYMMDD') as date_where_max_result_occurs
from (select
id
,test_id
,date
,result
,MAX(lpad(to_char(result),10,'0')||to_char(date,'YYYYMMDD'))
over (partition BY id, test_id
order by date
RANGE BETWEEN CURRENT ROW AND INTERVAL '2' DAY FOLLOWING )
AS max_result_with_date
from table) -
Multiple Select List not returning multiple values
I have created a Multiple Select List, and the manual says that when multiple values are selected and the page is submmitted (I'm using a GO button), the multiple values are put into the ITEM field, separated with a colon . However, whenever I select multiple entries in my list, it only ever returns a value for the first item (like a single select list).
Does anybody know what I may have missed ? I'm still fairly new to this stuff, so I'm hoping it's fairly simple!
thanks.
p.s I'm using a dynamic list of values.Andy / Scott,
Sorry, please bear with me ... still new to this.
I have a multi select list item in page 2, called P2_MULTI_LIST. When the page is submitted, I no longer physically pass it to page 3, but how would page 3 pick up the value ? Do I need to set up a P3_MULTI_LIST item in page3 and set that item to the P2_MULTI_LIST value, or do I reference P2_MULTI_LIST directly in my Page3 SQL query ?
Also, if I then want to propogate the value in P2_MULTI_LIST down to another page from page3, via a link in a report, is this possible ?
thanks
Tim -
Trying to do something as data would be uploaded and edited in grid. At present I am using Ora_Hash.
SELECT
e.ROWID, e.Employee_Id,e.First_Name,e.Last_Name,e.Email,e.Phone_Number,e.Hire_Date,
e.Job_Id,e.Salary,e.Manager_Id,e.Department_Id,
owa_opt_lock.checksum('HR','EMPLOYEES', e.rowid) As HashForThisRow
FROM EMPLOYEES e;
ThxNo you can't.
owa_opt_lock.checksum is a function which is used for optimist locking. It locks the row(probably by using Select FOR Update mechanism) which you select and which you are trying to update. As a result, what you are eventually trying to do is that inside a Select query you are using a function that is locking the rows, which is not allowed. And thus your query will fail with the below mentioned error.
ORA-14551: cannot perform a DML operation inside a query
ORA-06512: at "SYS.DBMS_SQL", line 1575
ORA-06512: at "SYS.OWA_OPT_LOCK", line 172
14551. 00000 - "cannot perform a DML operation inside a query "
*Cause: DML operation like insert, update, delete or select-for-update
cannot be performed inside a query or under a PDML slave.
*Action: Ensure that the offending DML operation is not performed or
use an autonomous transaction to perform the DML operation within
the query or PDML slave.
Thanks,
Ishan -
How to use Cursor's Fetch to return multiple column Data
Hi ,
This is my Table :
SQL> select * from Login;
USERNAME PASSWORD CONFIRMPASSWORD
RAvi SAI SAI
Kiran Keyboard Keyboard
VARNU JAINA JAINA
This is my stored Procedure , for that Table , this is working fine , where he Cursor's Fetch is returning only a Single column of Data .
CREATE OR REPLACE PROCEDURE getEmpName(EMP_ID IN VARCHAR2)
IS
EMPNAME VARCHAR2(50);
CURSOR MYCUR
IS
SELECT USERNAME from login where USERNAME=EMP_ID;
BEGIN
OPEN MYCUR ;
LOOP
FETCH MYCUR INTO EMPNAME;
DBMS_OUTPUT.PUT_LINE(EMPNAME);
EXIT WHEN MYCUR%NOTFOUND;
END LOOP;
close MYCUR ;
END;
Now , I have a requirement as , i need to get the Password also along with Username ,
Please tell me how can i use FETCH of Cursor to populate Details into the Declared Variables
FETCH MYCUR INTO EMPNAME;
CREATE OR REPLACE PROCEDURE getEmpName(EMP_ID IN VARCHAR2)
IS
EMPNAME VARCHAR2(50);
PASSWORD VARCHAR2(50);
CURSOR MYCUR
IS
SELECT USERNAME , PASSWORD from login where USERNAME=EMP_ID;
BEGIN
OPEN MYCUR ;
LOOP
FETCH MYCUR INTO EMPNAME; // Help needed here**
DBMS_OUTPUT.PUT_LINE(EMPNAME);
EXIT WHEN MYCUR%NOTFOUND;
END LOOP;
close MYCUR ;
END;
Edited by: user10503747 on Oct 14, 2010 11:51 AM
Edited by: user10503747 on Oct 14, 2010 11:52 AM1) I'm hoping that this is a homework assignment. You would never, in the real world, store a password in clear text. And you would never, from a database design, have separate Password and ConfirmPassword columns.
2) The INTO clause can accept a list of variables. So
FETCH your_cursor_name INTO local_variable1, local_variable2;would be valid.
3) You would generally want to avoid having local variables that share the name of a column-- that leads to some pretty serious confusion related to scoping. One common convention is to prefix local variables with a "l_".
4) You would generally want local variables to inherit the type of the column rather than explicitly declaring the length of a string in your procedure. That way, if you change the table in the future, your code will continue to work.
5) And finally, a procedure that just calls DBMS_OUTPUT would cause all sorts of red flags in reality. It would make far more sense for this to be a function that returns a value than a procedure that tries to write to a buffer that the client application may or may not have created.
Justin -
Help needed in returning multiple columns from one field
Hi,
I have been given a task to rewrite the query (see below), so that I get three columns from the one field, based on their contents.
The script returns all the columns and concatenates them into one, but returns them as comma delimmited within the returned column, to generate a report.
These tables are from Siebel.
Our Oracle version is:
select banner from sys.v_$version;
--returns
Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
PL/SQL Release 8.1.7.4.0 - Production
CORE 8.1.7.0.0 Production
TNS for Solaris: Version 8.1.7.4.0 - Production
NLSRTL Version 3.4.1.0.0 - Production
Here is what I was given;
set arraysize 200
Spool d:\Daily_reports\Data\RegManage\Electricity\eCISPLUS_Product_Switch_Not_Started_Discount.csv
-- this will concatenate all the returned columns into one
SELECT DISTINCT '"'||
rtrim(ltrim(S_ORG_EXT.OU_NUM)) || '","' ||
rtrim(ltrim(S_ORG_EXT.NAME)) || '","' ||
rtrim(ltrim(S_EVT_ACT.TODO_CD)) || '","' ||
rtrim(ltrim(S_EVT_ACT.EVT_STAT_CD)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_SOURCE_TYPE)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_TXU_PRICELIST_NAME)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_CLASS_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_PEAKANYTIME_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_PA_CLASS_ELEC)) || '","' ||
rtrim(ltrim(S_ORG_EXT.X_OTH_ACCOUNT_NUMBER)) || '","' ||
rtrim(ltrim(S_ADDR_PER.X_NMI_NUM)) || '","' ||
rtrim(ltrim(S_ADDR_PER.X_NMI_CHECKSUM)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.QUOTE_NUM)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_COOLING_OFF_DATE)) || '","' ||
rtrim(ltrim(S_PROD_INT.NAME)) || '"'
The field S_PROD_INT.NAME is the one that I need to split so that it is retured
into three different columns.
1st column needed -
where S_PROD_INT.NAME Like 'Electricity TRUenergy Business Edge __ %'
OR S_PROD_INT.NAME Like 'Electricity TRUenergy Business Now __ %'
OR S_PROD_INT.NAME Like 'Electricity Go for More __ %'
OR S_PROD_INT.NAME Like 'Electricity Mates Rates __ %'
2nd column needed -
where S_PROD_INT.NAME Like 'Gas TRUenergy Business Edge __ %'
OR S_PROD_INT.NAME Like 'Gas TRUenergy Business Now __ %'
OR S_PROD_INT.NAME Like 'Gas Go for More __ %'
OR S_PROD_INT.NAME Like 'Gas Mates Rates __ %'
3rd column needed from
S_PROD_INT.NAME Like '* ___ Discount for Paying Your Bill by the Due Date')
-- Tables
FROM SIEBEL.S_ADDR_PER S_ADDR_PER,
SIEBEL.S_DOC_QUOTE S_DOC_QUOTE,
SIEBEL.S_EVT_ACT S_EVT_ACT,
SIEBEL.S_ORG_EXT S_ORG_EXT,
SIEBEL.S_PRI_LST S_PRI_LST,
SIEBEL.S_PROD_INT S_PROD_INT,
SIEBEL.S_QUOTE_ITEM S_QUOTE_ITEM
WHERE S_EVT_ACT.TARGET_OU_ID = S_ORG_EXT.ROW_ID
AND S_DOC_QUOTE.ROW_ID = S_QUOTE_ITEM.SD_ID
AND S_ADDR_PER.ROW_ID = S_ORG_EXT.PR_ADDR_ID
AND S_PROD_INT.ROW_ID = S_QUOTE_ITEM.PROD_ID
AND S_ORG_EXT.ROW_ID = S_DOC_QUOTE.TARGET_OU_ID
AND S_DOC_QUOTE.PRI_LST_ID = S_PRI_LST.ROW_ID
AND S_EVT_ACT.TODO_CD = 'Product Switch-Electricity'
AND S_EVT_ACT.EVT_STAT_CD In ('In Progress','Not Started','On Hold')
AND S_DOC_QUOTE.STAT_CD In ('Completed','Verbal Acceptance','Written Acceptance')
AND S_DOC_QUOTE.X_OFFER_TYPE = 'Product Switch'
AND (S_PROD_INT.NAME Like 'Electricity TRUenergy Business Edge __ %' OR S_PROD_INT.NAME Like 'Electricity TRUenergy Business Now __ %'
OR S_PROD_INT.NAME Like 'Electricity Go for More __ %' OR S_PROD_INT.NAME Like 'Electricity Mates Rates __ %'
OR S_PROD_INT.NAME Like 'Electricity __ Discount for Paying Your Bill by the Due Date' or S_PROD_INT.NAME Like 'Electricity TRUenergy Business Edge ___ %'
OR S_PROD_INT.NAME Like 'Electricity TRUenergy Business Now ___ %'
OR S_PROD_INT.NAME Like 'Electricity Go for More ___ %'
OR S_PROD_INT.NAME Like 'Electricity Mates Rates ___ %'
OR S_PROD_INT.NAME Like 'Electricity ___ Discount for Paying Your Bill by the Due Date')
UNION
SELECT DISTINCT '"'||
rtrim(ltrim(S_ORG_EXT.OU_NUM)) || '","' ||
rtrim(ltrim(S_ORG_EXT.NAME)) || '","' ||
rtrim(ltrim(S_EVT_ACT.TODO_CD)) || '","' ||
rtrim(ltrim(S_EVT_ACT.EVT_STAT_CD)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_SOURCE_TYPE)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_TXU_PRICELIST_NAME)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_CLASS_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_PEAKANYTIME_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_PA_CLASS_ELEC)) || '","' ||
rtrim(ltrim(S_ORG_EXT.X_OTH_ACCOUNT_NUMBER)) || '","' ||
rtrim(ltrim(S_ADDR_PER.X_NMI_NUM)) || '","' ||
rtrim(ltrim(S_ADDR_PER.X_NMI_CHECKSUM)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.QUOTE_NUM)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_COOLING_OFF_DATE)) || '"'
FROM SIEBEL.S_ADDR_PER S_ADDR_PER,
SIEBEL.S_DOC_QUOTE S_DOC_QUOTE,
SIEBEL.S_EVT_ACT S_EVT_ACT,
SIEBEL.S_ORG_EXT S_ORG_EXT,
SIEBEL.S_PRI_LST S_PRI_LST
WHERE S_EVT_ACT.TARGET_OU_ID = S_ORG_EXT.ROW_ID
AND S_ADDR_PER.ROW_ID = S_ORG_EXT.PR_ADDR_ID
AND S_ORG_EXT.ROW_ID = S_DOC_QUOTE.TARGET_OU_ID
AND S_DOC_QUOTE.PRI_LST_ID = S_PRI_LST.ROW_ID
AND S_EVT_ACT.TODO_CD = 'Product Switch-Electricity'
AND S_EVT_ACT.EVT_STAT_CD In ('In Progress','Not Started','On Hold')
AND S_DOC_QUOTE.STAT_CD In ('Completed','Verbal Acceptance','Written Acceptance')
AND S_DOC_QUOTE.X_OFFER_TYPE = 'Product Switch'
AND NOT EXISTS (SELECT /*+ unnest */ 'X'
FROM SIEBEL.S_PROD_INT S_PROD_INT, SIEBEL.S_QUOTE_ITEM S_QUOTE_ITEM
WHERE S_DOC_QUOTE.ROW_ID = S_QUOTE_ITEM.SD_ID
AND S_PROD_INT.ROW_ID = S_QUOTE_ITEM.PROD_ID
AND (S_PROD_INT.NAME Like 'Electricity TRUenergy Business Edge __ %'
OR S_PROD_INT.NAME Like 'Electricity TRUenergy Business Now __ %'
OR S_PROD_INT.NAME Like 'Electricity Go for More __ %'
OR S_PROD_INT.NAME Like 'Electricity Mates Rates __ %'
OR S_PROD_INT.NAME Like 'Electricity __ Discount for Paying Your Bill by the Due Date' or S_PROD_INT.NAME Like 'Electricity TRUenergy Business Edge ___ %'
OR S_PROD_INT.NAME Like 'Electricity TRUenergy Business Now ___ %'
OR S_PROD_INT.NAME Like 'Electricity Go for More ___ %'
OR S_PROD_INT.NAME Like 'Electricity Mates Rates ___ %'
OR S_PROD_INT.NAME Like 'Electricity ___ Discount for Paying Your Bill by the Due Date'));
spool off
This is what one row looks like;
"1-UWLHY","Alpha Vic Kelly Kay","Product Switch-Electricity","Not Started","AMBASS_50REBATE","Go Green PKANY-Powercor","QEGS1","GG","QEGS1","GG","","6407421678","8","1-UWLI6","12-JUN-07","Electricity Mates Rates 7% Discount"
we want
"1-UWLHY","Alpha Vic Kelly Kay","Product Switch-Electricity","Not Started","AMBASS_50REBATE","Go Green PKANY-Powercor","QEGS1","GG","QEGS1","GG","","6407421678","8","1-UWLI6","12-JUN-07",'Electricity Mates Rates 7% Discount","Gas TRUenergy Business Edge 10% Discount","Electricity 3% Discount for Paying Your Bill by the Due Date"
I have tried to:
- create a cursor
- each column as - ie
S_PROD_INT.NAME as "EVD",
S_PROD_INT.NAME as "GVD",
S_PROD_INT.NAME as "PbDDD"
but when I try to use them in where "EVD" Like 'etc...'
error that table does not exist
I have tried also as
where 'EVD' Like 'etc...' -- error
where EVD Like 'etc...' -- error
- create them as %TYPE variable
- more
but keep getting errors
Any ideas and help will be most appreciated as I have tried and researched all I can think and get my hands on with my limited skills.
Thanks.
Regards, SteveUnfortunately my skills are not high enough as I obviously don't know where to put the code exaclty.
I am first just running it on the top part of the script but get the error;
ORA-00900: invalid SQL statement
What I am running is; -- Commented
WITH s_prod_int AS
SELECT 1 row_id, 'Electricity Mates Rates 7% Discount' name FROM s_prod_int --these were 'FROM dual' originally, I changed to s_prod_int (Received same error regardless whether from dual or s_prod_int)
UNION ALL
SELECT 1, 'Gas TRUenergy Business Edge 10% Discount' FROM s_prod_int
UNION ALL
SELECT 1, 'Electricity 3% Discount for Paying Your Bill by the Due Date' FROM s_prod_int
SELECT DISTINCT '"'||
rtrim(ltrim(S_ORG_EXT.OU_NUM)) || '","' ||
rtrim(ltrim(S_ORG_EXT.NAME)) || '","' ||
rtrim(ltrim(S_EVT_ACT.TODO_CD)) || '","' ||
rtrim(ltrim(S_EVT_ACT.EVT_STAT_CD)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_SOURCE_TYPE)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_TXU_PRICELIST_NAME)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_CLASS_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_PEAKANYTIME_ELEC)) || '","' ||
rtrim(ltrim(S_PRI_LST.X_TXU_CISRATE_PA_CLASS_ELEC)) || '","' ||
rtrim(ltrim(S_ORG_EXT.X_OTH_ACCOUNT_NUMBER)) || '","' ||
rtrim(ltrim(S_ADDR_PER.X_NMI_NUM)) || '","' ||
rtrim(ltrim(S_ADDR_PER.X_NMI_CHECKSUM)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.QUOTE_NUM)) || '","' ||
rtrim(ltrim(S_DOC_QUOTE.X_COOLING_OFF_DATE)) || '","' ||
rtrim(ltrim(a.name)) ||'","'|| --I assume I am still able to right and left trim
rtrim(ltrim(b.name)) ||'","'||
rtrim(ltrim(c.name))||'"' name -- not sure what this name is here for (have ran with and without it here
FROM SIEBEL.S_ADDR_PER S_ADDR_PER,
SIEBEL.S_DOC_QUOTE S_DOC_QUOTE,
SIEBEL.S_EVT_ACT S_EVT_ACT,
SIEBEL.S_ORG_EXT S_ORG_EXT,
SIEBEL.S_PRI_LST S_PRI_LST,
s_prod_int a, -- I assume these needed to go in here
s_prod_int b,
s_prod_int c
SIEBEL.S_QUOTE_ITEM S_QUOTE_ITEM
WHERE S_EVT_ACT.TARGET_OU_ID = S_ORG_EXT.ROW_ID
AND S_DOC_QUOTE.ROW_ID = S_QUOTE_ITEM.SD_ID
AND S_ADDR_PER.ROW_ID = S_ORG_EXT.PR_ADDR_ID
AND S_PROD_INT.ROW_ID = S_QUOTE_ITEM.PROD_ID
AND S_ORG_EXT.ROW_ID = S_DOC_QUOTE.TARGET_OU_ID
AND S_DOC_QUOTE.PRI_LST_ID = S_PRI_LST.ROW_ID
AND S_EVT_ACT.TODO_CD = 'Product Switch-Electricity'
AND S_EVT_ACT.EVT_STAT_CD In ('In Progress','Not Started','On Hold')
AND S_DOC_QUOTE.STAT_CD In ('Completed','Verbal Acceptance','Written Acceptance')
AND S_DOC_QUOTE.X_OFFER_TYPE = 'Product Switch'
AND a.NAME LIKE 'Electricity TRUenergy Business Edge%' --Placed all the where part in the original where clause
OR a.NAME LIKE 'Electricity TRUenergy Business Now%'
OR a.NAME LIKE 'Electricity Go for More%'
OR a.NAME LIKE 'Electricity Mates Rates%'
AND a.row_id = b.row_id
AND (
b.NAME LIKE 'Gas TRUenergy Business Edge%'
OR b.NAME LIKE 'Gas TRUenergy Business Now%'
OR b.NAME LIKE 'Gas Go for More%'
OR b.NAME LIKE 'Gas Mates Rates%'
AND b.row_id = c.row_id
AND (
c.NAME LIKE '%Discount for Paying Your Bill by the Due Date'
NAME -- Not sure what this NAME is for - tried with and without - same error -
How to Make FUNCTION to return multiple column and multiple row output
Hi All,
Kindly Share ur Idea;Thanks in Advance;
i Have Table Demo.
table DEMO:
id name
1 a10
1 a11
1 a12
2 b10
2 b11
3 ccc
and the function is like:
create or replace function (p1 number) return varchar2 as
vid number;
vname varchar2(20);
begin
select id,name into vid,vname from demo where id=p1;
return v1;
end;
this function returns output for id=3;
BUT,
i need output as (for input as 1)
vid vname
1 a10
1 a11
1 a12A function returns a single datatype.
That datatype may be an atomic datatype (varchar2, number etc.) or it may be an object/record datatype, or even a collection datatype.
Where are you going to use this function? In PL/SQL or SQL?
If you are wanting to use it in SQL, then you would need a pipelined function e.g.
SQL> CREATE OR REPLACE TYPE myemp AS OBJECT
2 ( empno number,
3 ename varchar2(10),
4 job varchar2(10),
5 mgr number,
6 hiredate date,
7 sal number,
8 comm number,
9 deptno number
10 )
11 /
Type created.
SQL> CREATE OR REPLACE TYPE myrectable AS TABLE OF myemp
2 /
Type created.
SQL> CREATE OR REPLACE FUNCTION pipedata(p_min_row number, p_max_row number) RETURN myrectable PIPELINED IS
2 v_obj myemp := myemp(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3 BEGIN
4 FOR e IN (select *
5 from (
6 select e.*
7 ,rownum rn
8 from (select * from emp order by empno) e
9 )
10 where rn between p_min_row and p_max_row)
11 LOOP
12 v_obj.empno := e.empno;
13 v_obj.ename := e.ename;
14 v_obj.job := e.job;
15 v_obj.mgr := e.mgr;
16 v_obj.hiredate := e.hiredate;
17 v_obj.sal := e.sal;
18 v_obj.comm := e.comm;
19 v_obj.deptno := e.deptno;
20 PIPE ROW (v_obj);
21 END LOOP;
22 RETURN;
23 END;
24 /
Function created.
SQL> select * from table(pipedata(1,5));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-1980 00:00:00 800 20
7499 ALLEN SALESMAN 7698 20-FEB-1981 00:00:00 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-1981 00:00:00 1250 500 30
7566 JONES MANAGER 7839 02-APR-1981 00:00:00 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-1981 00:00:00 1250 1400 30
SQL> select * from table(pipedata(6,10));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-1981 00:00:00 2850 30
7782 CLARK MANAGER 7839 09-JUN-1981 00:00:00 2450 10
7788 SCOTT ANALYST 7566 19-APR-1987 00:00:00 3000 20
7839 KING PRESIDENT 17-NOV-1981 00:00:00 5000 10
7844 TURNER SALESMAN 7698 08-SEP-1981 00:00:00 1500 0 30
SQL> select * from table(pipedata(11,15));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7876 ADAMS CLERK 7788 23-MAY-1987 00:00:00 1100 20
7900 JAMES CLERK 7698 03-DEC-1981 00:00:00 950 30
7902 FORD ANALYST 7566 03-DEC-1981 00:00:00 3000 20
7934 MILLER CLERK 7782 23-JAN-1982 00:00:00 1300 10
SQL>If you are using it in PL/SQL then just populating a collection datatype and returning that will do. Though you should question why you want to pass large amounts of data around like that first.
Explain your purpose and what you are intending to do and we can recommend the best way.
{message:id=9360002} -
I want a select statement to return two values, sum of one column and customer number
I have two columns one called invoice_number and the other invoice_amount. I want a select statement to return two columns.... invoice_number and then the sum of the invoice_amount(s) for each unique invoice number.
SELECT sum(invoice_amount) AS Totalinvoice_amount FROM InvoiceTB where invoice_number = 'INV102'
This is where I've started, which returns:
Totalinvoice_amount
500.00
Any help is appreciated.
Please mark my post as helpful or the answer or better yet.... both! :) Thanks!Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Pl/sql block returning multiple rows
Hi,
I've created a plsql block which obtains an id from a name and then uses this id in another sql statement. The select statement to get the id works fine and the correct id is placed into the variable awardID.
when i try to use this variable in another select statement it returns multiple rows. but when i just use the id in the select statement it works fine.
select AwardID into awardID
from award_objtabA
where Name = awardName;
this returns the correct id '5999'
select Points into award_points
from award_objtabA
where AwardID = awardID;
this returns multiple rows
select Points into award_points
from award_objtabA
where AwardID = 5999;
this works fine
can anybody help with this confusing error?
Cheersselect AwardID
into awardID <= the same column
from award_objtabA
where Name = awardName;
this returns the correct id '5999'
select Points
into award_points
from award_objtabA
where AwardID = awardID; <= the same column
this returns multiple rows
to solve use different name not exactly the same as that of the column name
select AwardID
into vAwardID
from award_objtabA
where Name = awardName;
select Points
into award_points
from award_objtabA
where AwardID = vAwardID;as Alex and Kamal have suggested use different name for your variable. -
Multiple Column Report Question
Post Author: LCS213
CA Forum: Crystal Reports
I'm creating a student transcript report that has multiple columns, and is three groups deep. The first two groups need to be in a columnar layout. However, I'd like the third group to contain a subreport that spans the width of the entire page.
Are there any options within Crystal XI to do this? I know in the Section Expert, i can format the detail level with multiple columns, and on the Layout tab, I can select "Format Groups with multiple columns". But that seems like it either formats ALL groups or NO groups. Am I mistaken? Is there a workaround I'm missing?
Thanks in advance for any help on this issue!Hi Dan,
According to your description, we are not clear about the “6 sections for each Additional task”. Do you mean six data records? If so, based on my understanding, there are three groups and eighteen records in your report. You want to make those records appear
on eighteen separated pages, right?
In Reporting Service, we can add a page break to groups within data regions to control the amount of information on each page. In your scenario, you should add page break to group “Sub task”, group “Additional Task” and details. As we tested in our environment,
add page break to these records, the report appears in eighteen pages. Please refer to steps below:
1.On Group Properties of Details1, choose “Between each instance of a group” option on Page Breaks page. Please refer to screenshot below:
Choose same option on Group Properties for group “Month” and “Name”.
2.Click drop-down button next to Column Groups, then choose Advanced Mode.
3.Click on Static under Row Groups, set RepeatOnNewPage as True in Tablix Member Properties.
4.Click Preview, the results before and after adding page break look like below:
If you still have any question, please post some detail information or screenshot about the report design you expect.
Best regards,
Qiuyun Yu -
Single query return multiple value
Hi ALL,
I have a sql query as below :
select order_number,
(select decode(hcp.contact_point_purpose,'ABC',hcp.email_address,'CDE',hcp.email_address,null)
from hz_contact_points,
hz_parties hz
WHERE hz.party_id=hcp.owner_table_id) Email
FROM oe_order_headers_all h
WHERE h.order_number='102'
Actually the problem i am facing is the inner select query is returning multiple row , so my main query is erroring out, i need to capture the multiple row.
in the above example the inner decode statement returning two mail address, i need to capture that, but while executing the whole query it is erroring out as saying single query returns multiple values.
please help me on this to capture multiple values
Thanks>
select order_number,
(select decode(hcp.contact_point_purpose,'ABC',hcp.email_address,'CDE',hcp.email_address,null)
from hz_contact_points,
hz_parties hz
WHERE hz.party_id=hcp.owner_table_id
/* a join is missing here that points to table oe_order_headers_all h*/) Email
FROM oe_order_headers_all h
WHERE h.order_number='102'
>
I can see a join missing in your select sub query.
However, if you you are still getting the single query return multiple value error then you need to replace the sql sub query
select decode(hcp.contact_point_purpose,'ABC',hcp.email_address,'CDE',hcp.email_address,null)
from hz_contact_points,
hz_parties hz
WHERE hz.party_id=hcp.owner_table_id)by decode(hcp.contact_point_purpose,'ABC',hcp.email_address,'CDE',hcp.email_address,null)
and join the two tables in you main query..
Regards
Biju
ED: Provide test script and your expected output to get what you want exacly!
Edited by: biju2012 on Sep 7, 2012 12:06 AM -
Return multiple values from a function to a SELECT statement
I hope I've provided enough information here. If not, just let me know what I'm missing.
I am creating a view that will combine information from a few tables. Most of it is fairly straightforward, but there are a couple of columns in the view that I need to get by running a function within a package. Even this is fairly straightforward (I have a function named action_date in a package called rp, for instance, which I can use to return the date I need via SELECT rp.action_date(sequence_number).
Here's the issue: I actually need to return several bits of information from the same record (not just action_date, but also action_office, action_value, etc.) - a join of the tables won't work here as I'll explain below. I can, of course, run a separate function for each statement but that is obviously inefficient. Within the confines of the view select statement however, I'm not sure how to return each of the values I need.
For instance, right now, I have:
Table1:
sequence_number NUMBER(10),
name VARCHAR(30),
Table2:
Table1_seq NUMBER(10),
action_seq NUMBER(10),
action_date DATE,
action_office VARCHAR(3),
action_value VARCHAR(60),
I can't simply join Table1 and Table2 because I have to do some processing in order to determine which of the matching returned rows I actually need to select. So the package opens a cursor and processes each row until it finds the one that I need.
The following works but is inefficient since all of the calls to the package will return columns from the same record. I just don't know how to return all the values I need into the SELECT statement.
CREATE VIEW all_this_stuff AS
SELECT sequence_number, name,
rp.action_date(sequence_number) action_date,
rp.action_office(sequence_number) action_office,
rp.action_value(sequence_number) action_value
FROM table1
Is there a way to return multiple values into my SELECT statement or am I going about this all wrong?
Any suggestions?
Thanks so much!Hi,
What you want is a Top-N Query , which you can do using the analytic ROW_NUMBER function in a sub-query, like this:
WITH got_rnum AS
SELECT action_seq, action_dt, action_office, action_type, action_value
, ROW_NUMBER () OVER ( ORDER BY action_date
, action_seq
, action_serial
) AS rnum
FROM table2
WHERE action_code = 'AB'
AND action_office LIKE 'E' -- Is this right?
SELECT action_seq, action_dt, action_office, action_type, action_value
FROM got_rnum
WHERE rnum = 1
;As written, this will return (at most) one row.
I suspect you'll really want to get one row for each group , where a group is defined by some value in a table to which you're joining.
In that case, add a PARTITION BY clause to the ROW_NUMBER function.
If you'd post a little sample data (CREATE TABLE and INSERT statements), I could show you exactly how.
Since I don't have your tables, I'll show you using tables in the scott schema.
Here's a view that has data from the scott.dept table and also from scott.emp, but only for the most senior employee in each department (that is, the employee with the earliest hiredate). If there happens to be a tie for the earliest hiredate, then the contender with the lowest empno is chosen.
CREATE OR REPLACE VIEW senior_emp
AS
WITH got_rnum AS
SELECT d.deptno
, d.dname
, e.empno
, e.ename
, e.hiredate
, ROW_NUMBER () OVER ( PARTITION BY d.deptno
ORDER BY e.hiredate
, e.empno
) AS rnum
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
SELECT deptno
, dname
, empno
, ename
, hiredate
FROM got_rnum
WHERE rnum = 1
SELECT *
FROM senior_emp
;Output:
. DEPTNO DNAME EMPNO ENAME HIREDATE
10 ACCOUNTING 7782 CLARK 09-JUN-81
20 RESEARCH 7369 SMITH 17-DEC-80
30 SALES 7499 ALLEN 20-FEB-81
By the way, one of the conditions in the query you posted was
action_office LIKE 'E'which is equivalent to
action_office = 'E'(LIKE is always equivalent to = if the string after LIKE doesn't contain any wildcards.)
Did you mean to say that, or did you mean something like this:
action_office LIKE 'E%'instead?
Maybe you are looking for
-
What App allows me to stream music stored on the computer to IOS 4.3
I could have swore that a feature of Airplay in 4.3 was the ability to listen to music on the iphone that is stored on the computer. I feel like I have searched through both the iPod app and the Remote app and can't seem to figure out how to do that.
-
Snow Leo is blocking ports!
Hello dear folks! I've got some nasty persistent problem with my mac. Neither of my torrent clients can establish connection; whenever I open preferences, it does say "port XXX : blocked". I've tried everything, enabling/disabling the FW, adding all
-
Hi all, I have a field of length 8. I need to split it into components of different lengths. How would i do it in ABAP? Bala
-
Asant cannot find package, javac can??
Hi All, I am working through the J2EE training disc CDJ-310A. I am trying to build the bankproject with asant and build.xml. Compiling with asant. Classpath set and works (javac finds it and set looks okay). Javac finds junit fine. Is there some othe
-
Quad freezes with horizontal flashing
My Quad freezes in several modes; in several programs with horizontal flashing that preceed the freeze. Once frozen, the pointer will move with mouse control but everything else is frozen, nothing responds to the mouse or the keyboard or special key