Complex Query using findUsers()
I have not run across any posting where the findUsers() or findUsersFiltered() methods were doing anything other than a simple query. Is it possible to do a complex query? For instance, find all users whose status is NOT Deleted. Or all users with a date attribute that is before a given date. Or all users with an attribute value of 'X' OR 'Y'. These are simple SQL queries. I can see how to do a series of simple queries and deal with merging or filtering the results. But wouldn't it be way more efficient to do it in the original query?
If it is possible, can you point me to an example?
Thanks,
Bill
Hi zhangbin,
This message may be useful.
Re: How can I make queries to DB?
--mark
Similar Messages
-
Complex Query using DPL ?
how can i do a complex Query using DPL ?
i.e.
select * from employee where age>25 and managerId=3 and salary>=6000 and salary <= 10000 order by id desc
thx in advace!!
zhangbinHi zhangbin,
This message may be useful.
Re: How can I make queries to DB?
--mark -
Complex Query using ViewCriteria tag??
Hi
I would like to know whether a query with combination of "And" and "Or" conditions can be achieved using ViewCriteria, CriteriaRow and Criteria tags.
For eg: the SQL query should look like this
select * from <table name>
where column1 = x
and (column2=y or column3=z);
I tried couple of combinations using the tags but could get either "AND" or "OR" but not combination of both.
Any suggestions would help.
ThanksWith the built in ViewCriteria implementation you cannot do and/or as per your example. But an equivalent query:
where (column1 = x and column2 = y) or (column1 = x and column3 = z)
is doable. -
Complex query using 'sql like' statement.
Given the following basic class structure.
public class Project {
private String name;
private Architecture architecture;
private Resources resources;
public Architecture getArchitecture();
public void setArchitecture(Architecture architecture);
public Resources getResources();
public void setResources();
public String getName();
public void setName(String name);
public Architecture {
private String name;
public String getName();
public void setName(String name);
public Resources {
private String name;
public String getName();
public void setName(String name);
we want to be able to do a query like:
find all Projects where (Project.name.indexOf("a") > 0) &&
(Project.architecture.name.indexof("b") > 0)
In essence a "like" comparison across String properties of the JDO objects.
Is it supported in KODO?? Anyone tried it out? Also, is it possible to
generate SQL type query in KODO?
rgds
NKNote that this relies on a current bug in Kodo -- we should escape out the
'%' character, but we don't.
Some day, we'll fix that bug. But, before then, we will introduce a proper,
supported mechanism for finding records that have a string field that
contains some substring.
-Patrick
On 5/30/02 6:12 PM, "Andrew" <[email protected]> wrote:
Hi,
I wanted to perform a LIKE statement to get records where the partial string
existed in a field -> eg. select a where a.field LIKE "SPRING" - 2 records,
SpringSteen & MindSprings
I did it like this:
setFieldName("Spring");
String sFilter = "field.startsWith(%\" + getFieldName() + \"%)"; -
Extent anExtent = pm.getExtent(this.getClass(), true);
Query aQuery = pm.newQuery(this.getClass(), anExtent, sFilter);
return (Collection) aQuery.execute();
Hope it helps. oh, don't know about the generating sql query.
Nitin Kanani wrote:
Given the following basic class structure.
public class Project {
private String name;
private Architecture architecture;
private Resources resources;
public Architecture getArchitecture();
public void setArchitecture(Architecture architecture);
public Resources getResources();
public void setResources();
public String getName();
public void setName(String name);
public Architecture {
private String name;
public String getName();
public void setName(String name);
public Resources {
private String name;
public String getName();
public void setName(String name);
we want to be able to do a query like:
find all Projects where (Project.name.indexOf("a") > 0) &&
(Project.architecture.name.indexof("b") > 0)
In essence a "like" comparison across String properties of the JDO
objects.
Is it supported in KODO?? Anyone tried it out? Also, is it possible to
generate SQL type query in KODO?
rgds
NK
Patrick Linskey [email protected]
SolarMetric Inc. http://www.solarmetric.com -
1. How to create an explain plan with rowsource statistics for a complex query that include multiple table joins ?
When multiple tables are involved , and the actual number of rows returned is more than what the explain plan tells. How can I find out what change is needed in the stat plan ?
2. Does rowsource statistics gives some kind of understanding of Extended stats ?You can get Row Source Statistics only *after* the SQL has been executed. An Explain Plan midway cannot give you row source statistics.
To get row source statistics either set STATISTICS_LEVEL='ALL' in the session that executes theSQL OR use the Hint "gather_plan_statistics" in the SQL being executed.
Then use dbms_xplan.display_cursor
Hemant K Chitale -
How to bind local variables in complex query in PL/SQL
Hi
I have long complex query with local varibales; when I run in Dynamic SQL I get an error. Not sure which is the easiest way to bind local variables.
L_QUERY := q'[select m.segment1 col1,
'1' col2,
'13' col3,
l.operand col4,
0 col5,
'Y' col8,
'N' col9,
'N' col10,
decode(h.name,'09_UKOR_*','EOS Credit','10_UKOR_*','TopUp','11_UKOR_*','Main') col18
from qp_list_headers_v h,
qp_list_lines_v l,
mtl_system_items m
where h.name in ('09_UKOR_*','10_UKOR_*','11_UKOR_*')
and h.list_header_id = l.list_header_id
and m.inventory_item_id = l.product_attr_value
and m.organization_id = 84
UNION all
SELECT qs.PRODUCT_ATTR_VALUE col1,
hca.account_number col2,
decode (su.site_use_code,'BILL_TO','21','SHIP_TO','18') col3,
0 col4,
qs.operand col5,
null col6,
null col7,
'N' col8,
'Y' col9,
'N' col10,
--Local variables
(case when (trunc(nvl(qq.start_date_active,sysdate)) between l_cur_year_from and l_cur_year_to) and
( trunc(nvl(qq.end_date_active,l_cur_year_to)) >= trunc(l_cur_year_to) ) then 'TopUp'
when (trunc(qq.start_date_active) between l_next_year_from and l_next_year_to) and
( trunc(nvl(qq.end_date_active,l_next_year_to)) >= l_next_year_to) then 'Main'
when (trunc(qq.start_date_active) between l_last_year_from and l_last_year_to) and
( trunc(nvl(qq.end_date_active,l_last_year_to)) >= l_last_year_to ) then 'EOS Return'
end )col18,
--Local variables
(case when (trunc(qq.start_date_active) between l_next_year_from and l_next_year_to) and
( trunc(nvl(qq.end_date_active,l_next_year_to)) >= l_cur_year_to ) then 'N'
else
'Y' end ) col21
FROM qp.qp_list_headers_b qlhb,
qp.qp_list_headers_tl qlht,
qp.qp_qualifiers qq,
qp_modifier_summary_v qs,
hz_cust_site_uses_all su,
hz_cust_acct_sites_all sa,
hz_cust_accounts hca
WHERE 1=1
and qlhb.LIST_TYPE_CODE = 'DLT'
and nvl(qlhb.active_flag,'Y') = 'Y'
and qlhb.list_header_id = qlht.list_header_id
AND qq.list_header_id = qlhb.list_header_id
and qq.list_type_code = qlhb.LIST_TYPE_CODE
and qq.QUALIFIER_CONTEXT = 'CUSTOMER'
and qs.list_header_id = qlhb.list_header_id
and qs.LIST_LINE_TYPE_CODE = 'DIS'
and su.site_use_id = qq.qualifier_attr_value --1064
AND su.cust_acct_site_id = sa.cust_acct_site_id
AND sa.cust_account_id = hca.cust_account_id ]';
--call Dynamic SQL function
l_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(l_cursor, p_QUERY , DBMS_SQL.NATIVE);
l_rows := DBMS_SQL.EXECUTE(l_cursor);
Any help will be much appreciated.
iI guess I should use DBMS_SQL.BIND_VARIABLE to bind all variables seperately so not to mess with query1) What is the error you get?
2) If you use bind variables inside a query, you should prefix them with colons, so use :l_cur_year_from etc.
3) Why are you using dbms_sql instead of native dynamic sql and/or ref-cursors?
Toon -
Trying to form complex query - need help
I have a fairly complex query that I need to join the results of to show actual and goal by day. The actuals are an aggregation of records that get put in every day, while the targets are a single entry in range format indicating an active range for which the target applies. I'm working on a query that will put things together by month and I'm running into a snag. Can someone please point out where appropriate naming needs to go to get this to come together?
This one works:
(select DATE_INDEX, SUM(LDS) as TTLLDS, SUM(TONS) as TTLTONS from
(select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
(select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
ELSE SUM(w.SPOT_WEIGHT)
END as WT
from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
group by c.DATE_INDEX, c.VEH_LOC
union
select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
group by c.DATE_INDEX, c.VEH_LOC)
group by DATE_INDEX, VEH_LOC)
group by DATE_INDEX)Now I need to add in the following query:
select (u.MACH_TPH_D+u.MACH_TPH_N)/2 as MTPH_TGT, (u.LABOR_TPH_D+u.LABOR_TPH_N)/2 as LTPH_TGT
from UTIL_TARGET_LOADERS u
where u.ORG_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)The join needs to be based on VEH_LOC and DAY in the form:
... WHERE u.ORG_ID = x.VEH_LOC
AND x.DATE_INDEX between u.START_DATE and NVL(u.END_DATE,sysdate)I had one that worked just fine when only one entity was involved; the complication arises in that this is a division-level report so I have to individually resolve the subordinates and their goals before I can aggregate. This is one of two queries I need to tie together using a WITH clause so I can pivot the whole thing and present it in month-by-month fashion. When I try to tie it together like the query below, I get: invalid relational operator.
select ttls.DATE_INDEX, SUM(ttls.LDS) as TTLLDS, SUM(ttls.TONS) as TTLTONS, u.TARGET_LTPH, u.TARGET_MTPH
from UTIL_TARGET_LOADERS u,
(select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
(select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
ELSE SUM(w.SPOT_WEIGHT)
END as WT
from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
group by c.DATE_INDEX, c.VEH_LOC
union
select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
group by c.DATE_INDEX, c.VEH_LOC)
group by DATE_INDEX, VEH_LOC) ttls
where ttls.DATE_INDEX beween u.START_DATE and NVL(u.END_DATE,sysdate)
and ttls.VEH_LOC = u.ORG_ID
group by ttls.DATE_INDEXI know this is a nested mess, as it has to grab the production from two tables for a range of VEH_LOC values and sum and aggregate by day and VEH_LOC, then I have to try and match that to the targets based on VEH_LOC and day. My final query is to aggregate the whole mess of sums and averages by month.
I'd appreciate it if someone can point me in the right direction.Figured it out.
select ttl.DATE_INDEX, SUM(ttl.LDS) as TTLLDS, SUM(ttl.TONS) as TTLTONS,
AVG((u.MACH_TPH_D+u.MACH_TPH_N)/2) as MTPH_TGT,
AVG((u.LABOR_TPH_D+u.LABOR_TPH_N)/2) as LTPH_TGT
from
(select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
(select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
ELSE SUM(w.SPOT_WEIGHT)
END as WT
from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
group by c.DATE_INDEX, c.VEH_LOC
union
select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
group by c.DATE_INDEX, c.VEH_LOC)
group by DATE_INDEX, VEH_LOC) ttl, UTIL_TARGET_LOADERS u
where u.ORG_ID = ttl.VEH_LOC
and ttl.DATE_INDEX between u.START_DATE and NVL(U.END_DATE,sysdate)
group by ttl.DATE_INDEX, (u.LABOR_TPH_D+u.LABOR_TPH_N)/2 -
Complex Query in toplink descriptor
I have a complex query with "Order by" and "Max" aggregation function and I am trying to convert it to a named query. I am using toplink descriptor in Jdeveloper 11G.
1. Is it possible to write a named query which returns values from more than two different table?
2. how to specify Max operation in the Toplink descriptor.
Here is my Query
select * from table1 t1 ,table2 t2
where
t1.id = t2.id and
t1.name ='Name' and
t1.status<>'DELETE' and
t2.a_id = (select max(a_id) from table2 t21 where
t21.a_id = t1.id and
group by id)TopLink supports sub-selects through sub-queries in TopLink Expressions, and through JPQL. I'm not sure on the JDev support though, you may need to use a code customizer or amendment to add the named query.
James : http://www.eclipselink.org -
Complex Query which needs tuning
Hello :
I have a complex query that needs to be tuned. I have little experience in tuning the sql and hence taking the help of your guys.
The Query is as given below:
Database version 11g
SELECT DISTINCT P.RESPONSIBILITY, P.PRODUCT_MAJOR, P.PRODUCT_MINOR,
P.PRODUCT_SERIES, P.PRODUCT_CATEGORY AS Category1, SO.REGION_CODE,
SO.STORE_CODE, S.Store_Name, SOL.PRODUCT_CODE, PRI.REPLENISHMENT_TYPE,
PRI.SUPPLIER_CODE,
SOL.SOLD_WITH_NIC, SOL.SUGGESTED_PRICE,
PRI.INVOICE_COST, SOL.FIFO_COST,
SO.ORDER_TYPE_CODE, SOL.DOCUMENT_NUM,
SOS.SLSP_CD, '' AS FNAME, '' AS LNAME,
SOL.PRICE_EXCEPTION_CODE, SOL.AS_IS,
SOL.STATUS_DATE,
Sum(SOL.QUANTITY) AS SumOfQUANTITY,
Sum(SOL.EXTENDED_PRICE) AS SumOfEXTENDED_PRICE
--Format([SALES_ORDER].[STATUS_DATE],"mmm-yy") AS [Month]
FROM PRODUCT P,
PRODUCT_MAJORS PM,
SALES_ORDER_LINE SOL,
STORE S,
SALES_ORDER SO,
SALES_ORDER_SPLITS SOS,
PRODUCT_REGIONAL_INFO PRI,
REGION_MAP R
WHERE P.product_major = PM.PRODUCT_MAJOR
and SOL.PRODUCT_CODE = P.PRODUCT_CODE
and SO.STORE_CODE = S.STORE_CODE
AND SO.REGION_CODE = S.REGION_CODE
AND SOL.REGION_CODE = SO.REGION_CODE
AND SOL.DOCUMENT_NUM = SO.DOCUMENT_NUM
AND SOL.DELIVERY_SEQUENCE_NUM = SO.DELIVERY_SEQUENCE_NUM
AND SOL.STATUS_CODE = SO.STATUS_CODE
AND SOL.STATUS_DATE = SO.STATUS_DATE
AND SO.REGION_CODE = SOS.REGION_CODE
AND SO.DOCUMENT_NUM = SOS.DOCUMENT_NUM
AND SOL.PRODUCT_CODE = PRI.PRODUCT_CODE
AND PRI.REGION_CODE = R.CORP_REGION_CODE
AND SO.REGION_CODE = R.DS_REGION_CODE
AND P.PRODUCT_MAJOR In ('STEREO','TELEVISION','VIDEO')
AND SOL.STATUS_CODE = 'D'
AND SOL.STATUS_DATE BETWEEN '01-JUN-09' AND '30-JUN-09'
AND SO.STORE_CODE NOT IN
('10','20','30','40','70','91','95','93','94','96','97','98','99',
'9V','9W','9X','9Y','9Z','8Z',
'8Y','92','CZ','FR','FS','FT','FZ','FY','FX','FW','FV','GZ','GY','GU','GW','GV','GX')
GROUP BY
P.RESPONSIBILITY, P.PRODUCT_MAJOR, P.PRODUCT_MINOR, P.PRODUCT_SERIES, P.PRODUCT_CATEGORY,
SO.REGION_CODE, SO.STORE_CODE, /*S.Short Name, */
S.Store_Name, SOL.PRODUCT_CODE,
PRI.REPLENISHMENT_TYPE, PRI.SUPPLIER_CODE,
SOL.SOLD_WITH_NIC, SOL.SUGGESTED_PRICE, PRI.INVOICE_COST,
SOL.FIFO_COST, SO.ORDER_TYPE_CODE, SOL.DOCUMENT_NUM,
SOS.SLSP_CD, '', '', SOL.PRICE_EXCEPTION_CODE,
SOL.AS_IS, SOL.STATUS_DATE
Explain Plan:
SELECT STATEMENT, GOAL = ALL_ROWS Cost=583 Cardinality=1 Bytes=253
HASH GROUP BY Cost=583 Cardinality=1 Bytes=253
FILTER
NESTED LOOPS Cost=583 Cardinality=1 Bytes=253
HASH JOIN OUTER Cost=582 Cardinality=1 Bytes=234
NESTED LOOPS
NESTED LOOPS Cost=571 Cardinality=1 Bytes=229
NESTED LOOPS Cost=571 Cardinality=1 Bytes=207
NESTED LOOPS Cost=569 Cardinality=2 Bytes=368
NESTED LOOPS Cost=568 Cardinality=2 Bytes=360
NESTED LOOPS Cost=556 Cardinality=3 Bytes=435
NESTED LOOPS Cost=178 Cardinality=4 Bytes=336
NESTED LOOPS Cost=7 Cardinality=1 Bytes=49
HASH JOIN Cost=7 Cardinality=1 Bytes=39
VIEW Object owner=CORP Object name=index$_join$_015 Cost=2 Cardinality=3 Bytes=57
HASH JOIN
INLIST ITERATOR
INDEX UNIQUE SCAN Object owner=CORP Object name=PRODMJR_PK Cost=0 Cardinality=3 Bytes=57
INDEX FAST FULL SCAN Object owner=CORP Object name=PRDMJR_PR_FK_I Cost=1 Cardinality=3 Bytes=57
VIEW Object owner=CORP Object name=index$_join$_016 Cost=4 Cardinality=37 Bytes=740
HASH JOIN
INLIST ITERATOR
INDEX RANGE SCAN Object owner=CORP Object name=PRDMNR1 Cost=3 Cardinality=37 Bytes=740
INDEX FAST FULL SCAN Object owner=CORP Object name=PRDMNR_PK Cost=4 Cardinality=37 Bytes=740
INDEX UNIQUE SCAN Object owner=CORP Object name=PRODMJR_PK Cost=0 Cardinality=1 Bytes=10
MAT_VIEW ACCESS BY INDEX ROWID Object owner=CORP Object name=PRODUCTS Cost=171 Cardinality=480 Bytes=16800
INDEX RANGE SCAN Object owner=CORP Object name=PRD2 Cost=3 Cardinality=681
TABLE ACCESS BY INDEX ROWID Object owner=DS Object name=SALES_ORDER_LINE Cost=556 Cardinality=1 Bytes=145
BITMAP CONVERSION TO ROWIDS
BITMAP INDEX SINGLE VALUE Object owner=DS Object name=SOL2
TABLE ACCESS BY INDEX ROWID Object owner=DS Object name=SALES_ORDER Cost=4 Cardinality=1 Bytes=35
INDEX RANGE SCAN Object owner=DS Object name=SO1 Cost=3 Cardinality=1
TABLE ACCESS BY INDEX ROWID Object owner=DS Object name=REGION_MAP Cost=1 Cardinality=1 Bytes=4
INDEX RANGE SCAN Object owner=DS Object name=REGCD Cost=0 Cardinality=1
MAT_VIEW ACCESS BY INDEX ROWID Object owner=CORP Object name=PRODUCT_REGIONAL_INFO Cost=2 Cardinality=1 Bytes=23
INDEX UNIQUE SCAN Object owner=CORP Object name=PRDRI_PK Cost=1 Cardinality=1
INDEX UNIQUE SCAN Object owner=CORP Object name=BI_STORE_INFO_PK Cost=0 Cardinality=1
MAT_VIEW ACCESS BY INDEX ROWID Object owner=CORP Object name=BI_STORE_INFO Cost=1 Cardinality=1 Bytes=22
VIEW Object owner=DS cost=11 Cardinality=342 Bytes=1710
HASH JOIN Cost=11 Cardinality=342 Bytes=7866
MAT_VIEW ACCESS FULL Object owner=CORP Object name=STORE_CORP Cost=5 Cardinality=429 Bytes=3003
NESTED LOOPS Cost=5 Cardinality=478 Bytes=7648
MAT_VIEW ACCESS FULL Object owner=CORP Object name=STORE_GROUP Cost=5 Cardinality=478 Bytes=5258
INDEX UNIQUE SCAN Object owner=CORP Object name=STORE_REGIONAL_INFO_PK Cost=0 Cardinality=1 Bytes=5
INDEX RANGE SCAN Object owner=DS Object name=SOS_PK Cost=2 Cardinality=1 Bytes=19
Regards,
BMPFirst thing that i notice in this query is you are Using Distinct as well as Group by.
Your group by will always give you distinct results ,then again why do you need the Distinct?
For example
WITH t AS
(SELECT 'clm1' col1, 'contract1' col2,10 value
FROM DUAL
UNION ALL
SELECT 'clm1' , 'contract1' ,10 value
FROM DUAL
UNION ALL
SELECT 'clm1', 'contract2',10
FROM DUAL
UNION ALL
SELECT 'clm2', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm3', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm4', 'contract2',10
FROM DUAL)
SELECT distinct col1,col2,sum(value) from t
group by col1,col2Is always same as
WITH t AS
(SELECT 'clm1' col1, 'contract1' col2,10 value
FROM DUAL
UNION ALL
SELECT 'clm1' , 'contract1' ,10 value
FROM DUAL
UNION ALL
SELECT 'clm1', 'contract2',10
FROM DUAL
UNION ALL
SELECT 'clm2', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm3', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm4', 'contract2',10
FROM DUAL)
SELECT col1,col2,sum(value) from t
group by col1,col2And also
AND SOL.STATUS_DATE BETWEEN '01-JUN-09' AND '30-JUN-09'It would be best to use a to_date when hard coding your dates.
Edited by: user5495111 on Aug 6, 2009 1:32 PM -
Need SQL query using View - Please help
Hi,
I have similar requirement like below.
I have two tables DEPT and EMP and some departments may not have employees. I have created below view, which displays all DEPT records, even though there are no emplyees.
CREATE OR REPLACE VIEW dept_emp_vw AS
SELECT deptno, empid, 0 AS selected
FROM dept d, emp e
WHERE d.deptno = e.deptnno (+);
Ex.
DEPTNO EMPID SELECTED
10 101 0
10 102 0
20 103 0
30 103 0
40 104 0
50 <null> 0
Application will pass "empid" to the view (for ex. empid = 103) and I want result like below.
Ex.
DEPTNO EMPID SELECTED
10 101 0
10 102 0
20 103 1
30 103 1
40 104 0
50 <null> 0
Can you please let me know the query using "dept_emp_vw" view. We have Oracle 11g Release 2.
Thanks a lot for the help.Not possible using normal SQL - as SQL is not a procedure language and does not support variable declaration and use (e.g. passing empid as a variable and using it both as a predicate and as a condition in the SQL projection).
That said - SQL can be "+parameterised+". An approach that is ugly and contrary to the basic design and use of SQL. But it can support the (very weird) view definition of yours.
E.g.
SQL> create or replace procedure SetVariable( name varchar2, value varchar2 ) is
2 begin
3 DBMS_SESSION.set_context( 'MyVariables', name, value );
4 end;
5 /
Procedure created.
SQL>
SQL>
SQL> create or replace context MyVariables using SetVariable;
Context created.
SQL>
SQL> create or replace view my_funky_weird_view as
2 select
3 e.empno,
4 e.ename,
5 e.job,
6 case e.empno
7 when to_number(sys_context( 'MyVariables', 'empid' )) then
8 0
9 else
10 1
11 end as "SELECTED"
12 from emp e
13 /
View created.
SQL>
SQL> exec SetVariable( 'empid', 7499 )
PL/SQL procedure successfully completed.
SQL>
SQL> select * from session_context where namespace = 'MYVARIABLES';
NAMESPACE ATTRIBUTE VALUE
MYVARIABLES EMPID 7499
SQL>
SQL> select * from my_funky_weird_view order by selected;
EMPNO ENAME JOB SELECTED
7499 ALLEN SALESMAN 0
7521 WARD SALESMAN 1
7566 JONES MANAGER 1
7654 MARTIN SALESMAN 1
7698 BLAKE MANAGER 1
7934 MILLER CLERK 1
7788 SCOTT ANALYST 1
7839 KING PRESIDENT 1
7844 TURNER SALESMAN 1
7876 ADAMS CLERK 1
7900 JAMES CLERK 1
7902 FORD ANALYST 1
7369 SMITH CLERK 1
7782 CLARK MANAGER 1
14 rows selected.
SQL>But I will N\OT recommend doing it this way. It is not natural SQL as PL/SQL is needed to "+inject+" name-value pairs into the context for the SQL view to use. It is ugly. It is not standard. It cannot scale. It is complex to use. Etc.
Yes, there are instances when this approach is exactly what one needs - when for example dealing with a trusted context and using the contents for implementing a security layer. But in the above case - I would rather want to see the actual business requirement first, as I think you're barking up the wrong tree with the view solution you have imagined. -
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 -
How to store data from a complex query and only fresh hourly or daily?
We have a report which runs quite slow (1-2 minutes) because the query is quite complicate, so we would like to run this query daily only and store in a table so for those procedures that need to use this complex query as a subquery, can just join to this table directly to get results.
However, I am not sure what kind of object I should use to store data for this complex query. Is data in global temp table only persist within transaction? I need something that can persist the data and be access by procedures.
Any suggestions are welcome,
CheersThank you for your reply. I looked at the materialized view earlier on, but have some difficulties to use it. So I have some questions here:
1.The complex query is not a sum or aggregate functions, it just need to get data from different tables based on different conditions, in this case is it still appropriate to use meterialized view?
2.If it is, I created one, but how to use it in my procedure? From the articles I read, it seems I can't just query from this view directly. So do I need to keep the complex query in my procedure and how the procedure will use the meterialized view instead?
3. I also put the complex query in a normal view, then create a materialized view for this normal view (I expect the data from the complex query will be cache here), then in the procedure I just select * from my_NormalView, but it takes the same time to run even when I set the QUERY_REWRITE_ENABLED to true in the alter session. So I am not sure what else I need to do to make sure the procedure use the materialized view instead of the normal view. Can I query from the Materialized View directly?
Below in the code I copied from one of the article to create the materialized view based on my normal view:
CREATE MATERIALIZED VIEW HK3ControlDB.MW_RIRating
PCTFREE 5 PCTUSED 60
TABLESPACE "USERS"
STORAGE (INITIAL 50K NEXT 50K)
USING INDEX STORAGE (INITIAL 25K NEXT 25K)
REFRESH START WITH ROUND(SYSDATE + 1) + 11/24
NEXT NEXT_DAY(TRUNC(SYSDATE), 'MONDAY') + 15/24
enable query rewrite
AS SELECT * FROM HK3ControlDB.VW_RIRating;
Cheers -
Creating a complex query in CR designer
I'm running Crystal Reports XI Release 2 on Windows NT. I have a very complex query and cannot figure out how to make it work in CR. Basically, I've got a table A with fields containing reference codes that may or may not be present. I created a SQL query that imbeds select statements in the select fields (the field names between SELECT and FROM). So, it would read something like SELECT field1, field2, (select Code_Name from REF_Table where REF_Table.code=TableA.code and TableA.code is not null) FROM TableA. That works great in SQL Developer.
In CR, I tried a command field but that produces a cartesian set where I get a line for every record that has the field for each and every record in TableA. No good. I just want the name for the value in the TableA record translated to the name if it is present. Is there a way to do this in CR? I used to be able to edit the SQL directly in CR but now it won't let me do that. Anyone know a better way to solve this kind of problem?I'm not an Oracle guy so I don't know what you can or can't do in PL SQL.
I'm surprised that you are getting different results between SQL Developer and the CR Command. CR should pass the query, exactly as it's written back to the server, same as SQL Dev.
IN T-SQL...If you want to make sure that the sub-query is returning only 1 row, try placing "TOP 1" behind the SELECT.
(select top 1 loc.location_name
from ref_location loc
where loc.location_num=obd.location_num and odb.location_num is not null)
I'm thinking that Oracle doesn't have the the TOP N option but it does have a rownum feature. So maybe...
(SELECT x.location_name
FROM (
select loc.location_name
from ref_location loc
where loc.location_num=obd.location_num and odb.location_num is not null) x
WHERE rownum = 1
ORDER BY rownum)
This is assuming that Oracle allows you to use ORDER BY in a sub-query. T-SQL only allows it if it's used in conjunction with TOP N...
Just a thought,
Jason -
Is it impossible at all to create a materialized view using a complex query (i.e. joins + union)?
Thanks in advance
FabioOkay, childishness aside, it is possible to create materialized views using joins, unions and sub-queries. However, these sorts of complex materialized views have limited refresh options.
Cheers, APC -
Working with a complex query and trying to get rid of duplicates
I'm working with the following complex query, and I need to
select vin, number, year, make, model, and state from
2007 vehicles in certain models, vin types
2006 vehicles in certain models, vin types
Is there a more efficient way to write this than how I've been writing it?:
select distinct vehicle.vin VIN, unit.STATION_ID STID, unit.EMBEDDED_AREA_CODE||unit.EMBEDDED_PREFIX||lpad(unit.EMBEDDED_RON,4,0)
MIN, unit.AUTHENTICATION_ID AUTHCODE, vehicle.user_veh_desc, veh_model.VEH_MODEL_DESC MODEL, veh_model.VEH_MANUF_YEAR YEAR, acct_veh.STATE STATE
from vehicle
inner join veh_unit on vehicle.vehicle_sak=veh_unit.vehicle_sak
inner join unit on veh_unit.unit_sak=unit.unit_sak
inner join vdu_profile on vdu_profile.vehicle_sak=vehicle.vehicle_sak
inner join vdu_profile_program on vdu_profile_program.VDU_PROFILE_SAK=vdu_profile.VDU_PROFILE_SAK
inner join veh_model on vehicle.VEH_MODEL = veh_model.VEH_MODEL
inner join acct_veh on acct_veh.VEHICLE_SAK = vehicle.VEHICLE_SAK
and vehicle.user_veh_desc like ('%2007%')
AND unit.unit_gen_id >= 36
AND vdu_profile_program.VDU_PROGRAM_SAK = 3
and vdu_profile_program.PREFERENCE_VALUE = 'Y'
and acct_veh.STATE in ('MN','ND','IA')
AND (veh_model.VEH_MODEL_DESC like ('%Vehicle2%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle3%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle5%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle6%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle7%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle8%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle9%'))
and ACCT_VEH.ACCT_VEH_STATUS_ID = 'A'
and (vehicle.vin like ('_______3_________')
or vehicle.vin like ('_______W_________')
or vehicle.vin like ('_______K_________')
or vehicle.vin like ('_______0_________'))
UNION
select distinct vehicle.vin VIN, unit.STATION_ID STID, unit.EMBEDDED_AREA_CODE||unit.EMBEDDED_PREFIX||lpad(unit.EMBEDDED_RON,4,0)
MIN, unit.AUTHENTICATION_ID AUTHCODE, vehicle.user_veh_desc, veh_model.VEH_MODEL_DESC MODEL, veh_model.VEH_MANUF_YEAR YEAR, acct_veh.STATE STATE
from vehicle
inner join veh_unit on vehicle.vehicle_sak=veh_unit.vehicle_sak
inner join unit on veh_unit.unit_sak=unit.unit_sak
inner join vdu_profile on vdu_profile.vehicle_sak=vehicle.vehicle_sak
inner join vdu_profile_program on vdu_profile_program.VDU_PROFILE_SAK=vdu_profile.VDU_PROFILE_SAK
inner join veh_model on vehicle.VEH_MODEL = veh_model.VEH_MODEL
inner join acct_veh on acct_veh.VEHICLE_SAK = vehicle.VEHICLE_SAK
and vehicle.user_veh_desc like ('%2006%')
AND unit.unit_gen_id >= 36
AND vdu_profile_program.VDU_PROGRAM_SAK = 3
and vdu_profile_program.PREFERENCE_VALUE = 'Y'
and acct_veh.STATE in ('MN','ND','IA')
AND (veh_model.VEH_MODEL_DESC like ('%Vehicle1%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle2%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle3%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle4%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle5%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle6%')
OR veh_model.VEH_MODEL_DESC like ('%Vehicle7%'))
and ACCT_VEH.ACCT_VEH_STATUS_ID = 'A'
and (vehicle.vin like ('_______Z_________')
or vehicle.vin like ('_______K_________'))I am not sure how many rows you have in the tables but I am assuming that there would be performance benefits in performing the query only once. You can combine similar coding and use an OR condition for the parts that are different. I left the 'K' vin comparison in both years for readability.
I was not able to fully test my code but I believe that it will work as is. I hope it helps. If you are happier with the UNION you may want to investigate the WITH clause as you can query a subset of data twice, rather than the full data set.
-- Common code
SELECT DISTINCT
vehicle.vin,
unit.station_id stid,
unit.embedded_area_code || unit.embedded_prefix || LPAD (unit.embedded_ron, 4, 0) MIN,
unit.authentication_id authcode,
vehicle.user_veh_desc,
veh_model.veh_model_desc model,
veh_model.veh_manuf_year YEAR,
acct_veh.state
FROM vehicle
INNER JOIN veh_unit
ON vehicle.vehicle_sak = veh_unit.vehicle_sak
INNER JOIN unit
ON veh_unit.unit_sak = unit.unit_sak
INNER JOIN vdu_profile
ON vdu_profile.vehicle_sak = vehicle.vehicle_sak
INNER JOIN vdu_profile_program
ON vdu_profile_program.vdu_profile_sak = vdu_profile.vdu_profile_sak
INNER JOIN veh_model
ON vehicle.veh_model = veh_model.veh_model
INNER JOIN acct_veh
ON acct_veh.vehicle_sak = vehicle.vehicle_sak
AND unit.unit_gen_id >= 36
AND vdu_profile_program.vdu_program_sak = 3
AND vdu_profile_program.preference_value = 'Y'
AND acct_veh.state IN ('MN', 'ND', 'IA')
AND acct_veh.acct_veh_status_id = 'A'
AND
-- Annual code
( -- 2006 conditions
AND vehicle.user_veh_desc LIKE ('%2007%')
AND regexp_like(veh_model.veh_model_desc, 'Vehicle[2356789]')
AND regexp_like(vehicle.vin, '_{7}[3WK0]_{9}')
OR
( -- 2007 conditions
AND vehicle.user_veh_desc LIKE ('%2006%')
AND regexp_like(veh_model.veh_model_desc, 'Vehicle[1234567]')
AND regexp_like(vehicle.vin, '_{7}[ZK]_{9}')
).
Maybe you are looking for
-
Where can I purchase a license of lion. My MBP didn't come with any disks (open box). I have purchased Mountain Lion and can't get it's download to work either. I get "This download is currupt or has been tampered with". When I boot into recovery I
-
SelectBooleanCheckbox in Map or Array?
Hi guys, Anybody has example on putting selectBooleanCheckbox in a Map/Array in backing bean so that in JSP page we don't need to put it like 1 by 1. For example, if I got a list of 5 facilities for selectBooleanCheckbox, I don't need to do it like t
-
Dear All i have a a project ADF-BC / JSF - JDeveloper 11.1.2.3.0 latest, and i have EO contains PK constrain in db in 2 fields (userid & Roleid) and i implemented bundle to handle error message with jbo error code and it works fine in AM test and i h
-
Time characteristic hierarchy in drill down report
Hello Experts, I try to modify the standard drilldown report in CJE2 - 001/12KST1C. It contains selection characteristic Period/year. Is it possible to add hierarchy to this characteristic? Ie. I want to be able to select year and drill down to perio
-
CS5 on Windows to CS6 on a Mac - can it be done?
I will switch over to a Mac from a Windows platform. Presently I am using Photoshop CS5, but since I cannot install this on a Mac perhaps I can update to a CS6 and ask for a Mac platform. Would I be able to install this on my new Mac? What about the