Need different rows from single query based on condition
Hi,
I have a table with 100 rows that holds employees and their roles.
I need to write a SQL(not a PL/SQL block) as below
1. When employee with role 'VP' logs in, the query should return all the 100 rows.
2. When employee with role 'MGR' logs in, the query should return only those rows whose MGR is the logged in employee.
3. When employee with role 'SALE_EXEC' logs in, it should return single rows corresponding to this SALE_EXEC.
My requirement here is to get these outputs from a single query.
Can anyone please help me with this.
Thanks,
Vivek.
use vpd
New Policy Groups
When adding the policy to a table, view, or synonym, you can use the DBMS_RLS.ADD_GROUPED_POLICY interface to specify the group to which the policy belongs. To specify which policies will be effective, you add a driving context using the DBMS_RLS.ADD_POLICY_CONTEXT interface. If the driving context returns an unknown policy group, then an error is returned.
If the driving context is not defined, then all policies are executed. Likewise, if the driving context is NULL, then policies from all policy groups are enforced. In this way, an application accessing the data cannot bypass the security setup module (which sets up application context) to avoid any applicable policies.
You can apply multiple driving contexts to the same table, view, or synonym, and each of them will be processed individually. In this way, you can configure multiple active sets of policies to be enforced.
Consider, for example, a hosting company that hosts Benefits and Financial applications, which share some database objects. Both applications are striped for hosting using a SUBSCRIBER policy in the SYS_DEFAULT policy group. Data access is partitioned first by subscriber ID, then by whether the user is accessing the Benefits or Financial applications (determined by a driving context). Suppose that Company A, which uses the hosting services, wants to apply a custom policy which relates only to its own data access. You could add an additional driving context (such as COMPANY A SPECIAL) to ensure that the additional, special policy group is applied for data access for Company A only. You would not apply this under the SUBSCRIBER policy, because the policy relates only to Company A, and it is more efficient to segregate the basic hosting policy from other policies.
How to Implement Policy Groups
To create policy groups, the administrator must do two things:
Set up a driving context to identify the effective policy group.
Add policies to policy groups as required.
The following example shows how to perform these tasks.
Note:
You need to set up the following data structures for the examples in this section to work:
DROP USER finance CASCADE;
CREATE USER finance IDENTIFIED BY welcome2;
GRANT RESOURCE TO apps;
DROP TABLE apps.benefit;
CREATE TABLE apps.benefit (c NUMBER);
Step 1: Set Up a Driving Context
Begin by creating a namespace for the driving context. For example:
CREATE CONTEXT appsctx USING apps.apps_security_init;
Create the package that administers the driving context. For example:
CREATE OR REPLACE PACKAGE apps.apps_security_init IS
PROCEDURE setctx (policy_group VARCHAR2);
END;
CREATE OR REPLACE PACKAGE BODY apps.apps_security_init AS
PROCEDURE setctx ( policy_group varchar2 ) IS
BEGIN
REM Do some checking to determine the current application.
REM You can check the proxy if using the proxy authentication feature.
REM Then set the context to indicate the current application.
DBMS_SESSION.SET_CONTEXT('APPSCTX','ACTIVE_APPS', policy_group);
END;
END;
Define the driving context for the table APPS.BENEFIT.
BEGIN
DBMS_RLS.ADD_POLICY_CONTEXT('apps','benefit','APPSCTX','ACTIVE_APPS');
END;
Step 2: Add a Policy to the Default Policy Group.
Create a security function to return a predicate to divide the data by company.
CREATE OR REPLACE FUNCTION by_company (sch varchar2, tab varchar2)
RETURN VARCHAR2 AS
BEGIN
RETURN 'COMPANY = SYS_CONTEXT(''ID'',''MY_COMPANY'')';
END;
Because policies in SYS_DEFAULT are always executed (except for SYS, or users with the EXEMPT ACCESS POLICY system privilege), this security policy (named SECURITY_BY_COMPANY), will always be enforced regardless of the application running. This achieves the universal security requirement on the table: namely, that each company should see its own data regardless of the application that is running. The function APPS.APPS_SECURITY_INIT.BY_COMPANY returns the predicate to make sure that users can only see data related to their own company:
BEGIN
DBMS_RLS.ADD_GROUPED_POLICY('apps','benefit','SYS_DEFAULT',
'security_by_company',
'apps','by_company');
END;
Step 3: Add a Policy to the HR Policy Group
First, create the HR group:
CREATE OR REPLACE FUNCTION hr.security_policy
RETURN VARCHAR2
AS
BEGIN
RETURN 'SYS_CONTEXT(''ID'',''TITLE'') = ''MANAGER'' ';
END;
The following creates the policy group and adds a policy named HR_SECURITY to the HR policy group. The function HR.SECURITY_POLICY returns the predicate to enforce security on the APPS.BENEFIT table:
BEGIN
DBMS_RLS.CREATE_POLICY_GROUP('apps','benefit','HR');
DBMS_RLS.ADD_GROUPED_POLICY('apps','benefit','HR',
'hr_security','hr','security_policy');
END;
Step 4: Add a Policy to the FINANCE Policy Group
Create the FINANCE policy:
CREATE OR REPLACE FUNCTION finance.security_policy
RETURN VARCHAR2
AS
BEGIN
RETURN ('SYS_CONTEXT(''ID'',''DEPT'') = ''FINANCE'' ');
END;
Create a policy group named FINANCE and add the FINANCE policy to the FINANCE group:
BEGIN
DBMS_RLS.CREATE_POLICY_GROUP('apps','benefit','FINANCE');
DBMS_RLS.ADD_GROUPED_POLICY('apps','benefit','FINANCE',
'finance_security','finance', 'security_policy');
END;
As a result, when the database is accessed, the application initializes the driving context after authentication. For example, with the HR application:
execute apps.security_init.setctx('HR');
Validating the Application Used to Connect to the Database
The package implementing the driving context must correctly validate the application that is being used to connect to the database. Although the database always checks the call stack to ensure that the package implementing the driving context sets context attributes, inadequate validation can still occur within the package.
For example, in applications where database users or enterprise users are known to the database, the user needs the EXECUTE privilege on the package that sets the driving context. Consider a user who knows that:
The BENEFITS application allows more liberal access than the HR application
The setctx procedure (which sets the correct policy group within the driving context) does not perform any validation to determine which application is actually connecting. That is, the procedure does not check either the IP address of the incoming connection (for a three-tier system) or the proxy_user attribute of the user session.
Such a user could pass to the driving context package an argument setting the context to the more liberal BENEFITS policy group, and then access the HR application instead. Because the setctx does no further validation of the application, this user bypasses the normally more restrictive HR security policy.
By contrast, if you implement proxy authentication with VPD, then you can determine the identity of the middle tier (and the application) that is actually connecting to the database on behalf of a user. In this way, the correct policy will be applied for each application to mediate data access.
For example, a developer using the proxy authentication feature could determine that the application (the middle tier) connecting to the database is HRAPPSERVER. The package that implements the driving context can thus verify whether the proxy_user in the user session is HRAPPSERVER. If so, then it can set the driving context to use the HR policy group. If proxy_user is not HRAPPSERVER, then it can disallow access.
In this case, when the following query is executed
SELECT * FROM APPS.BENEFIT;
Oracle Database picks up policies from the default policy group (SYS_DEFAULT) and active namespace HR. The query is internally rewritten as follows:
SELECT * FROM APPS.BENEFIT WHERE COMPANY = SYS_CONTEXT('ID','MY_COMPANY') and SYS_CONTEXT('ID','TITLE') = 'MANAGER';
How to Add a Policy to a Table, View, or Synonym
The DBMS_RLS package enables you to administer security policies by using its procedures for adding, enabling, refreshing, or dropping policies, policy groups, or application contexts. You need to specify the table, view, or synonym to which you are adding a policy, as well as the data pertinent to that policy, such as the policy name. Such data also includes names for the policy group and the function implementing the policy. You can also specify the types of statements the policy controls (SELECT, INSERT, UPDATE, DELETE, CREATE INDEX, or ALTER INDEX).
for more you can refer to
http://download-west.oracle.com/docs/cd/B19306_01/network.102/b14266/apdvcntx.htm
Similar Messages
-
Single row from this query without create a group by
Can I have a single row from this query without create a group by on tipo (TIPO can have only 2 value (A,D)
SELECT
CASE TIPO
WHEN 'D' THEN SUM(IMPORTO) ELSE 0 END DIMPORTO,
CASE TIPO
WHEN 'A' THEN SUM(IMPORTO) ELSE 0 END AIMPORTO
FROM MGIORNALE
WHERE K_CONTO = '100001' --CONTO
AND DECODE(T_MOVIM,'MRAP',TO_DATE('31/12/'||to_char(a_competenza),'DD/MM/YYYY'),DATA_RG)
-- BETWEEN DATAA AND DATAB
BETWEEN '01/01/2006' AND '31/12/2006'
--GROUP BY TIPO
--AND TIPO = COL_conto
Thanks in advanceIs like this?
sum (CASE TIPO
WHEN 'D' THEN IMPORTO ELSE 0 END) DIMPORTO, -
Updating, Adding, or Deleting rows from a query
So, I've got this page that originally was made of four
different forms. It worked great. But, then the client asked that I
"idiot proof" the page.
Originally, I had a form that enclosed a one-line table (with
Add & Clear buttons on the end) that took seven fields and
"added" a new record to the "tbljob" table.
That was followed with another form that created a table from
the results of a cfquery and listed all the rows from tbljob (with
Update/Delete buttons on the end of each row).
This worked fine when it was two separate forms. Now, I'm
trying to use only one "form" while displaying the two tables. The
buttons are now done with Javascript (so there are dialog boxes).
The problem is that I am getting a CF error message because the
insert, update, and delete commands are trying to pass the contents
of the fields as comma-delimited lists rather than the single data
item that is actually in each field. I am passing a hidden field
with each row that contains the "job_id".
I know the problem is that I'm not identifying each row as
being unique. I guess it's like I'm passing an "array" of data
rather than just a "row".
Basically, I just want to be able to display a blank row for
adding records followed by multiple rows from a query. The blank
row needs to have "Add" and "Cancel" buttons at the end and the
multi-row part has to have "Update" and "Delete" buttons on the end
of each row and manage the appropriate records.
It's got me stumped. TIA.What you can do is try using <cfloop> to insert, update
and delete your information.
For instance deleting the information try this.
<!---- Delete Jobs---->
<cfloop index="JobIDs" list="#Job_ID#" delimiters=",">
<cfquery name="delteall" datasource="dbsource">
DELETE
FROM table
Where job_Id = #JobIDs#
</cfquery>
</cfloop>
Do the same for your inserting and updating.
Because what CF see is job_id = 1,2,3,4,5,6,7,8,9
SO you are looping over a list = #job_id# and delimiter is "
, " and we are calling the index JobIDs. JobIDs are the values - 1
2 3 4 5 6 ....
I hope this helps. As for the displaying of the images, use
if then statements:
<cfif isdefined("edittable") and edittable eq '"y">
Then display the query and outputs for the editing options
<cfelseif isdefined("deletetable") and addtable eq "y">
The dispaly the query and outputs for adding options
<cfelse>
Then display the query for outputing the delete options
</cfif>
So your link will have soemthing of this sort <a
href="samepage.cfm?deletetable=y">Add Table</a>
Hope this helps,
Sevor Klu -
Need a record from second query which is not a part of main query.
I have this Query which Leads me to two Rwos of Data
select papf.employee_number E_CODE
,to_char(paaf1.effective_start_date,'DD-MON-RRRR') EFFECTIVE_START_DATE
,DECODE(to_char(paaf1.effective_end_date,'DD-MON-RRRR'),'31-DEC-4712',NULL,to_char(paaf1.effective_end_date,'DD-MON-RRRR')) EFFECTIVE_END_DATE
,TRIM(SUBSTR(PAAF1.ASS_ATTRIBUTE21,INSTR(PAAF1.ASS_ATTRIBUTE21,'-')+1)) PREVIOUS_CO
from apps.per_all_assignments_f paaf
,apps.per_all_people_f papf
,apps.per_grades pg
,apps.per_jobs pj
,apps.per_person_types ppt
,apps.per_person_type_usages_f pptuf
,apps.per_all_assignments_f paaf1
where 1=1
and papf.person_id = paaf.person_id
and pptuf.person_id = papf.person_id
and pptuf.person_type_id = ppt.person_type_id
and ppt.user_person_type = 'Employee'
and papf.current_employee_flag ='Y'
and paaf.primary_flag = 'Y'
and paaf1.primary_flag = 'Y'
and paaf1.grade_id = pg.grade_id
and paaf1.job_id = pj.job_id
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between pptuf.effective_start_date and pptuf.effective_end_date
and papf.person_id = paaf1.person_id
and (TRIM(UPPER(paaf1.ass_attribute24)) <> TRIM(UPPER(paaf.ass_attribute24))
OR TRIM(UPPER(paaf1.ass_attribute21)) <> TRIM(UPPER(paaf.ass_attribute21))
OR TRIM(UPPER(paaf1.ass_attribute22)) <> TRIM(UPPER(paaf.ass_attribute22))
OR TRIM(UPPER(paaf1.ass_attribute25)) <> TRIM(UPPER(paaf.ass_attribute25))
OR TRIM(UPPER(paaf1.ass_attribute23)) <> TRIM(UPPER(paaf.ass_attribute23))
OR paaf1.grade_id <> paaf.grade_id)
and paaf1.effective_end_date = paaf.effective_start_date - 1
and papf.employee_number in ('10620')
and paaf1.effective_start_date >= '01-JAN-1950'
---------------------------OUT PUT-----------------------------
E_CODE EFFECTIVE_START_DATE EFFECTIVE_END_DATE PREVIOUS_CO
Row1 10620 17-SEP-2009 30-NOV-2009 CORPORATE
Row2 10620 19-NOV-2007 31-JAN-2008 CORPORATE
Problem is enire output of the query is perfectly fine but in the second row at column effective_start_date insted of 19-NOV-2007 in need a value from another query. there must not be any change in rest of the columns data including first row.
i.e select ORIGINAL_DATE_OF_HIRE from per_all_people_f
where employee_number = '10620'
and rownum < 2
---------------------------OUT PUT----------------------------
15-MAY-2006
Is there is any approach to get this thing.
Thanks in advance
Bachan.
Edited by: Bachan on Sep 20, 2010 8:17 PMmaybe a union for your second row.
select E_CODE,
EFFECTIVE_START_DATE,
EFFECTIVE_END_DATE,
PREVIOUS_CO
from (select rownum rn,
papf.employee_number E_CODE
,to_char(paaf1.effective_start_date,'DD-MON-RRRR') EFFECTIVE_START_DATE
,DECODE(to_char(paaf1.effective_end_date,'DD-MON-RRRR'),'31-DEC-4712',NULL,to_char(paaf1.effective_end_date,'DD-MON-RRRR')) EFFECTIVE_END_DATE
,TRIM(SUBSTR(PAAF1.***_ATTRIBUTE21,INSTR(PAAF1.***_ATTRIBUTE21,'-')+1)) PREVIOUS_CO
from apps.per_all_assignments_f paaf
,apps.per_all_people_f papf
,apps.per_grades pg
,apps.per_jobs pj
,apps.per_person_types ppt
,apps.per_person_type_usages_f pptuf
,apps.per_all_assignments_f paaf1
where 1=1
and papf.person_id = paaf.person_id
and pptuf.person_id = papf.person_id
and pptuf.person_type_id = ppt.person_type_id
and ppt.user_person_type = 'Employee'
and papf.current_employee_flag ='Y'
and paaf.primary_flag = 'Y'
and paaf1.primary_flag = 'Y'
and paaf1.grade_id = pg.grade_id
and paaf1.job_id = pj.job_id
and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
and trunc(sysdate) between pptuf.effective_start_date and pptuf.effective_end_date
and papf.person_id = paaf1.person_id
and (TRIM(UPPER(paaf1.***_attribute24)) TRIM(UPPER(paaf.***_attribute24))
OR TRIM(UPPER(paaf1.***_attribute21)) TRIM(UPPER(paaf.***_attribute21))
OR TRIM(UPPER(paaf1.***_attribute22)) TRIM(UPPER(paaf.***_attribute22))
OR TRIM(UPPER(paaf1.***_attribute25)) TRIM(UPPER(paaf.***_attribute25))
OR TRIM(UPPER(paaf1.***_attribute23)) TRIM(UPPER(paaf.***_attribute23))
OR paaf1.grade_id paaf.grade_id)
and paaf1.effective_end_date = paaf.effective_start_date - 1
and papf.employee_number in ('10620')
and paaf1.effective_start_date >= '01-JAN-1950'0
where rn = 1
union all
select employee_number E_CODE,
ORIGINAL_DATE_OF_HIRE,
EFFECTIVE_END_DATE,
TRIM(SUBSTR(PAAF1.***_ATTRIBUTE21,INSTR(PAAF1.***_ATTRIBUTE21,'-')+1)) PREVIOUS_CO
from per_all_people_f
where employee_number = '10620'
and rownum < 2note: untested -
Hi everyone!
I want to know if it's posible to group the rows from a query into a unique row. Example:
SELECT Field_1 FROM Table_1
Returns the following fileds:
Field_1
A
B
C
D
I would want to modify my query so that the result obtained would be a single row containing all the others. In the previous example the result would be "ABCD".
Thanks in advance. Best regards,-- For anyone who wants to test this,
-- just copy this entire post as is,
-- save it to a .sql file, then start that file.
-- Before doing this,
-- make sure that you don't already have
-- a table named table_1 that would be dropped
-- or a .sql file named query.sql that would be overwritten.
-- If you do, then change the names below to avoid any conflicts.
-- In order to understand what the code is doing,
-- please read all of the comments included in this file,
-- as not everything is displayed when you run it.
-- I have gone into great detail with this
-- because I have posted similar things before
-- and a lot of people have total missed the fact
-- that it is not a static query,
-- that the results are dependent upon the number of rows,
-- and that it works for any number of rows,
-- even when the number of rows is unknonwn.
-- test data:
DROP TABLE table_1
CREATE TABLE table_1
(field_1 VARCHAR2 (1))
INSERT INTO table_1 (field_1)
VALUES ('A')
INSERT INTO table_1 (field_1)
VALUES ('B')
INSERT INTO table_1 (field_1)
VALUES ('C')
INSERT INTO table_1 (field_1)
VALUES ('D')
COMMIT
SELECT * FROM table_1
-- Running the code below will create and start
-- a file named query.sql which will contain the query below,
-- which will produce the results below that:
-- query that will be created:
-- SELECT MAX(DECODE(ROWNUM,1,field_1,NULL))
-- ||MAX(DECODE(ROWNUM,2,field_1,NULL))
-- ||MAX(DECODE(ROWNUM,3,field_1,NULL))
-- ||MAX(DECODE(ROWNUM,4,field_1,NULL))
-- FROM table_1 GROUP BY NULL;
-- results that will be produced:
-- ABCD
-- Notice that in this example,
-- there are only four values concatenated
-- because there are only four rows in the table.
-- If there were more rows, it would concatenate more values.
-- The number of concatenated values
-- is dependent upon the number of rows.
-- The following dynamic sql uses a single query,
-- which produces and executes a single query,
-- whose number of concatenated values is dependent
-- upon the number of rows in the table.
-- Because the echo is set off in the first line,
-- you won't see this code when you run it,
-- just the query that it creates.
SET ECHO OFF FEEDBACK OFF HEADING OFF PAGESIZE 0 VERIFY OFF
SPOOL query.sql
-- This is the start of the single query:
SELECT text
FROM (SELECT 1 AS orderby,
'SELECT MAX(DECODE(ROWNUM,1,field_1,NULL))' AS text
FROM DUAL
UNION
-- This section is the part that dynamically creates
-- one additional concatenated value
-- for each additional row after the first row.
-- This is just a subquery within the single query.
SELECT rn AS orderby,
'||MAX(DECODE(ROWNUM,'||rn||',field_1,NULL))' AS text
FROM (SELECT ROWNUM rn
FROM table_1)
WHERE rn > 1
UNION
SELECT COUNT (*) + 1 AS orderby,
'FROM table_1 GROUP BY NULL;' AS text
FROM table_1)
ORDER BY orderby;
-- This is the end of the single query.
SPOOL OFF
START query
SET ECHO ON
-- If you want to see the file containing
-- the single query that was created, then just:
-- SQL> EDIT query.sql -
SUM two fileds from different rows from the same table
I would like to SUM two fileds from different rows from the same table but I don't know how to do that.
E.g.
BillingTransactionsIndex CreateDate UserType UserIndex TransType Reference Total Balance
2 6/5/2008 15:02 1 51 1 150 -288.2 -288.2
5 6/8/2008 11:55 1 51 1 157 -1.58674 -289.787
In the table above I want SUM fields Total and Balance for the first row and the the next row SUM 2nd row Total with 1st row Balance
Please help
ThanksSQL> with tbl as
2 (select 1 as ID, 90 as total from dual
3 union all
4 select 2 as ID, 23 as total from dual
5 union all
6 select 3 as ID, 15 as total from dual
7 union all
8 select 4 as ID, 20 as total from dual)
9 select id , total, sum(total) over (order by ID) as balance from tbl
10 /
ID TOTAL BALANCE
1 90 90
2 23 113
3 15 128
4 20 148
SQL> -
Running Query Based on Condition
Hi
I want to run different query based on a condition. I am not able to execute the following query. is this the right way to run different query based on condition?
SELECT * FROM (
CASE WHEN (SELECT COUNT (*) from data1 where ID = 1 AND stype = 'A') > 0 THEN select * from data where ID = 1 AND stype = 'A'
WHEN (SELECT COUNT (*) from data1 where ID = 1 AND stype = 'B') > 0 THEN select * from data where ID = 1 AND stype = 'B'
WHEN (SELECT COUNT (*) from data1 where ID = 1 AND stype = 'C') > 0 THEN select * from data where ID = 1 AND stype = 'C'
END
) as a;
Edited by: user6016744 on 21 Apr, 2011 12:40 AMThis works for us; hope it helps.
CREATE OR REPLACE FUNCTION SCHEMA.GET_STEP_COST (loan_number_in in VARCHAR2, step_code_in in VARCHAR2 default null, ws_in in VARCHAR2 default null )
RETURN VARCHAR2 IS retval VARCHAR2 (50);
/*passing in loan number and step code you need the cost for, will return the cost*/
BEGIN
CASE ws_in --depending on workstation in is the table select
WHEN 'F' THEN
EXECUTE IMMEDIATE 'SELECT '|| step_code_in ||' FROM CLAIMS_FCL_STEPS WHERE LOAN_NUMBER = '|| loan_number_in ||'' INTO retval;
WHEN 'R' THEN
EXECUTE IMMEDIATE 'SELECT '|| step_code_in ||' FROM CLAIMS_RS_STEPS WHERE LOAN_NUMBER = '|| loan_number_in ||'' INTO retval;
WHEN 'L' THEN
EXECUTE IMMEDIATE 'SELECT '|| step_code_in ||' FROM CLAIMS_LM_STEPS WHERE LOAN_NUMBER = '|| loan_number_in ||'' INTO retval;
ELSE
retval := 0;
END CASE;
RETURN retval;
END GET_STEP_COST;
/ -
Deleting rows from one table while filter condition lies into another table
Hi All,
I'm facing a problem deleting a row from a table where the condition lies in other table. To ealaborate the scenario further let's say I have two table T1 (transaction table) and T2 (Master Table). I want to delete a record from table T1 by checking some conditions lies in table T2.
I tried with the below query but this query deleting records from both the table.
DELETE FROM ( SELECT * FROM T1 top INNER JOIN T2 tp
ON top.TID = tp.TID
WHERE top.DEAL_SITE_ID = inputparameter1
AND (TP.SEGMENT <>inputparameter2 OR tp.segment is not null));
Here the record is getting deleted from both the tables but I don't want to delete data from my master table i.e table T2.
Any help would be highly appreciated. Thanks in advance.
Regards,
SubhadeepSQL> select *
2 from the_table;
X Y
AA a
BB a
CC a
AA b
DD b
SQL> select *
2 from the_table_2;
X Y
AA a
BB a
CC a
SQL> delete from
2 (
3 select *
4 from the_table t1 inner join the_table_2 t2
5 on t1.x = t2.x
6 and t1.y = t2.y);
3 rows deleted.
SQL> select *
2 from the_table;
X Y
AA b
DD b
SQL> select *
2 from the_table_2;
X Y
AA a
BB a
CC a -
Select top row in Single Query?
Hi
Can somebody help me to write a query to get the first row after order by clause using single query alone.
Example:
I can write following query to select first row
select * from (selec * from t order by col1) where rownum = 1;
But here I should not use inline view to get the result. Because my original requirement needs to use this query in select list and it needs to use a column (of a table from the FROM clause) in the where clause of inline query. Because there is restriction that we can not use the column (of a table from the FROM clause) more than one level of inline query.
Please help me.Raghav.786 wrote:
Hi
Can somebody help me to write a query to get the first row after order by clause using single query alone.
Example:
I can write following query to select first row
select * from (selec * from t order by col1) where rownum = 1;
But here I should not use inline view to get the result. Because my original requirement needs to use this query in select list and it needs to use a column (of a table from the FROM clause) in the where clause of inline query. Because there is restriction that we can not use the column (of a table from the FROM clause) more than one level of inline query.
Please help me.
What Oracle version are you?
If you have 12c you can use
select col1,...
from t
order by col1
fetch first 1 row only;
If less than 12c, you have can't do it without a subquery.
What are you actually trying to do? Read Re: 2. How do I ask a question on the forums?
and follow the advice there by giving example create table and insert sample data statements and
explaining clearly what you are trying to do. Then we can help more. -
How can I Generate two different reports from single execution of Test cases in NI teststand
Hi,
My requirement is to generate two different reports from NI teststand. One for the Logging of error descriptions and the other report is by default generated by the Teststand. How can i generate a txt file that contains error descriptions other than that mentioned in the default report?
Solved!
Go to Solution.Do you need to do that just for these two sequences but not for other sequences? I don't see a problem to use SequenceFilePostStepRuntimeError. Create this callback in both sequence files and configure them to log into the same file. SequenceFilePostStepRuntimeError callback is called after each step of the sequence file if it has runtime error. You can access the calling step error information via RunState.Caller.Step.Result.Error property. Take a look to attached example.
The "other way" is useful if you need to log errors not for every step of the sequence file, but for some of them. This is more complex, because you need to create a custom step types for these steps. For the custom step you can create substeps (post-step in your case) which will be executed every time after step of this type executed. Then, this is you job to determine if error happened in the step, acces to step's error information is via Step.Result.Error property.
Also, be aware that step's post-expression is not executed in case of error in the step.
Sergey Kolbunov
CLA, CTD
Attachments:
SequenceFilePostStepRuntimeError_Demo.seq 7 KB -
Update several rows with single query (easy question, I guess)
Hi all!
I have table with two columns - name and value.
I populate it with several sql queries:
insert into settings (name, value) values ('company_name', 'My Company');
insert into settings (name, value) values ('company_address', 'Company Address 12');
insert into settings (name, value) values ('company_city', 'South Park');
How can update rows with company_name and company_city in single query?
Thank you in advance!How can update rows with company_name and company_city in single query?I guess something like this was meant:
update settings set value = ??? where name in ('company_name ', 'company_city');But it's still unclear to me what should be used instead of question marks... :)
Regards. -
qn. Field1
112
121
222
444
333
ans. Field1
444
333
222
112
121
let me know the answer .a single query for thisi assume it is not
ans. Field1
444
333
222
112
121
it is
ans. Field1
444
333
222
121
112
just do ORDER BY Field1 DESC -
Report from sql query invalid date condition
Hi, I created a Reports From SQL Query. My sql is like
"select column1, column2
from myschema.tablename
where mydate > :p_date1 and mydate < :p_date2"
I am using portal with turkish and english option. If I pass p_date paramaters 'dd-MON-yyyy' format
(for example p_date1 = 23-MAR-2003 p_date2 = 26-APR-2003) in english mode portlet is return correct result,
in turkish mode 'No Row Returned'. I changed my sql statement with
"select column1, column2
from myschema.tablename
where to_date(mydate,'dd/mm/yyy') > to_date(:p_date1,'dd/mm/yyy') and to_date(mydate) < to_date(:p_date2,'dd/mm/yyyy')"
and I pass p_date parameters 'dd/mm/yyyy' format (for example p_date1 = 23/03/2003 p_date2 = 26/04/2003)
but now turkish and english mode No row returned.
How may I write correct sql statament. My database NLS_DATE_FORMAT=DD/MM/YYYY and NLS_LANGUAGE=TURKISH.
thanks.Hi,
Try this for turkish mode:
Case 1: the 'mydate' column has a kind of a char data type (like char, varchar or varchar2)
and a value like '30/03/2003'
"select column1, column2
from myschema.tablename
where
to_date(mydate,'DD/MM/YYYY') > :p_date1
and
to_date(mydate,'DD/MM/YYYY') < :p_date2"
Case 2: the mydate column has a date type:
"select column1, column2
from myschema.tablename
where
mydate > :p_date1
and
mydate < :p_date2"
In both cases use the following parameter values:
p_date1 = 23/03/2003
p_date2 = 26/04/2003
This should work
Thanks
Peter -
Colour for a Paticular row in alv grid,based on condition
Hi Master's.
I want to display a paticular row in my alv_grid based on my condition a diffrent colour.
The Entire row ?
Regards,
AbhisekIf you are using the SALV model you can use class
* Final output table
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
auart TYPE auart,
kunnr TYPE kunnr,
t_color TYPE lvc_t_scol, " << for color
END OF ty_vbak.
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
DATA: lt_s_color TYPE lvc_t_scol,
ls_s_color TYPE lvc_s_scol.
ls_s_color-color-col = col_positive.
ls_s_color-color-int = 0.
ls_s_color-color-inv = 0.
APPEND ls_s_color TO lt_s_color.
CLEAR ls_s_color.
* Modify that data back to the output table
la_vbak-t_color = lt_s_color.
MODIFY ct_vbak FROM la_vbak.
CLEAR la_vbak.
You can check the entire code at: [SALV Table 9 u2013 Apply Colors|http://help-abap.zevolving.com/2008/10/salv-table-9-apply-colors/]
Regards,
Naimesh Patel -
Constructing a Query based on condition
Dear all,
i have a table attendance, it have two types of records, 1 is PLANNED 2 is RELEASED.
Dept_id *1* has two child sections. *2* and *3*.
if the Data entry operator from Dept_id=1 is logon, he could see PLANNED & RELEASED (BOTH) attendance from Dept_id =1
but
he could only see those entries from his child departments which are RELEASED.
How could we construct such a query?
RegardsThank you dear,
i have a table for Department
create table departments (dept_id number primary key,
dept_type char(1), ---- 1 for Department and 2 for Section (one department could have sections)
dname varchar2(60),
parent_dept number references departments(dept_id),
location varchar2(60));
create table attendance_header (att_date date,
att_kind char(1), ------------1 for OFFICIAL and 2 for OVERTIME
dept_sec_id number references departments(dept_id),
status char(1), -------PLANNED or RELEASED
is_holiday char(1),
is_weekend char(1));
alter table attendance_header add constraint pk_att_header primary key(att_date,att_kind,dept_sec_id);
create table attendance_details (emp_id varchar2(4) references emp(emp_id),
att_date date,
att_kind char(1),
dept_sec_id number references departments(dept_id),
att_type_no varchar2(2) references attendance_types(att_type_no), --- late , absent etc
duration number, ------ how much time he late
transaction_invoker varchar2(50));
these are the tables
NOW, suppose we take 3 departments 1,2,3 . 1 is the head Department of 2 &3 sections.
every section or department has its own manager.
the department 1 manager could see the data of its own departments both PLANNED & RELAESED.
BUT
for the sections 2 and 3, HE (the department 1 manager) could see only the RELEASED Data.
i want a single query which is issued by the department 1 manager for this purpose.
thanks and regards.
Edited by: Muhammad on Mar 31, 2010 1:19 AM
Maybe you are looking for
-
So I woke up in the morning today, detached my phone which was connected to a Bose Sound Dock II using an original Apple 30-Pin to Lightning connecter. I checked my notifications, replied to some messages on Whatsapp, GTalk, Snapchat and then unplugg
-
How can I get iTunes in English in Mexico. I am registered with a Canadian address
I have an iTunes account with a Canadian address. However, I am currently living in Mexico. How can I get iTunes Store downloaded to my computer and appleTV in English?
-
Direct procurement always as extended classic -- why ?
Hi , As per SAP's specification , ' SRM Server is the leading server for direct procurement. This means the system behaves according to extended classic scenario' . Is there any specific reason on why the scenario would always be extended classic for
-
HOW TO FIND THAT MY MACBOOK PRO POWER CHANGER IS ORGINAL OR NOT . WHERE TO FIND IT .
-
Can anyone Help me with this message? We could not complete your itunes store request an uknown error occurred (-1201)?