SQL query using connect by clause
Hi,
I have table:
SQL> col id for 999
SQL> col code for a30
SQL> select id,code from t1;
ID CODE
704 ,2,3,7,8,
707 ,15,12,17,18,
742 ,23,25,27,28,
5 ,53,65,67,58,
4 rows selected.---
When I run query:
select id,
substr(code,instr(code,chr(44),1,level)+1,instr(code,chr(44),1,level+1)-instr(code,chr(44),1,level)-1) code
from(
select id,
code,
length(code) - length(replace(code,',',''))-1 len
from t1
where id=5)
connect by level <= len;
ID CODE
5 53
5 65
5 67
5 58
4 rows selected.How to modify my query to get this output:
ID CODE
5 53
5 65
5 67
5 58
704 2
704 3
704 7
704 8
707 15
707 12
707 17
707 18
742 23
742 25
742 27
742 28What is the best way to get this output?
My db:
SQL> select * from v$version;
BANNER
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
PL/SQL Release 9.2.0.6.0 - Production
CORE 9.2.0.6.0 Production
TNS for Solaris: Version 9.2.0.6.0 - Production
NLSRTL Version 9.2.0.6.0 - Production
5 rows selected.Thanks for answers!
Regards,
Marko
Hi,
Generate a separate "counter table" that has the numbers 1, 2, 3, ... , x, where x is the greatest number you'll ever need.
Join to this table, using a join condition that gets the exact numbers you need for each row.
Use the number from the counter table where you are currently using LEVEL.
That is:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= ( SELECT MAX ( LENGTH (code)
- LENGTH (REPLACE (code, ','))
FROM t1
) - 1
select id,
substr(code,instr(code,chr(44),1,n)+1,instr(code,chr(44),1,n+1)-instr(code,chr(44),1,n)-1) code
FROM t1
JOIN cntr ON cntr.n <= LENGTH (code) - LENGTH (REPLACE (code, ','))
;If you're using Oracle 10 (or higher) regular expressions would simplify this a lot.
Similar Messages
-
Recursive joins / how to define query using connect by clause
Hi,
I have a table A have 1-1 reltionship with table Employee
Structure of table is as follows
Table A
id name employee_id
100 aa 1
200 bb 2
300 cc 3
400 dd 4
500 ee 5
Table Employee
id parent_id
1 null
2 1
3 2
4 3
5 1
6 1
I want to get all records from A table whose employee_id is equal to 2 or (recursive)decendants/child of employee with id 2 (i.e. employee with id 2,3,4 )
i.e I want recursively join where in I get records from table A as
id name employee_id
200 bb 2
300 cc 3 (because it is child of employee with id 2)
400 dd 4 (becaue it is child of employee with id 3 which is child of 2)
I know we can use In clause , but it will be performance wise not good.
Can we do it in Toplink without using IN clause?
Oracle has connect by clause, but other database might not have this caluse. So how can write a toplink query which can run on any database.
Any help is highly appreciated.
Thanks a lot.You can use TopLink's Hierarchical Query support but this only works with Oracle DB as it relies on the database to perform the query.
--Shaun -
How to obtain the transformed SQL query using SEM_MATCH
Dear all,
Is it possible to get the transformed relational SQL query when using the SEM_MATCH prefix, by querying directly on the database. We are able to obtain the relational SQL query using Joseki/Jena, however this is not the way to go for us. We would like (if possible) to get it straight from the oracle database by logging or something.
Kind regards.
MaxHi Max,
Just to clarify. What SEM_MATCH prefix are you talking about?
A SEM_MATCH based query is indeed a SQL query as SEM_MATCH is a SQL table function. So if you don't want to go through Java APIs or web service endpoint, then running SEM_MATCH directly should give you what you need. Or maybe you just want to see the underlying generated (from SEM_MATCH) SQL query. If that is true, can you please tell us why?
Thanks,
Zhe Wu -
Set TIMEOUT on OCCI sql query using ?
Is there a way to set a timeout on a sql query using OCCI ?
OCCI does not provide an API to set timeouts on SQL Queries.
You can use the CREATE PROFILE command and assign resources at
a user level. The restrictions may be applied to memory/resources which
in turn would control the SQL execution time. Please check the
CREATE PROFILE command.
Rgds
Amogh -
How to find sql query using sqlid
Hi
I am not aware of that sqlid .DBA is saying a particular sqlid is making problem .how to find the particular sql query using sql_id ?Are you aware of modplsql when i executed the query the result is like
DECLARE
rc__ NUMBER;
simple_list__ OWA_UTIL.vc_arr;
complex_list__ OWA_UTIL.vc_arr;
BEGIN
OWA.init_cgi_env (:n__, :nm__, :v__);
HTP.htbuf_len := 84;
NULL;
NULL;
simple_list__ (1) := 'sys.%';
simple_list__ (2) := 'dbms\_%';
simple_list__ (3) := 'utl\_%';
simple_list__ (4) := 'owa\_%';
simple_list__ (5) := 'owa.%';
simple_list__ (6) := 'htp.%';
simple_list__ (7) := 'htf.%';
simple_list__ (8) := 'wpg_docload.%';
IF ((owa_match.match_pattern ('Oly_browse.oly_pattern ',
simple_list__,
complex_list__,
TRUE
THEN
rc__ := 2;
ELSE
NULL;
NULL;
oly_browse.oly_pattern (search_phrase => :search_phrase, --Oly_browse.oly_pattern is package name
btn => :btn,
p_qual => :p_qual,
p_bcat => :p_bcat,
p_stdy => :p_stdy,
p_bloc => :p_bloc,
z => :z
IF (WPG_DOCLOAD.is_file_download)
THEN
rc__ := 1;
WPG_DOCLOAD.get_download_file (:doc_info);
NULL;
NULL;
NULL;
COMMIT;
ELSE
rc__ := 0;
NULL;
NULL;
NULL;
COMMIT;
OWA.get_page (:data__, :ndata__);
END IF;
END IF;
:rc__ := rc__;
END; Edited by: vishnu prakash on Sep 8, 2010 10:16 PM -
Can Portal Report from SQL Query use where column IN (:bind_variable)
I would like to create a portal report from sql query with IN (:bind_variable) in the where clause. The idea is that the user would enter comma-separated or comma-quote-separated values for the bind_variable. I have tried this several ways but nothing seems to work. Can this be done?
TrentonHi,
Which version of portal are you using. This is a bug. It has been fixed in 30984.
Thanks,
Sharmila -
Sql query using LIKE is very slow
Hi,
I am running SQL query with LIKE on two table with more than two million records on oracle 10g and sun solaris OS. Does anybody have any idea or alternative to improve this query?
it will never use index because i m using LIKE '%xyz%'. I Have posted query below
PROCEDURE order_search
v_search_type_in IN VARCHAR2
,v_search_value1_in IN VARCHAR2 DEFAULT 'NONE'
,v_search_value2_in IN VARCHAR2 DEFAULT 'NONE'
,v_group_id_in IN bcf_groups.group_id%TYPE DEFAULT 0
,v_open_in IN NUMBER DEFAULT 0
,v_requested_in IN NUMBER DEFAULT 0
,v_cancelled_in IN NUMBER DEFAULT 0
,v_closed_in IN NUMBER DEFAULT 0
,v_employee_id_in IN sxweb00.customer.customer_id%TYPE DEFAULT 0
,outcursor IN OUT FulfillmentCurType
IS
v_status_code NUMBER;
v_upper_search_value1 VARCHAR2(500);
v_lower_search_value1 VARCHAR2(500);
v_open_status VARCHAR2(10);
v_closed_status VARCHAR2(10);
v_cancelled_status VARCHAR2(10);
v_requested_status VARCHAR2(10);
v_group_for_search bcf_groups.group_id%TYPE;
v_sql_select VARCHAR2(4000);
v_sql_from VARCHAR2(4000);
v_sql_where VARCHAR2(4000);
v_sql_order_by VARCHAR2(4000);
v_group_where VARCHAR2(100);
v_status_where VARCHAR2(500);
BEGIN
IF v_open_in = 1 THEN
v_open_status := 'OPEN';
END IF;
IF v_closed_in = 1 THEN
v_closed_status := 'CLOSED';
END IF;
IF v_cancelled_in = 1 THEN
v_cancelled_status := 'CANCELLED';
END IF;
IF v_requested_in = 1 THEN
v_requested_status := 'REQUESTED';
END IF;
IF UPPER(v_search_type_in) = 'GROUP'
THEN
v_group_for_search := v_search_value1_in;
ELSE
v_group_for_search := v_group_id_in;
END IF;
-- This is the select statement used for all search types
v_sql_select := ' '
|| 'po.order_id order_id '
|| ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null) first_location '
|| ',TO_CHAR(po.order_date, ''mm/dd/yyyy'') order_date '
|| ',' || v_group_for_search || ' group_id '
|| ',TRIM(TO_CHAR(bcf_fulfillment.get_shipping_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_tax_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_subtotal(po.order_id,' || v_group_for_search || ') ,''$999,999,999,999,999,990.99'')) total '
|| ',bcf_fulfillment.get_billing_name (po.customer_id) billing_name '
|| ',bcf_fulfillment.get_open_and_assigned_count(po.order_id,' || v_group_for_search || ') open_and_assigned_count '
|| ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') divisions '
|| ',bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ' ) division_count '
|| ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ') picker_name '
|| ',bcf_fulfillment.get_picker_count (po.order_id,' || v_group_for_search || ') picker_count '
|| ',bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search || ') order_status '
|| ',po.customer_id customer_id '
-- IF there is only one unique picker then that means there are no unassigned or NULL pickers
-- therefore by default the checkbox should not be selected else the count picker_count is
-- 0 or > 1 meaning that at least one line item has an unassigned employee_id and by default
-- the checkbox should be selected
|| ',DECODE(bcf_fulfillment.get_picker_count(po.order_id,' || v_group_for_search || '),1 ,0,1) print_cb '
|| ',bcf_fulfillment.get_fulfillment_codes(po.order_id,' || v_group_for_search || ') fulfillment_types '
|| ',bcf_fulfillment.has_group_been_reassigned(po.order_id,' || v_group_for_search || ') group_reassigned_flag '
|| ',bcf_fulfillment.get_chain_codes(po.order_id,' || v_group_for_search || ') chain_codes '
|| ',po.status_id order_status_id '
|| ',bcf_fulfillment.get_viewed_by_picker_flag(po.order_id,' || v_group_for_search || ') viewed_by_picker_flag '
|| ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ', ''N'') pickers_without_unassigned '
|| ',bcf_fulfillment.get_group_list (po.order_id) group_list '
|| ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ') line_item_count '
|| ',bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') shipping_priority'
-- Anna wants all users to know and always wants to know no matter what the status of the auth or order
|| ',(SELECT decode(count(*),0,''N'',''Y'') FROM bcf_payment WHERE ics_rflag = ''DAVSNO'' AND order_id = po.order_id AND status_code IN (''ATH2'',''FUL2'',''CPT0'',''CPT2'')) avs_failed'
|| ',trunc(po.order_date) date_for_sort'
-- When we need to look at all orders then the v_group_for_search will be zero i.e. customer service rep
IF v_group_for_search = 0 THEN
-- This is the core tables used by most search types
v_sql_from := ' '
|| 'FROM product_order po'
-- || ',bcf_lookup lu'
-- This is the core table join needed for most search types
v_sql_where := ' '
|| 'WHERE 1 = 1 '
-- No orders will be looked at for fulfillment purposes that occurred before the minimum
-- shipping cost functionality was added
-- || 'AND po.order_date >= lu.implement_date '
|| 'AND po.order_date >= (SELECT min(implement_date) FROM bcf_lookup) '
|| 'AND po.submitted_flag = 1 '
|| 'AND po.channel_code = ''BCF'' '
-- When we are looking at all orders then we are not going to restrict it by group
v_group_where := ' ';
-- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
-- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
-- the first search of a search type is done.
v_status_where := ' '
|| 'AND bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search|| ') '
|| 'IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
ELSE
-- This is the core tables used by most search types
v_sql_from := ' '
|| 'FROM product_order po'
|| ',bcf_product_order_groups pog '
-- This is the core table join needed for most search types
v_sql_where := ' '
|| 'WHERE 1 = 1 '
|| 'AND po.order_id = pog.order_id '
|| 'AND po.channel_code = ''BCF'' '
-- This is used only where the search type requires searching by a group.
v_group_where := ' '
|| 'AND pog.group_id = ' || v_group_for_search
-- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
-- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
-- the first search of a search type is done.
v_status_where := ' '
|| 'AND pog.dn_status IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
END IF;
-- If an employee_id is provided then we need to add in a where clause so that only orders that belong to that
-- employee are pulled
IF v_employee_id_in > 0
THEN
v_sql_where := v_sql_where
|| 'AND pog.dn_employee_id = ' || TO_CHAR(v_employee_id_in) || ' '
END IF;
IF v_group_id_in = get_group_fulfillment_center1
THEN
v_sql_order_by := 'ORDER BY '
|| 'bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ') '
|| ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') '
|| ',po.order_id '
ELSIF v_group_id_in = c_GROUP_FULFILLMENT_CENTER_212
THEN
v_sql_order_by := 'ORDER BY '
|| 'bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
|| ',TRUNC(po.order_date)'
|| ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ')'
|| ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null)'
|| ',po.order_id'
ELSE
v_sql_order_by := 'ORDER BY '
|| ' bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
|| ',chain_codes DESC'
|| ',po.order_id '
END IF;
IF UPPER(v_search_type_in) = 'ORDERNUMBER'
THEN
v_sql_where := v_sql_where || v_group_where
|| 'AND po.order_id = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'STATUSONLY'
THEN
v_sql_where := v_sql_where || v_group_where || v_status_where
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
ELSIF UPPER(v_search_type_in) = 'BILLINGNAME'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.upper_billing_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'GROUP'
THEN
IF v_search_value1_in = '0' -- Unassigned
THEN
v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
v_sql_where := v_sql_where || ' ' || v_status_where
|| 'AND po.order_id = pod.order_id '
|| 'AND pod.group_id IS NULL '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
ELSE
v_sql_where := v_sql_where || ' ' || v_status_where
|| 'AND pog.group_id = :v_group_for_search '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_group_for_search;
END IF;
ELSIF UPPER(v_search_type_in) = 'SHIPPINGNAME'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.upper_shipping_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'USERNAME'
THEN
v_lower_search_value1 := LOWER(v_search_value1_in);
v_sql_from := v_sql_from || ' ,sxweb01.customer c ';
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.customer_id = c.customer_id '
|| 'AND c.username LIKE ''%'' || :v_lower_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_lower_search_value1;
ELSIF UPPER(v_search_type_in) = 'BILLINGADDRESS1'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_from := v_sql_from || ' ,order_payment op '
|| ' ,credit_card cc '
|| ' ,om_address oma '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = op.business_object_id '
|| 'AND op.business_object_type = ''ORDR'' '
|| 'AND op.payment_method = ''CRCD'' '
|| 'AND op.payment_method_id = cc.credit_card_id '
|| 'AND cc.address_id = oma.address_id '
|| 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'SHIPPINGADDRESS1'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_from := v_sql_from || ' ,shipping_info si '
|| ' ,om_address oma '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = si.order_id '
|| 'AND si.ship_to_address_id = oma.address_id '
|| 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'BILLINGPHONE'
THEN
v_sql_from := v_sql_from || ' ,order_payment op '
|| ' ,credit_card cc '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = op.business_object_id '
|| 'AND op.business_object_type = ''ORDR'' '
|| 'AND op.payment_method = ''CRCD'' '
|| 'AND op.payment_method_id = cc.credit_card_id '
|| 'AND cc.home_phone = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'SHIPPINGPHONE'
THEN
v_sql_from := v_sql_from || ' ,shipping_info si '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = si.order_id '
|| 'AND si.ship_phone = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'CREDITCARDNUMBER'
THEN
v_sql_from := v_sql_from || ' ,order_payment op '
|| ' ,credit_card cc '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = op.business_object_id '
|| 'AND op.business_object_type = ''ORDR'' '
|| 'AND op.payment_method = ''CRCD'' '
|| 'AND op.payment_method_id = cc.credit_card_id '
|| 'AND cc.credit_card_number = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'PONUMBER'
THEN
v_sql_from := ' '
|| 'FROM ' || ' product_order_detail_actv_v pod ,' || SUBSTR(v_sql_from, 6);
v_sql_where := v_sql_where || v_group_where
|| 'AND pod.po_no = :v_search_value1_in '
|| 'AND po.order_id = pod.order_id '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT /*+ ORDERED */ DISTINCT ' || v_sql_select || v_sql_from || v_sql_where
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'USERASSIGNED'
THEN
IF v_search_value1_in = '0' -- 'Unassigned'
THEN
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND pog.unassigned_flag = ''Y'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
ELSE
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND pog.dn_employee_id = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
END IF;
ELSIF UPPER(v_search_type_in) = 'ORDERDATE'
THEN
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND po.order_date BETWEEN TO_DATE( :v_search_value1_in, ''MON DD YYYY HH24:MI:SS'') AND TO_DATE( :v_search_value2_in ,''MON DD YYYY HH24:MI:SS'') '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in, v_search_value2_in;
ELSIF UPPER(v_search_type_in) = 'PRINTJOB'
THEN
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select
|| ' '
|| 'FROM product_order po '
|| ' ,bcf_print_jobs pj '
|| 'WHERE 1 = 1 '
|| 'AND pj.order_id = po.order_id '
|| 'AND pj.username = :v_search_value1_in '
|| 'AND pj.create_date = TO_DATE( :v_search_value2_in,''MON DD YYYY HH24:MI:SS'') '
|| v_sql_order_by
USING v_search_value1_in, v_search_value2_in;
ELSIF UPPER(v_search_type_in) = 'FULFILLMENTTYPE'
THEN
v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND pod.order_id = po.order_id '
-- Because we have to drill down the the POD level
-- we now need to ensure the pod records match on group
|| 'AND pod.group_id = ' || v_group_for_search
-- We curr. have 3 fufillment types FC165, FC212, FCBABY
-- so substr on 1st char works for Garry
|| 'AND UPPER(SUBSTR(pod.fulfillment_type,1,1)) = UPPER(:v_search_value1_in) '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'INSUFFICIENTAUTHORIZATION'
THEN
v_sql_where := v_sql_where
|| 'AND bcf_get_authorization_amount(po.order_id) > 0'
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by;
END IF;
END order_search;
Thanks
V
Edited by: vishal patel on Oct 23, 2008 3:59 PMPROCEDURE order_search
v_search_type_in IN VARCHAR2
,v_search_value1_in IN VARCHAR2 DEFAULT 'NONE'
,v_search_value2_in IN VARCHAR2 DEFAULT 'NONE'
,v_group_id_in IN bcf_groups.group_id%TYPE DEFAULT 0
,v_open_in IN NUMBER DEFAULT 0
,v_requested_in IN NUMBER DEFAULT 0
,v_cancelled_in IN NUMBER DEFAULT 0
,v_closed_in IN NUMBER DEFAULT 0
,v_employee_id_in IN sxweb00.customer.customer_id%TYPE DEFAULT 0
,outcursor IN OUT FulfillmentCurType
IS
v_status_code NUMBER;
v_upper_search_value1 VARCHAR2(500);
v_lower_search_value1 VARCHAR2(500);
v_open_status VARCHAR2(10);
v_closed_status VARCHAR2(10);
v_cancelled_status VARCHAR2(10);
v_requested_status VARCHAR2(10);
v_group_for_search bcf_groups.group_id%TYPE;
v_sql_select VARCHAR2(4000);
v_sql_from VARCHAR2(4000);
v_sql_where VARCHAR2(4000);
v_sql_order_by VARCHAR2(4000);
v_group_where VARCHAR2(100);
v_status_where VARCHAR2(500);
BEGIN
IF v_open_in = 1 THEN
v_open_status := 'OPEN';
END IF;
IF v_closed_in = 1 THEN
v_closed_status := 'CLOSED';
END IF;
IF v_cancelled_in = 1 THEN
v_cancelled_status := 'CANCELLED';
END IF;
IF v_requested_in = 1 THEN
v_requested_status := 'REQUESTED';
END IF;
IF UPPER(v_search_type_in) = 'GROUP'
THEN
v_group_for_search := v_search_value1_in;
ELSE
v_group_for_search := v_group_id_in;
END IF;
-- This is the select statement used for all search types
v_sql_select := ' '
|| 'po.order_id order_id '
|| ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null) first_location '
|| ',TO_CHAR(po.order_date, ''mm/dd/yyyy'') order_date '
|| ',' || v_group_for_search || ' group_id '
|| ',TRIM(TO_CHAR(bcf_fulfillment.get_shipping_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_tax_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_subtotal(po.order_id,' || v_group_for_search || ') ,''$999,999,999,999,999,990.99'')) total '
|| ',bcf_fulfillment.get_billing_name (po.customer_id) billing_name '
|| ',bcf_fulfillment.get_open_and_assigned_count(po.order_id,' || v_group_for_search || ') open_and_assigned_count '
|| ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') divisions '
|| ',bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ' ) division_count '
|| ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ') picker_name '
|| ',bcf_fulfillment.get_picker_count (po.order_id,' || v_group_for_search || ') picker_count '
|| ',bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search || ') order_status '
|| ',po.customer_id customer_id '
-- IF there is only one unique picker then that means there are no unassigned or NULL pickers
-- therefore by default the checkbox should not be selected else the count picker_count is
-- 0 or > 1 meaning that at least one line item has an unassigned employee_id and by default
-- the checkbox should be selected
|| ',DECODE(bcf_fulfillment.get_picker_count(po.order_id,' || v_group_for_search || '),1 ,0,1) print_cb '
|| ',bcf_fulfillment.get_fulfillment_codes(po.order_id,' || v_group_for_search || ') fulfillment_types '
|| ',bcf_fulfillment.has_group_been_reassigned(po.order_id,' || v_group_for_search || ') group_reassigned_flag '
|| ',bcf_fulfillment.get_chain_codes(po.order_id,' || v_group_for_search || ') chain_codes '
|| ',po.status_id order_status_id '
|| ',bcf_fulfillment.get_viewed_by_picker_flag(po.order_id,' || v_group_for_search || ') viewed_by_picker_flag '
|| ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ', ''N'') pickers_without_unassigned '
|| ',bcf_fulfillment.get_group_list (po.order_id) group_list '
|| ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ') line_item_count '
|| ',bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') shipping_priority'
-- Anna wants all users to know and always wants to know no matter what the status of the auth or order
|| ',(SELECT decode(count(*),0,''N'',''Y'') FROM bcf_payment WHERE ics_rflag = ''DAVSNO'' AND order_id = po.order_id AND status_code IN (''ATH2'',''FUL2'',''CPT0'',''CPT2'')) avs_failed'
|| ',trunc(po.order_date) date_for_sort'
-- When we need to look at all orders then the v_group_for_search will be zero i.e. customer service rep
IF v_group_for_search = 0 THEN
-- This is the core tables used by most search types
v_sql_from := ' '
|| 'FROM product_order po'
-- || ',bcf_lookup lu'
-- This is the core table join needed for most search types
v_sql_where := ' '
|| 'WHERE 1 = 1 '
-- No orders will be looked at for fulfillment purposes that occurred before the minimum
-- shipping cost functionality was added
-- || 'AND po.order_date >= lu.implement_date '
|| 'AND po.order_date >= (SELECT min(implement_date) FROM bcf_lookup) '
|| 'AND po.submitted_flag = 1 '
|| 'AND po.channel_code = ''BCF'' '
-- When we are looking at all orders then we are not going to restrict it by group
v_group_where := ' ';
-- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
-- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
-- the first search of a search type is done.
v_status_where := ' '
|| 'AND bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search|| ') '
|| 'IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
ELSE
-- This is the core tables used by most search types
v_sql_from := ' '
|| 'FROM product_order po'
|| ',bcf_product_order_groups pog '
-- This is the core table join needed for most search types
v_sql_where := ' '
|| 'WHERE 1 = 1 '
|| 'AND po.order_id = pog.order_id '
|| 'AND po.channel_code = ''BCF'' '
-- This is used only where the search type requires searching by a group.
v_group_where := ' '
|| 'AND pog.group_id = ' || v_group_for_search
-- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
-- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
-- the first search of a search type is done.
v_status_where := ' '
|| 'AND pog.dn_status IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
END IF;
-- If an employee_id is provided then we need to add in a where clause so that only orders that belong to that
-- employee are pulled
IF v_employee_id_in > 0
THEN
v_sql_where := v_sql_where
|| 'AND pog.dn_employee_id = ' || TO_CHAR(v_employee_id_in) || ' '
END IF;
IF v_group_id_in = get_group_fulfillment_center1
THEN
v_sql_order_by := 'ORDER BY '
|| 'bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ') '
|| ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') '
|| ',po.order_id '
ELSIF v_group_id_in = c_GROUP_FULFILLMENT_CENTER_212
THEN
v_sql_order_by := 'ORDER BY '
|| 'bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
|| ',TRUNC(po.order_date)'
|| ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ')'
|| ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null)'
|| ',po.order_id'
ELSE
v_sql_order_by := 'ORDER BY '
|| ' bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
|| ',chain_codes DESC'
|| ',po.order_id '
END IF;
IF UPPER(v_search_type_in) = 'ORDERNUMBER'
THEN
v_sql_where := v_sql_where || v_group_where
|| 'AND po.order_id = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'STATUSONLY'
THEN
v_sql_where := v_sql_where || v_group_where || v_status_where
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
ELSIF UPPER(v_search_type_in) = 'BILLINGNAME'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.upper_billing_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'GROUP'
THEN
IF v_search_value1_in = '0' -- Unassigned
THEN
v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
v_sql_where := v_sql_where || ' ' || v_status_where
|| 'AND po.order_id = pod.order_id '
|| 'AND pod.group_id IS NULL '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
ELSE
v_sql_where := v_sql_where || ' ' || v_status_where
|| 'AND pog.group_id = :v_group_for_search '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_group_for_search;
END IF;
ELSIF UPPER(v_search_type_in) = 'SHIPPINGNAME'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.upper_shipping_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'USERNAME'
THEN
v_lower_search_value1 := LOWER(v_search_value1_in);
v_sql_from := v_sql_from || ' ,sxweb01.customer c ';
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.customer_id = c.customer_id '
|| 'AND c.username LIKE ''%'' || :v_lower_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_lower_search_value1;
ELSIF UPPER(v_search_type_in) = 'BILLINGADDRESS1'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_from := v_sql_from || ' ,order_payment op '
|| ' ,credit_card cc '
|| ' ,om_address oma '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = op.business_object_id '
|| 'AND op.business_object_type = ''ORDR'' '
|| 'AND op.payment_method = ''CRCD'' '
|| 'AND op.payment_method_id = cc.credit_card_id '
|| 'AND cc.address_id = oma.address_id '
|| 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'SHIPPINGADDRESS1'
THEN
v_upper_search_value1 := UPPER(v_search_value1_in);
v_sql_from := v_sql_from || ' ,shipping_info si '
|| ' ,om_address oma '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = si.order_id '
|| 'AND si.ship_to_address_id = oma.address_id '
|| 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_upper_search_value1;
ELSIF UPPER(v_search_type_in) = 'BILLINGPHONE'
THEN
v_sql_from := v_sql_from || ' ,order_payment op '
|| ' ,credit_card cc '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = op.business_object_id '
|| 'AND op.business_object_type = ''ORDR'' '
|| 'AND op.payment_method = ''CRCD'' '
|| 'AND op.payment_method_id = cc.credit_card_id '
|| 'AND cc.home_phone = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'SHIPPINGPHONE'
THEN
v_sql_from := v_sql_from || ' ,shipping_info si '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = si.order_id '
|| 'AND si.ship_phone = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'CREDITCARDNUMBER'
THEN
v_sql_from := v_sql_from || ' ,order_payment op '
|| ' ,credit_card cc '
v_sql_where := v_sql_where || ' ' || v_group_where
|| 'AND po.order_id = op.business_object_id '
|| 'AND op.business_object_type = ''ORDR'' '
|| 'AND op.payment_method = ''CRCD'' '
|| 'AND op.payment_method_id = cc.credit_card_id '
|| 'AND cc.credit_card_number = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'PONUMBER'
THEN
v_sql_from := ' '
|| 'FROM ' || ' product_order_detail_actv_v pod ,' || SUBSTR(v_sql_from, 6);
v_sql_where := v_sql_where || v_group_where
|| 'AND pod.po_no = :v_search_value1_in '
|| 'AND po.order_id = pod.order_id '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT /*+ ORDERED */ DISTINCT ' || v_sql_select || v_sql_from || v_sql_where
USING v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'USERASSIGNED'
THEN
IF v_search_value1_in = '0' -- 'Unassigned'
THEN
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND pog.unassigned_flag = ''Y'' '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
ELSE
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND pog.dn_employee_id = :v_search_value1_in '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
END IF;
ELSIF UPPER(v_search_type_in) = 'ORDERDATE'
THEN
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND po.order_date BETWEEN TO_DATE( :v_search_value1_in, ''MON DD YYYY HH24:MI:SS'') AND TO_DATE( :v_search_value2_in ,''MON DD YYYY HH24:MI:SS'') '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in, v_search_value2_in;
ELSIF UPPER(v_search_type_in) = 'PRINTJOB'
THEN
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select
|| ' '
|| 'FROM product_order po '
|| ' ,bcf_print_jobs pj '
|| 'WHERE 1 = 1 '
|| 'AND pj.order_id = po.order_id '
|| 'AND pj.username = :v_search_value1_in '
|| 'AND pj.create_date = TO_DATE( :v_search_value2_in,''MON DD YYYY HH24:MI:SS'') '
|| v_sql_order_by
USING v_search_value1_in, v_search_value2_in;
ELSIF UPPER(v_search_type_in) = 'FULFILLMENTTYPE'
THEN
v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
|| 'AND pod.order_id = po.order_id '
-- Because we have to drill down the the POD level
-- we now need to ensure the pod records match on group
|| 'AND pod.group_id = ' || v_group_for_search
-- We curr. have 3 fufillment types FC165, FC212, FCBABY
-- so substr on 1st char works for Garry
|| 'AND UPPER(SUBSTR(pod.fulfillment_type,1,1)) = UPPER(:v_search_value1_in) '
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
ELSIF UPPER(v_search_type_in) = 'INSUFFICIENTAUTHORIZATION'
THEN
v_sql_where := v_sql_where
|| 'AND bcf_get_authorization_amount(po.order_id) > 0'
IF (outcursor%ISOPEN) THEN
CLOSE outcursor;
END IF;
OPEN outcursor FOR
'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by;
END IF;
END order_search;
-- -
SQL query using Group by and Aggregate function
Hi All,
I need your help in writing an SQL query to achieve the following.
Scenario:
I have table with 3 Columns. There are 3 possible values for col3 - Success, Failure & Error.
Now I need a query which can give me the summary counts for distinct values of col3 for each GROUP BY of col1 and col2 values. When there are no values for col3 then it should return ZERO count.
Example Data:
Col1 Col2 Col3
abc 01 success
abc 02 success
abc 01 success
abc 01 Failure
abc 01 Error
abc 02 Failure
abc 03 Error
xyz 07 Failure
Required Output:
c1 c2 s_cnt F_cnt E_cnt (Heading)
abc 01 2 1 1
abc 02 1 1 0
abc 03 0 0 1
xyz 07 0 1 0
s_cnt = Success count; F_cnt = Failure count; E_cnt = Error count
Please note that the output should have 5 columns with col1, col2, group by (col1,col2)count(success), group by (col1,col2)count(failure), group by (col1,col2)count(error)
and where ever there are NO ROWS then it should return ZERO.
Thanks in advance.
Regards,
ShivaHi,
user13015050 wrote:
Thanks TTT. Unfortunately I cannot use this solution because I have huge data for this.T's solution is basically the same as mine. The first 23 lines just simulates your table. Since you actually have a table, you would start with T's line 24:
SELECT col1 c1, col2 c2, SUM(decode(col3, 'success', 1, 0)) s_cnt, ...
user13015050 wrote:Thanks a lot Frank. It helped me out. I just did some changes to this as below and have no issues.
SELECT col1
, col2
, COUNT ( CASE
WHEN col3 = 'SUCCESS'
THEN 1
END
) AS s_cnt
, COUNT ( CASE
WHEN col3 = 'FAILED'
THEN 1
END
) AS f_cnt
, COUNT ( CASE
WHEN col3 = 'ERROR'
THEN 1
END
) AS e_cnt
FROM t1
WHERE c2 in ('PURCHASE','REFUND')
and c4 between to_date('20091031000000','YYYYMMDDHH24MISS') AND to_date('20100131235959','YYYYMMDDHH24MISS')
GROUP BY c1, c2
ORDER BY c1, c2;
Please let me know if you see any issues in this query.It's very hard to read.
This site normally compresses spaces. Whenever you post formatted text (such as queries or results) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
Also, post exactly what you're using. The code above is SELECTing col1 and col2, but there's no mention of either in the GROUP BY clause, so I don't believe it's really what you're using.
Other than that, I don't see anything wrong or suspicious in the query. -
I'm using the LabView Database Connectivity Toolset and am using the following query...
UPDATE IndexStation
SET Signal_Size=200
WHERE 'StartTime=12:05:23'
Now the problem is that this command seems to update all rows in the table IndexStation... Not just specifically the row where StartTime=12:05:23
I have tries all sorts of {} [] / ' " around certain characters and column names but it always seems to update all rows...
I've begun to use the SQL query tab in Access to try and narrow down as to why this happens, but no luck!
Any ideas!?
Thanks,
Chris.Chris Walter wrote:
I completely agree about the Microsoft issue.
But it seems no SQL based manual states that { } will provide a Date/Time constant.
Is this an NI only implementation? Because I can't seem to get it to function correctly within LabView or in any SQL query.
Chris.
There is nothing about the database toolkit in terms of SQL syntax that would be NI specific. The database Toolkit simply interfaces to MS ADO/DAO and the actual SQL syntax is usually implemented in the database driver or database itself although I wouldn't be surprised if ADO/DAO does at times munch a bit with that too.
The Database Toolkit definitely does not. So this might be a documentation error indeed. My understanding of SQL syntax is in fact rather limited so not sure which databases might use what delimiters to format date/time values. I know that SQL Server is rather tricky thanks to MS catering for the local date/time format in all their tools and the so called universal date/time format has borked on me on several occasions.
Rolf Kalbermatter
Rolf Kalbermatter
CIT Engineering Netherlands
a division of Test & Measurement Solutions -
Parameter passing to custom SQL query using PL/SQL FUNCTION
Hi
In order to pass a parameter to the query in custom folder of a business area I created a function and mapped it to the Custome query using Discoverer Desktop. There is no error in mapping as the system does not throw any error. When I am inputting the Parameter for the input values everytime the query doesnot return any rows.
Can anybody help in this regardHi,
I need to take the request Id as input from the user and then fetch only the data pertaining to that requet Id. As a lot of complex joins are involved I need to pass request id as parameter to the custome folder.
The package i greated:
CREATE OR REPLACE PACKAGE SETPARAM
AS
param1 varchar2(25);
param2 varchar2(25);
FUNCTION SET_PARAM1(p1 IN varchar2) RETURN NUMBER ;
FUNCTION GET_PARAM1 RETURN varchar2;
END SETPARAM;
CREATE OR REPLACE PACKAGE BODY SETPARAM AS
FUNCTION SET_PARAM1(p1 IN varchar2) RETURN NUMBER IS
BEGIN
fnd_client_info.set_org_context('138');
param1 := p1;
dbms_output.put_line(param1);
RETURN 1;
END;
FUNCTION GET_PARAM1 RETURN varchar2 AS
BEGIN
RETURN param1;
END;
END SETPARAM;
I registered the set_param1 function as a pl/sql function in discoverer admin.
This function is called on the condition associated with the parameter in Discoverer Desktop when i run the report.
In the custom folder query i have this piece in the where clause
WHERE tnfo.request_id = NVL(APPS.SETPARAM.GET_PARAM1,7383588)
And everytime i get the data pertaining to request id =7383588,
Please suggest where i went wrong
thanks
Ashwini -
APEX 'crawling' whilst executing a query using CONNECT BY
Hi Folks.
Anyone seen this behaviour before?
I can run this SQL (with the appropriate page item values substitued) in PL/SQL developer and I get the result in less than 0.094 seconds with a COST of 13
SELECT NULL LINK
,a.data_points
,5000 forecast_value
FROM
(SELECT to_char(trunc(add_months((SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
,LEVEL - 1)
,'mm')
,'MON-RR') data_points
FROM dual
CONNECT BY LEVEL <=
trunc(months_between((SELECT iag.agreement_end_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
,(SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)))
MINUS
SELECT pkg_ngc_utilities.get_long_traffic_month(imps.traffic_period_id)
FROM iot_mfs_plus_summary imps
WHERE imps.client_master_entity_id = :P317_CLIENT_MASTER_ENTITY_ID
AND imps.home_master_entity_id IN
(SELECT ipy.master_entity_id
FROM iot_party ipy
WHERE ipy.iot_agreement_id = :P317_IOT_REBATE_AGREEMENT_ID
AND ipy.client_side = 0) -- Roaming Partner
AND imps.visited_master_entity_id = imps.client_master_entity_id
AND pkg_ngc_utilities.get_long_traffic_month(imps.traffic_period_id) IN
(SELECT to_char(trunc(add_months((SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
,LEVEL - 1)
,'mm')
,'MON-RR') data_points
FROM dual
CONNECT BY LEVEL <= trunc(months_between ((SELECT iag.agreement_end_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID) ,
(SELECT iag.agreement_start_date
FROM iot_agreement iag
WHERE iag.iot_agreement_id =
:P317_IOT_REBATE_AGREEMENT_ID)
)))) a
ORDER BY to_date(a.data_points, 'MON-RR')If I use the same query in a Flash Chart the page still only takes a few seconds to display but the chart takes over a minute to display. APEX ADMIN shows the page being served in 2 - 3 seconds.
If I drop the chart and drop the query into a simple report region, the entire page takes over a minute to display.
Has anyone experienced anything similar?
This is the first time I have used CONNECT BY LEVEL in my SQL so that is my main suspect. But please remember, in SQL/Developer the query executes very quickly indeed.
Many thanks
Kind regards
Simon GaddHi Simon,
I had something similar. When my code ran is SQL is took seconds, but took minutes in an ApEx page. There seems to the something strange with the way ApEx and/or the optimizer execute code via ApEx pages.
I gave my code to one of my tuning experts and they added a simple rule to force index use:
select /*+RULE index(r AMS_REQUESTS_IDX4) */
r.requester_name,
r.ams_type_code_name,
d.display_date
from
(select TO_DATE(:P23_CALENDAR_DATE,'YYYYMMDD')-100+rownum display_date from all_objects where rownum < 300) d,
ams_requests_v r
where
trunc(d.display_date) between trunc(r.start_date) and trunc(r.end_date)
and r.approval_status != 'R'
and to_char(d.display_date,'DY') NOT IN ('SAT','SUN')
and d.display_date not in (select trunc(closure_date) from ams_closures)
and (nvl(r.restricted_view_flag,'N') = 'N' or trunc(r.end_date) >= trunc(sysdate))
and r.department_id in ( select
d.department_id
from
ams_departments d
connect by ( PRIOR DEPARTMENT_ID = PARENT_DEPARTMENT_ID)
start with department_id = (
select
department_id
from
ams_departments
where
nvl(terminating_dept_flag,'N') = 'Y'
and level = (select
min(level)
from
ams_departments
where
nvl(terminating_dept_flag,'N') = 'Y'
connect by department_id = prior parent_department_id
start with department_id = :P0_DEPARTMENT_ID)
connect by department_id = prior parent_department_id
start with department_id = :P0_DEPARTMENT_ID))
UNION
select /*+RULE index(r AMS_REQUESTS_IDX4) */
r.requester_name,
r.ams_type_code_name,
d.display_date
from
(select TO_DATE(:P23_CALENDAR_DATE,'YYYYMMDD')-100+rownum display_date from all_objects where rownum < 300) d,
ams_requests_v r
where
trunc(d.display_date) between trunc(r.start_date) and trunc(r.end_date)
and r.approval_status != 'R'
and to_char(d.display_date,'DY') NOT IN ('SAT','SUN')
and d.display_date not in (select trunc(closure_date) from ams_closures)
and nvl(r.restricted_view_flag,'N') = 'Y'
and r.department_id in ( select distinct
d.department_id
from
ams_departments d
connect by ( PRIOR DEPARTMENT_ID = PARENT_DEPARTMENT_ID)
start with department_id in (
select
department_id
from
ams_departments_v
where
:P0_PERSON_ID in (manager_id, deputy_id,administrator_id)))
UNION
select
NULL requester_name,
t.name ams_type_code_name,
trunc(c.closure_date) display_date
from
ams_closures c,
ams_closure_type_codes_v t
where
c.AMS_CLOSURE_TYPE_CODE = t.AMS_CLOSURE_TYPE_CODE
order by 3,1It might be worth you trying something similar.
best regards,
Martin -
Optimizing an SQL Query using Oracle SQL Developer
Hi ,
Currently i am using Oracle SQL Developer as my Database IDE .
Is it possible to use Orqcles SQLDeveloper for the purpose of Optimizing an SQL Query ??
For example assume i am having a query as :
Select from Tranac_Master where CUST_STATAUS='Y' and JCC_REPORT='N'*
Could anybody please tell me how can i use Oracle SQL Developer to optimize this query or any other SQL queries ??
Please share your ideas , thanks in advance .1. Your query looks very simplistic as it is, so I fail to see how you can better optimise it (unless 'Tranac_Master' is a view, in which case I'd need to see the view details).
2. No tool can automagically optimise your SQL to any degree of practical use. Very minor adjustments may be possible automatically, but really it is a question of you knowing your data & database design accurately, and then you applying your expert knowledge to tune it. -
Creating sql query using 3 tables
There is database (supposed to be relational but it is not) and
I cannot change the tables and it is very difficult to create SQL query.
Please, help!
First table T1
a1 char 20
time timestamp
a2 char 7 (the same as t2.a2 when is trimmed)
a3 number 5,0
a4 number 8,4
a5 number 7,3
Second table T2
a2 char 15
b1 number 1,0
b2 char 1,0
b3 char 4
a3 number 5,0
Third table T3
b3 char 4
c1 char 4
c2 number 7,3
c3 number 8,4
So, I need to create query (is it possible at all!?) from those 3 tables (a1, a2, b1,b2,b3, a4, a5, c2,c3
where time is within interval (from, to), a3 in interval (1,2,3,4), t2.b3=t3.b3, t1.a2=t2.a2
group by or sorted by a1, then, a2.
Any suggestion is welcome!
Thanks!
By the way, I will use this query in Crystal Reports.As I already mentioned, I received another conditions for the query, and when I create a new one, I
First table T1
a1 number 5,0
a2 char 7 (the same as t2.a2 when is trimmed)
a3 number 8,4
a4 number 7,3
a_time timestamp
Second table T2
a2 char 15
b1 number 1,0
b2 char 1,0
b3 char 4
b4 char 4
Third table T3
c1 char 4
c2 number 7,3
c3 number 8,4
b3 char 4
where if c1='MIN' c2, c3 return min values,
and if c1='MAX' c2,c3 return max values
SQL query:
select t1.a1, t1.a2, t2.b1, t2.b2, t2.b3, t1.a3, t1.a4, t3.c1,
t3.c2, t3.c3, t2.b4
from t1, t2, t3
where (TRIM(t1.a2)=TRIM(t2.a2)
and t1.a1=19
and ((to_char(t1.a_time, 'YYYY-MM-DD') >= '2006-03-15')
and (to_char(t1.a_time, 'YYYY-MM-DD') <= '2006-03-16')))
and t3.b3=t2.b3)
order by t1.a_time
Result set:
a1, a2, b1, b2, b3, a3, a4, c1('MAX'), c2', c3', b4
a1, a2, b1, b2, b3, a3, a4, c1('MIN'), c2", c3", b4
So when I executed SQL query it returns 2 rows for the same a2.
I want to get 1 row for each a2 together with c3 (c4) min and c3 (c4) max values.
How to name columns for c2 min and c2 max (the same for c3) in order to retreive 1 row per a2 value, something like this:
a1, a2, b1, b2, b3, a3, a4, c2min, c2max, c3min, c3max, b4
Thanks -
SQL query using lot of Temp space
I have sql query which is using lot of temp space , please suggest some ways to reduce this
SELECT A.POSITION_NBR, TO_CHAR(B.EFFDT,'YYYY-MM-DD'), rtrim( A.SEQNO), A.EMPLID, B.REG_REGION, A.MANAGER_ID, A.REPORTS_TO, case when A.POSITION_NBR = A.REPORTS_TO THEN 'POS reports to same position' else 'Positions with multiple Emp' End Case
FROM PS_Z_RPTTO_TBL A, PS_POSITION_DATA B, PS_POSTN_SRCH_QRY B1
WHERE B.POSITION_NBR = B1.POSITION_NBR AND B1.OPRID = 'MP9621Q' AND ( A.POSITION_NBR = B.POSITION_NBR AND ( A.REPORTS_TO = A.POSITION_NBR AND B.EFFDT =
(SELECT MAX(B_ED.EFFDT)
FROM PS_POSITION_DATA B_ED
WHERE B.POSITION_NBR = B_ED.POSITION_NBR) AND A.POSITION_NBR <> '00203392') OR ( B.EFFDT =
(SELECT MAX(B_ED.EFFDT)
FROM PS_POSITION_DATA B_ED
WHERE B.POSITION_NBR = B_ED.POSITION_NBR AND B_ED.EFFDT <= SYSDATE) AND B.MAX_HEAD_COUNT <>
(SELECT Count( C.EMPLID)
FROM PS_Z_RPTTO_TBL C)) ) UNION
SELECT F.POSITION_NBR, TO_CHAR(F.EFFDT,'YYYY-MM-DD'), '', '', F.REG_REGION, '', F.REPORTS_TO, ''
FROM PS_POSITION_DATA F, PS_POSTN_SRCH_QRY F1
WHERE F.POSITION_NBR = F1.POSITION_NBR AND F1.OPRID = 'MP9621Q' AND ( F.EFFDT =
(SELECT MAX(F_ED.EFFDT)
FROM PS_POSITION_DATA F_ED
WHERE F.POSITION_NBR = F_ED.POSITION_NBR AND F_ED.EFFDT <= SYSDATE) AND F.EFF_STATUS = 'A' AND F.DEPTID IN
(SELECT G.DEPTID
FROM PS_DEPT_TBL G
WHERE G.EFFDT =
(SELECT MAX(G_ED.EFFDT)
FROM PS_DEPT_TBL G_ED
WHERE G.SETID = G_ED.SETID AND G.DEPTID = G_ED.DEPTID AND G_ED.EFFDT <= SYSDATE) AND F.REG_REGION = G.SETID AND G.EFF_STATUS = 'I') )
Thanks in Advance
Rajanuse {noformat}<your code here>{noformat} tags to format your code.
I have sql query which is using lot of temp space , please suggest some ways to reduce thisIf your sort_area_size is not set sufficient oracle used temp space for sorting operation. As your code is not readable i cant say much more than this. Check with your DBA if you have to increase the temp space. -
SQL Query using bind variables
Hi All,
Can any one please explain me how oracle will parse the below substr query using the bind varaibles, just want to know what oracle does in the backend when processing the below substr query.
SUBSTR(:CDR_RT,3,7)
Regards,
DeeptiAll queries are processed in the same way.
The example I've used on this article: PL/SQL 101 : Cursors and SQL Projection
demonstrates the steps that a query/cursor goes through.
If you're wondering if the SUBSTR in your query is applied to the value being bound in, _before_ the query is executed, then you are mistaken, the value is bound in as it is, and the substr takes place as part of the execution of the query.
Maybe you are looking for
-
i updated my ipod ,but before it work completely the screen turn blue,then white and now it doesnt work even if it charge. So, what do i do? PLEASE ANSWER MY QUESTION PLEASE TELL ME WHAT TO DO so i todd what others did then i did it but it kept showi
-
Is QuickTime Pro what I need?
Hi. I just received a Kodak zi8 video camera for Christmas that will record in HD but the extension is .mov . I would like to put my videos on DVD to give my family and I am finding out it is hard to burn .mov . Will purchasing QTPro let me be able t
-
Safari 5.0 is a disaster
How do I switch back to Safari 4.0 from 5.0? Since I downloaded 5.0 yesterday, downloads have slowed sometimes to a complete halt, the computer has frozen, the loading indicator continues to move and other annoying activity has taken place. I now hav
-
HT201401 i have no edit button in my contacts
i noticed a while ago i no longer have an edit button in my contacts so i cannot edit any contacts i already have but i am also unable to too any new contacts without having to dial the number first and then adding it that way. Any help would be grea
-
[Solved] Can't compile lastfm-client
Hi! I'm having some problems compiling lastfm-client from AUR. Long story short, I'll just post the output of makepkg g++ -Wl,-rpath,/usr/lib -shared -Wl,-soname,libMoose.so.1 -o libMoose.so.1.0.0 ../../build/Moose/release/confirmdialog.o ../../build