SQL query to build search form
I have a table Department, with primary key DeptId.
Table: Department
DeptId Text
1 xx
2 yy
3 zz
I have another table Person, with key (not primary, and not unique) PersonId, and foreign key DeptId
Table: Person
PersonId DeptId
10 1
11 1
11 2
12 2
I want to build a search form, with search fields PersonId and DeptId, so that in the resulting table, some rows from the Department table are returned. It should obey the following:
- If both PersonId and DeptId are selected, and there is a row in the table Person that matches the selection, the row from the Department table should be returned.
- If only PersonId is selected, any row in Department table that matches a DeptId corresponding to that PersonId should be returned.
- If only DeptId is selected, return that row from Department exactly once (even if that DeptId doesn't appear in Person table, like DeptId 3 in this example)
- If neither is selected, return all rows from Department
I'm using JDeveloper 11.1.2.2.0.
Thanks,
Zach
Hi Navaneeth,
Taking the example from your link:
SELECT org.org_id, org.name FROM organisations org WHERE EXISTS
(SELECT 1 FROM events evt WHERE evt.org_id = org.org_id
AND evt.contact_name = 'Eddie Harris')
Let's say I make evt.contact_name a dropdown list, and when I query it, I leave it unselected. Would it return org.org_id, org.name from a row that has no corresponding event row?
Similar Messages
-
Executing a column which has a sql query in a tabular form
Hi,
I have a tabular form which contains a column named as SQL which has sql statements in it. Without going to the SQL workshop in Apex i want to execute that query in the tabular form itself. For that i've created a page item which fetches the particular rows sql query and created a button 'EXECUTE'. When i click the execute button it should run the query and should display whether the query is correct or not and should show the error message.
Can any one provide solution for this or whether there are any other ways to achive this scenario.
With Regards
Balaji.P.KBalaji P.K wrote:
Can any one provide solution for this or whether there are any other ways to achive this scenario.Difficult as we have no idea what this "scenario" actually is. Go back several steps and explain in detail the problem this is supposed to solve.
I have a tabular form which contains a column named as SQLBad idea. <tt>SQL</tt> is a PL/SQL reserved word. Using any token from Oracle namespaces as a database identifier is confusing/problematic.
which has sql statements in it.Why? Where does this SQL come from? What is the source of the tabular form?
Without going to the SQL workshop in Apex i want to execute that query in the tabular form itself.Explain the "execute that query in the tabular form itself" concept. Where do the results go? -
SQL Query constructing for searching values in Tables single Column
I have Table USER_SERVICES with 2 Columns USER_NAME, Services. Please find below sample data on USER_SERVICES Table.
A user can be assigned with multiple services(service1, service2 etc.,). So we store data as below :
USER_NAME Services
user1 service1
user1 service2
user1 service3
user2 service1
user2 service2
user3 service3
user4 service4
I need to frame a SQL Query to 'select users assigned with Service A and Service B and Service n'.
Note: 'And' condition is used when Searching for multiple services.
Example: The SQL Query has to dynamically handle condition if i give Single Service, multiple Services(service1 and service2 e.t.c,)
If i say select users assigned with service1 and service2, from the above Table, result should be user1 and user2
If i say select users assigned with service1 and service2 and service3, from the above Table, result should be user1
If i say select users assigned with service4 from the above table, result should be user4
If i say select users assigned with service1 from the above table, result should be user2,user1
If i say select users assigned with service1 and service2 from the above table, result should be user2,user1
How do i construct the SQL Query. Any help will be appreciated.
Thanks,
Sathish
Alternate email: [email protected]How it will be stored? In a string or a table or a collection type?
If you have it in a table you can do like this.
with t
as
select 'user1' user_name, 'service1' services from dual union all
select 'user1', 'service2' from dual union all
select 'user1', 'service3' from dual union all
select 'user2', 'service1' from dual union all
select 'user2', 'service2' from dual union all
select 'user3', 'service3' from dual union all
select 'user4', 'service4' from dual
t1 as
select 'service1' services from dual union all
select 'service2' from dual
select distinct user_name
from (
select t.user_name, t.services, t1.services, count(t1.services) over(partition by t.user_name) cnt, t1.cnt cnt_1
from t
left join (select t1.*, count(*) over() cnt from t1) t1 on t1.services = t.services
where cnt = cnt_1 -
After query clear ADF search form
I have a adf search form with a date picker. When I push the search button I go to my view object class to change the query
String criteria3 = (String)vcr.getAttribute("Datenaiss");
if (attrs.getName()=="Datenaiss"){
if (criteria3 != null) {
criteria3="> '"+criteria3+"'";
vcr.setAttribute("Datenaiss", criteria3 );
The I receive the result on my jsp but in the field of my date I see now e.g. > '1992-01-17'
can you help me ?If have found a solution to clear the search form
protected void executeQueryForCollection(Object object, Object[] object1, int i) {
// set the inputfields to uppercase
switchStringsViewCriteriaToUpper();
// execute the query
super.executeQueryForCollection(object, object1, i);
// clear the parameters
super.clearViewCriterias();
} -
PL/SQL returning a Sql query in a report form
I am producing a simple form to display the number of non-conforming items produced per shift. I use some PL/SQL code to generate the appropriate start and end dates for the shift then build up a query to return.
I am having problems when I use a date mask (DD-MM-YYYY HH24:MI:SS), the :MI section of the date mask is seen as bound variables. If I restrict the time to HH24 without minutes or seconds, it works fine.
Does anyone have any thoughts on how to overcome this problem?This is the code I am using. As the shifts actually starts at 07:15 and 19:15, I need to add the minutes for it to be correct but they fail.
DECLARE
startDateStr VARCHAR2(25);
endDateStr VARCHAR2(25);
midniteStr VARCHAR2(25);
sqlQuery VARCHAR(1000);
BEGIN
-- Set up the start and end dates
startDateStr := to_char(sysdate, 'DD-MON-YYYY') || ' 07:15:00';
endDateStr := to_char(sysdate, 'DD-MON-YYYY') || ' 19:15:00';
midniteStr := to_char(sysdate, 'DD-MON-YYYY') || ' 00:00:01';
if (sysdate > to_date(startDateStr, 'DD-MON-YYYY HH24:MI:SS')
and sysdate < to_date(endDateStr, 'DD-MON-YYYY HH24:MI:SS'))
then
startDateStr := to_char(sysdate, 'DD-MON-YYYY') || ' 07';
endDateStr := to_char(sysdate, 'DD-MON-YYYY') || ' 19';
else
if (sysdate > to_date(midniteStr, 'DD-MON-YYYY HH24:MI:SS')
and sysdate < to_date(startDateStr, 'DD-MON-YYYY HH24:MI:SS'))
then
startDateStr := to_char(sysdate - 1,'DD-MON-YYYY') || ' 19';
endDateStr := to_char(sysdate, 'DD-MON-YYYY') || ' 07';
else
startDateStr := to_char(sysdate, 'DD-MON-YYYY') || ' 19';
endDateStr := to_char(sysdate+1, 'DD-MON-YYYY') || ' 07';
end if;
end if;
sqlQuery := 'select count(*), defectsource_s, disposition_s
from [email protected] ncr
where creationdate_t > to_date('
|| q'#'#' || startDateStr ||
q'#', 'DD-MON-YYYY HH24')
and creationdate_t <= to_date('#' || endDateStr ||
q'#', 'DD-MON-YYYY HH24')
and disposition_s in ('Scrap', 'Rework'#' ||
') group by defectsource_s, disposition_s
order by disposition_s, defectsource_s;' ;
return sqlQuery;
END; -
SQL Query: How to search on a dynamic set of patterns?
Hello,
I have a requirement where I need to search a table with dynamic set of patterns. Here is something I am looking for:
select * from T1 where C1 LIKE 'AA%' or C1 LIKE 'BB%' ...
That is, the number of patterns is unknown, it could be 0 or 1 or more. I cannot use the IN clause because this requires exact match.
Can you please help in letting me know how to do this?
Thanks a lotDear,
+In your last statement you mentioned "like C2||'%'".
I can't use this because C2 is the name of a column in table T2.+
Did you try this and you got an error or you are "concluding you can't" use this because C2 is name of a column?
Please try it. If you run into error, please post the error and you query.
Also...
1. When posting code, use BEFORE and AFTER the code, to format it nicely. It makes it easier to read.
2. In you posting questions include sample table,data expected output and your attempt in solving the problem. this will make it easier on everyone to help sooner and more effectively.
Best way to learn, SQL programming is to try it out.
So please try the sample and the suggestion and let us know if you get any error.
vr
Sudhakar B. -
SQL Query / Insert Builder
I am making a Java program to access / update a database via JDBC. I need to make string queries like this:
db.update(
"INSERT INTO sample_table(str_col,num_col) VALUES('Ford', 100)");
The query above is simple, but you can see how assembling the query made out of constant string fragments and variables that my program updates can become more complicated. For example, I need to also store a date. Is there a utility similar to StringBuilder for building strings that can make this process easier? I am looking for a function specifically to build SQL queries. It might look something like this:
querybuilder.insert(<table>, <name1>, <value1>, <name2>, <value2>....<nameN>, <valueN>);
Where table is the name of the table that I would be inserting into and name is the column name of the table, value would be the value to be stored under the column name. Are there SQL functions for this?I made such a library myself (called it an SQLFormatter), but haven't put it on the web because I've not thought there would be a general interest in it. Essentially, I can create an Insert Into statement like this:
SQLFormatter formatter = SQLFormatter.get(myStatement);
String sql = formatter.createInsertIntoStatement("MyTable", new String[] {"Field1", "Field2", "Field3"}, new Object[] {value1, value2, value3});I'd be happy to share it with you, but I'm not sure about how I can make it reach you. Do I need to post it on a web site? -
Sql query to build average value
I'm trying to build a query capture the below information. Any help is really appreciated.
Table looks something like below.
Room
Subject
Students
A1
Science
10
A1
Maths
20
B1
Science
15
B2
English
25
C1
Deutsch
20
A1
French
15
C1
English
10
How can i write a query so that i takes the average of students in each room and display the output? I need to write a query so that it also picks up any new rooms and students added.
Average is sum of total students in room by number of times each room is specified.
eg: A1 is listed 3 times a the result would be 10+20+15 divived by 3.
A1 B1 B2 C1
15 15 20 15
Thanks You
KKThis is called pivot. And you need to know all rooms upfront. Anyway, assuming you are on 11G:
SQL> with t as (
2 select 'A1' room,'Science' subject,10 students from dual union all
3 select 'A1','Maths',20 from dual union all
4 select 'B1','Science',15 from dual union all
5 select 'B2','English',25 from dual union all
6 select 'C1','Deutsch',20 from dual union all
7 select 'A1','French',15 from dual union all
8 select 'C1','English',10 from dual
9 )
10 select "'A1'_X" A1,
11 "'B1'_X" B1,
12 "'B2'_X" B2,
13 "'C1'_X" C1
14 from t
15 pivot(
16 avg(students) x,
17 max(subject) y
18 for room in ('A1','B1','B2','C1')
19 )
20 /
A1 B1 B2 C1
15 15 25 15
SQL>
SY. -
SQL query for formatted search
Hi guys and expertise,
I have this one problem where i need to do the formatted search,below is my example query that i have done
declare @itemcode varchar (20)
set @itemcode = (select x.itemcode from OITM x where x.ItemCode=[%])
declare @itmgrp varchar(50)
set @itmgrp=(select y.itmsgrpnam from OITB y where y.ItmsGrpNam=[%1])
select
CASE
when @itemcode = 'Item Descriptions 1' then @itmgrp
else t0.ItmsGrpNam
end
from OITB t0 inner join
OITM t1 on t0.ItmsGrpCod=t1.ItmsGrpCod
where t1.itemcode=@itemcode
***correct me if i'm wrong
the condition is :-
whenever the user selection is 'Item Descriptions 1' user will have to select the product group name else
if the user selection is not equal to "Item Descriptions 1", automatically the product group name will be default according
to the item code itself.
i wonder if it's possible? if it is, then i suppose there should be a way,right?Hi Gordon...
Thanks a lot for the answer,
I did try some research and manage to find a way out for the query that I want, the query is as below:-
BEGIN
IF
$[$38.1.0]='Item Descriptions' or $[$38.1.0]='Item Descriptions 1' or $[$38.1.0]='Item Descriptions 2' or
$[$38.1.0]='Item Descriptions 3' or $[$38.1.0]='Item Descriptions 4'
SELECT T0.ItmsGrpNam FROM OITB T0 inner join OITM T1 ON T0.ItmsGrpCod=T1.ItmsGrpCod
Group By T0.ItmsGrpNam
ELSE
SELECT T0.ItmsGrpNam FROM OITB T0 inner join OITM T1
ON T0.ItmsGrpCod=T1.ItmsGrpCod where T1.ItemCode=$[$38.1.0]
END
regards,
Asyraf -
SQL Query for user defined form
Hi experts,
I want query to update some fields in my user defined form using stored procedure on clicking of ADD button.
Explanation:
I have User Defined form call "DC request" with Object 'DCLM'..On click of Add button, it is saving my record info into the form. But in one field if value in not entered then it is saving as 'Null'.
Addon is already made, can not change in coding.
I want to update this field with '0' value through query in stored procedure on click of Add Button. Can anyone help me out to achieve this.
ThanksHi Team Bone.
Please try below Transaction Notification which will Restrict User to ADD if Field_Name is NULL and then User Need to Enter ZERO i.e. 0 for Adding.
IF @OBJECT_TYPE = 'DCLM' AND (@TRANSACTION_TYPE = 'A' or @TRANSACTION_TYPE = 'U')
BEGIN
If Exists (Select T0.DocEntry from [Table_Name] T0 Where ( T0.Field_Name is null OR T0.Field_Name = ' ' ) AND T0.DocEntry = @lisT_of_cols_val_Tab_del )
begin
SET @error = -100000
SET @error_message = 'Filed_Name is NULL then So, Please enter ZERO Value i.e. 0 in Filed_Name'
End
End
Please change Table_Name and Field_Name with with UDO field.
Hope this help
Regards::::
Atul Chakraborty -
hi all,
i created a report that displays results based on this query initially.
select *
from EMP
where 1=1;
i'm using a SQL Query (PL/SQL Function Body Returning SQL Query).
i have search items in my page like empno, lastname, firstname, middle, email_address etc... when i place a value for lastname then in my query it should add AND LASTNAME = :P1_LASTNAME.
so the query returned should now have
select *
from EMP
where 1=1
and LASTNAME = :P1_LASTNAME;
is this possible?
i was thinking something like looping for all search items in the page (ie. text, popup lov, datetimepicker) then check whether the item is null or not... when not null then probably get the substring of the item name like (ie. substr(:P1_LASTNAME) and get only LASTNAME then add "=" then concat it with the item name. not sure if this is possible though.
thanks
allenhi denes,
thanks for the sample... i noticed that the sample hard codes additional conditions to be added to the sql query which is okay when the search criteria is less than 5-10... but i'm thinking if the search criteria will be like 15-30 fields or make all fields searchable, this might be uncomfortable.
what i was thinking is like loop thru all the items i.e.
declare
sql varchar2(4000) := 'select * from emp where 1=1';
begin
for i in (select * from all_items_in_apex) -- this is just an example.
loop
if i.value is not null then
sql := sql || ' and ' || i.column || '=' || i.value;
end if;
end loop;
end;
the only thing is that i don't know if there's a possible replacement for the clause (select * from all_items_in_apex).
sorry if i'm asking too much but this is how i normally do it in forms. the suggestion in the link you provided should be sufficient but i was just wondering if this idea is also possible.
thanks again.
allen -
Lookup vo based on sql query or on Entity?
Hello all,
which is better for performance to build a view object to be used as lookup [LOV] based on SQL query or build an Entity object and a view object based on it? and why?
regards,As fas as i know,EO is better rather than SQL because result is backedup by EO cache..
-
What is the process of oracle when SQL query execute
hi
I would like to know about process of oracle engine when we are execute a SQL query
regardsYou can search that either in the Oracle docs or at fine blog http://antognini.ch/blog/ of Chris or in his book. You may also check SQL Tuning by Dan Tow for that matter.
regards -
How to get the SQL Query statement of a Search Form ?
Hi all,
We have a requirement to send the query result of an ADF Search Form into report application (Crystal rpt).
Crystal can accept data source as query statement. SO I think of getting the exact query statement "generated" by the ADF Search form and send it to crystal.
Is this possible ?
Thank you very much,
xtantoTry the various get methods of the viewObject such as getQuery:
http://www.oracle.com/webapps/online-help/jdeveloper/10.1.2/state/content/navId.4/navSetId._/vtAnchor.getQuery%28%29/vtTopicFile.bc4jjavadoc%7Crt%7Coracle%7Cjbo%7CViewObject%7Ehtml/ -
How to build sql query for view object at run time
Hi,
I have a LOV on my form that is created from a view object.
View object is read-only and is created from a SQL query.
SQL query consists of few input parameters and table joins.
My scenario is such that if input parameters are passed, i have to join extra tables, otherwise, only one table can fetch the results I need.
Can anyone please suggest, how I can solve this? I want to build the query for view object at run time based on the values passed to input parameters.
Thanks
Srikanth AddankiAs I understand you want to change the query at run time.
If this is what you want, you can use setQuery Method then use executeQuery.
http://download.oracle.com/docs/cd/B14099_19/web.1012/b14022/oracle/jbo/server/ViewObjectImpl.html#setQuery_java_lang_String_
Maybe you are looking for
-
2009 24-inch iMac 3.06GHz (EMC no. 2267). I don't get any audio when connecting my Sony HD TV to my iMac via mini DisplayPort. Is there a fix or hardware upgrade? I'm using a Kanex Mini DisplayPort to HDMI cable w/ audio support.
-
Hi there! I would like to iMessage a friend's iPhone from my iPod. I only have their phone number, which is an Irish phone number, though my Apple ID is registered in Canada. I have been unable to find the correct number format i.e. country code, are
-
Setting flag delivery complete on purchase order with a report
Hello! In our system we have many old purchase orders without delivery complete flag. Now it is very difficult to get a correct report with open deliveries. Now we should set the flag delivery complete on each old PO and his needs a lot of time and a
-
IDoc to HTTP communication....Is B2B required?
Hi All, I have a scenario where I have to communicate from SAP IDoc to a 3rd party via HTTP communication. Please tell me if I have to configure this as a B2B or A2A. Is it must to use B2B for Idoc communication with 3rd party. If yes then please let
-
ICal Events Deleted, Don't Have MobileMe, Have TM Running, Help
Okay, so out of the blue I now only have this month's calendar events (Jan 11') in iCal. Everything else is GONE. Running iMac w/OS 10.5.8. iCal 3.0.8 I have several years worth of events that are now gone. I have not manually used the backup feature