How to write conditional query?
I have a use case-on the basis of the value of the entry, I need to run the query.
for example
Select * from employees where em em. empId =? and em.firstname =? and em.jobid =?
Select * from employees where em em. empId =? and em.firstname =?
I need to write a query that is used internally this time condition and return records
Select * from employees where em
If (EM.) JobID is nothing)
{
Select * from employees where em em. empId =? and em.firstname =?
}
on the other
{
Select * from employees where em em. empId =? and em.firstname =? and em.jobid =?
}
How could I write this problem in sql?
Hello
You can try the following:
Select * from employees where em em. empId =? and em.firstname =? and (em.jobid =? or em.jobid is null).
Kind regards
Rommel Pino
http://soadev.blogspot.com
Tags: Database
Similar Questions
-
How to write a query to return rows with the varchar column that contains even a single occurrence of the characters, such as Ÿ and
I have a table whose columns with values such as
MINNEAŸPOLIS and ¿VV ¿A
Only the characters that are allowed in this column are alphabets, numbers, spaces, points and supports.
Please help to write a SQL SELECT with Regexp_like query or any other option.
Thanks to you all! Under query worked for me. Thank you Frank to explain the concept of hooks inside regexp_like.
SELECT * FROM testspecial, WHERE REGEXP_LIKE (sampletext, "[^] ^ A - Z ^ a - z ^ 0-9 ^ [^.]") ^ {^} ^]') ;
-
How to write the query option in expdp
Hi Please someone help me how to write the query option in expdp... .in expdp using the query option...
where AM columnname between 5 May 12 02:57:00.000' and ' 02:59:59.999 6 May 12: ';
Please do what is necessary...Pavan Kumar says:
QUERY = (columnname scott.test: "where between 5 May 12 02:57:00.000 h ' and ' 6 May 12 AM 02:59:59.999'")Who will fail in databases, because you assume nls_date_format. How it is difficult to put to_date() surround channels? Rather than play with quotation marks, using one parfile thusly.
query=table_owner.table_1:"where business_date between to_date('20120505025700 am','yyyymmddhhmiss am') and to_date('20120505025959 am','yyyymmddhhmiss am')" query=table_owner.table_2:"where business_date between to_date('20120505025700 am','yyyymmddhhmiss am') and to_date('20120505025959 am','yyyymmddhhmiss am')" query=table_owner.table_3:"where business_date between to_date('20120505025700 am','yyyymmddhhmiss am') and to_date('20120505025959 am','yyyymmddhhmiss am')"
You do not have to have all the clauses in a single line, as they are side by side parfile, which would be enough. For this reason parfile is better than the command line in order to avoid all the back-citing dance.
-
No idea how to write this query
Hi, My Data is as below
DocNum doc_date type of amount
1154 15 November 11 232501.5 invoice
200206 4 November 11 - 243672.64 credit memo
Note flow 111 5 November 555.22 11
Output must be
DocNum doc_date amount Type AmountDR AmountCR
1154 232501.5 15 November 11 Bill 232501.5
Note credit 200206 4 November 11 - 243672.64 - 243672.64
Note flow 111 5 November 555.22 11 555.22
If the amount is > 0, then it must be displayed in the value of the amount to be AmountDR
If amount < 0 then it must be displayed in the value of the sum amount CR
Can help how to write this querywith sample_table as ( select 1154 Docnum,date '2011-11-15' doc_date,232501.5 Amount,'Invoice' type from dual union all select 200206,date '2011-11-04',-243672.64,'Credit Memo' from dual union all select 111,date '2011-11-05',555.22,'Debit Memo' from dual ) select Docnum, doc_date, Amount, type, case when Amount >= 0 then Amount end AmountDR, case when Amount < 0 then Amount end AmountCR from sample_table / DOCNUM DOC_DATE AMOUNT TYPE AMOUNTDR AMOUNTCR ---------- --------- ---------- ----------- ---------- ---------- 1154 15-NOV-11 232501.5 Invoice 232501.5 200206 04-NOV-11 -243672.64 Credit Memo -243672.64 111 05-NOV-11 555.22 Debit Memo 555.22 SQL>
SY.
-
How to write a query for the data exchange between two columns?
How to write a query for the data exchange between two columns?
I tried a request, does NOT work.
Thank you.update tmp t1 set t1.m1=t1.m2 and t1.m2=(select t2.m1 from tmp t2 where t2.student_id = t1.student_id)
Published by: user533361 on October 23, 2009 14:04Just plain and simple:
update tmp t1 set t1.m1=t1.m2, t1.m2=t1.m1 /
SY.
-
How to write a query to join and right join
Hello
With the help of 10 gr 2:
I have a scenario where generate us a report by joining a few tables.
same goes for ex:
Select col1, col2, col5, col10 col22...
from tableA, tableB.
where tableA.col1 = tableB.col1 etc.
I have a requirement where choose a flag in the front-end server must be a right join. so, if the flag is N, I need to return the data returned by the join; but, if the flag is there I need to make a right join and returns all the data corresponding to table A and table B, as well as the data in table B.
(I did mention only two tables, as they are, they main engines, there are very many other tables joined in this request)
It is possible to write a query, so that the two scenarios can be addressed based on the flag?
Thanks in advance
Hello
user565033 wrote:
... When Oracle treats of the WHERE clause that starts from the bottom or the top? So it's best to put filters that reject the unnecessary lines at the beginning of the place where clause or end? Is how important it? ...
No, it does not matter.
The optimizer evaluates what condition will be faster and/or more selective (that is, will eliminate the most lines) and make the first condition.
If the optimzer cannot decide on what terms will be faster or more selective, it can arbitrarily don't close at the end of the WHERE clause first, but, even if you already knew for sure, this is exactly the kind of thing that is likely to change from one version to the other, or platform to another.
The optimizer (in recent versions, at least) is very good. If the optimizer can not say what conditions should apply first, then, chances are, there is really no significant difference, so it would be not serious of which one was made first.
If you read something that indicates the order is important, it is very outdated. Long ago (version 6), there was no cost-based optimizer and the order of the conditions in the WHERE clause was important. The cost-based optimizer was introduced in Oracle 7 (1992) and has been greatly improved by Oracle 8.1 (1998). Also later Oracle 10, the older optimizer ("regulated") was available for those who really want to continue to use it.
-
How to write this query in the hierarchy
Hi gurus,
Really need your help on this query. Thank you very much in advance.
SELECT t1.key as root_key , (SELECT t2.unit_id AS unit_id level-1 AS level , t2.name, t2.creator FROM tab t2 START WITH t2.unit_id = t1.unit_id -----check each node as root CONNECT BY prior t2.unit_id = t2.parent_unit_id ) t1.name as parent_unit_name FROM tab t1
I'll write a query of the hierarchy as above, and that EACH line (node, totally more than 10200) is checked as root node to see how many sheets are accessible for her... It must be implemented in a single query.
I know inline query should NOT return multiple rows or multiple columns, but the inline elements are necessary and can certainly be made in a correct solution.
(env):
Database Oracle 12 c Enterprise Edition Release 12.1.0.2.0 - 64 bit Production
PL/SQL Release 12.1.0.2.0
)
Test data:
select 1 as unit_id, null as parent_organization_unit_id, 'U1' as name from dual union all select 2, 1, 'U2' FROM DUAL UNION ALL SELECT 3, NULL, 'U3' FROM DUAL UNION ALL SELECT 4, 3, 'U4' FROM DUAL UNION ALL SELECT 5, 2, 'U5' FROM DUAL UNION ALL SELECT 6, 5, 'U6' FROM DUAL UNION ALL SELECT 7, 6, 'U7' FROM DUAL UNION ALL SELECT 8, 5, 'U8' FROM DUAL UNION ALL SELECT 9, 5, 'U9' FROM DUAL;
Final result should be like this
key unit_id, level, name, parent_name 1 1 0 u1 u1 1 2 1 u2 u1 1 5 2 u5 u1 1 6 3 u6 u1 1 7 4 u7 u1 1 8 3 u8 u1 1 9 3 u9 u1 2 2 0 u2 u2 2 5 1 u5 u2 2 6 2 u6 u2 2 7 3 u7 u2 2 8 2 u8 u2 2 9 2 u9 u2
Don't know how get you your output, it does not match your data...
with tab as)
Select 1 as unit_id, null as parent_organization_unit_id 'U1' as the name of double
Union of all the
Select 2, 1, 'U2' FROM DUAL
UNION ALL
SELECT 3, NULL, 'U3' FROM DUAL
UNION ALL
SELECT 4, 3, 'U4' FROM DUAL
UNION ALL
SELECT 5, 2, 'U5' OF THE DOUBLE
UNION ALL
SELECT 6, 5, 'U6' OF THE DOUBLE
UNION ALL
SELECT 7, 6, "U7" OF THE DOUBLE
UNION ALL
SELECT 8, 5, 'U8' FROM DUAL
UNION ALL
9. SELECT, 5, 'U9' FROM DUAL
)
Select dense_rank() key (order by connect_by_root unit_id), unit_id, level - 1 as 'LEVEL', connect_by_root name root_parent_name
t tab
Start with parent_organization_unit_id is null
Connect prior unit_id = parent_organization_unit_id
KEY UNIT_ID LEVEL ROOT_PARENT_NAME 1 1 0 "U1". 1 2 1 "U1". 1 5 2 "U1". 1 6 3 "U1". 1 7 4 "U1". 1 8 3 "U1". 1 9 3 "U1". 2 3 0 "U3". 2 4 1 "U3". -
Hello
How to write the sql query
I have three type of table as
1 table emp
EMP_ID FIRST_NAME DEPT_ID
---------- -------------------- ----------
1 kumar 10
2 sam 20
3 30 damu
2 table dept
EMP_ID SALE_ID DEPT_ID
---------- ---------- ----------
1 101 10
2 102 20
3 103 30
3. table sale
EMP_ID SALE_ID SALE_AMT
---------- ---------- ----------
1 101 7
2 102 8
3 103 9
I want the result as
EMP_ID DEPT_ID SALE_AMT
------- --------- - --------
1 10 7
Thank you
Are you looking for this?
SELECT T1. EMP_ID,
T1. DEPT_ID,
W3M SALE_AMT
FROM EMP T1,
SALE T3
WHERE T1. EMP_ID = T3. EMP_ID;
OUTPUT:
EMP_ID DEPT_ID SALE_AMT
------- ---------- ----------
1 10 7
2 20 8
3 30 9
If this is not the case, after the actual output, you need. Because that gives you the amount of sales deptwise
-
How to write a query for the given scenario?
Hi all
I have two tables EMP, DEPT with data below.
EMP TABLE: -.
SAL DEPID EMPLOYMENT ENAME EID
111 RAM 1500 10 MANAGER
222 SAM ASST MANAGER 2000 20
KALA 333 2500 10 REGISTRAR
444. BIMA 20 3000 MANAGER
CHALA 555 MANAGER 3500 30
RANI 666 ASST MANAGER 4000 10
777 KAMAL MANAGER 2400 10
DEPT TABLE: -.
DEPID DNAME
XX 10
AA 20
30 ZZ
Q1: I want the sum of the salary of each Department and for the particular job. Here, in each Department manager, Assistant Manager, clerk positions are there.
I want to display the result as below...
10-20-30 JOBS
--------------------------------------------------------------------------
MANAGER OF 3900 3000 3500
ASST MANAGER 4000 2000 NULL
THE CLERK 2500 NULL NULL
Please tell me how to write a sql query?
Thank you
SAISQL> SELECT job, 2 MAX(DECODE(deptno, 10, sum_sal)) "10", 3 MAX(DECODE(deptno, 20, sum_sal)) "20", 4 MAX(DECODE(deptno, 30, sum_sal)) "30" 5 FROM (SELECT deptno, job, SUM(sal) sum_sal FROM emp GROUP BY deptno, job) 6 GROUP BY job; JOB 10 20 30 --------- ---------- ---------- ---------- CLERK 1300 1900 950 SALESMAN 5600 PRESIDENT 5000 MANAGER 2450 2975 2850 ANALYST 6000 SQL>
-
How to write this query?
Hi people,
I need to get a query in which a set of records, I get ONLY those which previous registry has a field with a value to this topic. Other values, the field can contain are not necessary.
I know that sounds easy but... I can't get it.
So, for Oracle 10 g 2... Here's my query:
SELECT a.person_id, a.person_status, a.message_id, a.order_id
OF t_HR one
WHERE a.person_status = "rejected".
AND a.id >
(SELECT max (b.id)
OF t_HR b
WHERE b.person_id = a.person_id
and b.order_id = a.order_id
AND b.person_status! "revised =".
B.ID AND < a.id)
ORDER BY desc a.id
Let me explain:
1 - HR table is a table of people. These people has serveral STATUS.
2 - ID is a sequential (each www.voyages-sncf.com has a different identification number).
3 - the application must get THAT all people "rejected".
4. - However, (subquery) I need ONLY those that previous register (the second register) holds a status of "OK". If the person holds a "revised" status he's not, he should be the next register (the third)
5.-L' ORDER ID DESC, so is the first register must have a STATUS = "rejected" and the second a 'OK '. IF the second register = "revised", then the third register must be 'OK '. And I need this query.
HOW DO?
My problem: the subquery gives you previous register of the same guy, but... it does not give you the value of the State, I need, which is 'OK '.
I tried to add to the subquery...
SELECT max (b.id)
OF mod_human_resource b
WHERE b.person_id = a.person_id
and b.order_id = a.order_id
AND b.person_status = 'OK '.
AND b.id < a.id
... but if I have 5 records of that person, the first is "rejected", the second is "accepted", the third is 'new' and the fourth is 'OK'... the subquery gives you the 4th register and which is not correct for me, it must be only the second one (prior to the first State registry).
I need to be a query, because I need to use it on a MERGER for a DWH.
If there is another way (function, or even a procedure) to make the MERGER rather than with a request, which would be ok too. I am poor DWH knowledge.
Thanks in advance.
Hello
So, you need to know if a line is the 'first' line, and you should also know what is the 'next' status, (even the 'first' and 'next' are already defined). This sounds like a job for analytical functions. ROW_NUMBER can tell you if a line is first or not, and LEAD can tell you what a value on the next row.
Since you post CREATE TABLE and INSERT statements for your own table, I'll use the table scott.emp to illustrate.
Consider these data from scott.emp:
SELECT DeptNo
ename
work
FROM scott.emp
ORDER BY deptno
ename DESC
;
Output:
DEPTNO ENAME JOB
---------- ---------- ---------
10 MILLER CLERK
PRESIDENT OF KING 10
MANAGER 10 CLARK
20 SMITH CLERK
ANALYST SCOTT 20
20 JONES MANAGER
20 FORD ANALYST
20 ADAMS CLERK
30 WARD SALESMAN
SELLER OF 30 TURNER
30 MARTIN SALESMAN
30 JAMES CLERK
MANAGER BLAKE 30
30 ALLEN SALESMAN
Now, let's say we want only who know the departments where the forefront (in order descending ename) a job = 'CLERK', and the following line (also in descending by ename order) = "ANALYST" job, and we want to know the ename of the first row. In other words, the correct output is:
DEPTNO ENAME
---------- ----------
20 SMITH
Note that deptno = 10 is not included, even if the first task is to "CLERK." that was because the second job in deptno = 10 is the "PRESIDENT", not "ANALYST."
Here's a way to get these results:
WITH got_analytics AS
(
SELECT ename, deptno, job
ROW_NUMBER () OVER (PARTITION BY deptno
ORDER BY ename DESC
) AS r_num
LEAD (employment) OVER (PARTITION BY deptno
ORDER BY ename DESC
) AS next_job
FROM scott.emp
)
SELECT deptno, ename
OF got_analytics
WHERE r_num = 1
AND job = 'CLERK '.
AND next_job = 'ANALYST '.
;
I hope that answers your question.
If this isn't the case, then, as Dan (and the FAQ forum) said, post CREATE TABLE and INSERT statements for some sample data and the exact results you want from these data.
Post your query, based on the one I have posted more top and ponit out where he gets results.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: https://forums.oracle.com/message/9362002#9362002
-
How to write this query procedure of ina
IF ((drapeau = ' n ') OR (flag = 'F') OR (FLAG =' WAS))
) THEN
SELECT CUSTOMER_ID, BUSINESS_PASSCODE, LOG ON TO V_CUSTOMER_ID, V_BUSINESS_PASSCODE, V_LOG OF THE CUSTOMER
WHERE V_BUSINESS_PASSCODE AND V_CUSTOMER_ID = & ID = & CODE AND
V_LOG IN('Y')
SO I HAVE TO RETURN THE ISP (OF TYPE VARCHAR) CAN YOU TELL HOW DOMake values flag, id, code as a parameter and an output parameter. You can have any number f settings if you want to return the results of the query.
as
create procedure (identification number, varchar2, varcahr2, out returnvalue flag code varchar2)
as
Start
-your code---
--
IF ((drapeau = ' n ') OR (flag = 'F') OR (FLAG =' WAS))
) THEN
SELECT CUSTOMER_ID, BUSINESS_PASSCODE, LOG ON TO V_CUSTOMER_ID, V_BUSINESS_PASSCODE, V_LOG OF THE CUSTOMER
WHERE V_CUSTOMER_ID = ID AND CODE = V_BUSINESS_PASSCODE AND
V_LOG IN('Y');
returnValue =; -
Hi guys,.
In 10g DB.
I'm when extracting data value_column, the column having empids for most lines, but for some emailids lines.
The data that I get like this: here, I associate myself with three tables. based on these data, I need to recover only emailids.
VALUE_COLUMN
2959345
[email protected]
6560043
2392044
[email protected]
now, I want to get for all emailids only employees. so wherever empid is present, I need to get the email for this employee.
Please can someone help me on this.
Thanks in advance!
Rgrds,
-LRKHello
Whenever you have a problem, after a little bit database (CREATE TABLE and INSERT, only relevant columns instructions) samples and the results desired from these data.
How do you know if value_column is an email address and empid or something else? I'm going to assume that any string containing the cahracter ' @' is an email address, and everything else is an empid.
When value_column contains an empid, how do you have the email address? I'll assume you have another column that contains the email address in these cases.
Here's a way to do it, given only value_column and other this column
SELECT CASE WHEN INSTR (value_column, '@') != 0 THEN value_column ELSE some_other_column END AS email FROM table_x ;
However, it would be better to re - write a part of your existing application, to produce the same results more effectively.
-
How to write conditional region coding?
We know that the BEEP has conditional region. In a cell, you can use conditional region to control the issuance of data of special affection. What happens if this cell is controlled by month? If months = 1, this cell see the 110, month = 2 this cell see the 800 month = 3 this 100 etc see the cell... How can I make this code?
<? call: sub1? >
<? model: sub1? >
<? If: (months) = 1? > 110 <? end if? > <? If: (months) = 2? > 800 <? end if? >...Sent u t updated RTF
-
How to write the query with parameters below and is null
Hi, I need to recover the data if the parameter is passed on the ground, else retrieve all records where the field is nullExample of
CREATE TABLE DEPT
(DEPTNO NUMBER (2),)
DNAME VARCHAR2 (14).
LOC VARCHAR2 (13));INSERT INTO DEPT VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO VALUES DEPT (20, 'SEARCH', 'DALLAS');
insert into dept values (30, 'SALES', 'CHICAGO');
INSERT INTO VALUES DEPT (40, 'OPERATIONS', 'BOSTON');ALTER table dept add (object_id number);
insert into dept values (50, 'OPERATIONS', 'BOSTON', 1);
insert into dept values (60, 'OPERATIONS', 'BOSTON', 1);
INSERT INTO VALUES DEPT (70, 'OPERATIONS', 'BOSTON', 2);Select * from Department where object_id =: p_object_id
If I pass the object_id 1, it shloud retrieve the values of dept with object_id 1, but if I don't pass the object_id, it must retrieve all records where object_id is null
IF 1 is passed as the id of the object, he must recover, 50, 60, if 1 is not passed as object_id it should 10,20,30,40 retive
It works fine for me separately as,.
Select * from Department where object_id =: p_object_id - 1
Select * from Department where the object_id is null;
Is it possible to do in a single query.
Thank you
SELECT * FROM dept WHERE object_id = :p_object_id OR (object_id is null and :p_object_id is null)
is an approach. It's a little talkative, but the meaning is clear. If you can identify a value that would never appear in the data (for example-1)
WHERE nvl( object_id, -1 ) = nvl(p_object_id, -1 );
Justin
-
How to write a query for this data?
In this table, I dob column that is of type DATE, for example, 12 January 89 I want output like this
Day of the year
January 12, 89 Thusrs_day
January 12, 90 Friday
up to
12 January 14 Sunday
Select add_months (d, ((level*12)-12)), to_char (add_months (d, ((level*12)-12)), 'DY')
de)
Select d double to_date('12-jan-89','dd-mon-rr')
)
connect by level<=>=>
----------------------------- ------------------------------
12.01.1989 GAME
12.01.1990 FRI
12.01.1991 SAT
12.01.1992 SUN
12.01.1993 MAR
12.01.1994 SEA
12.01.1995 GAME
12.01.1996 FRI
12.01.1997 SUN
12.01.1998 LUN
MAR 12.01.1999
12.01.2000 SEA
12.01.2001 FRI
12.01.2002 SAM
12.01.2003 SUN
12.01.2004 LUN
12.01.2005 WED
12.01.2006 GAME
12.01.2007 FRI
SAM 12.01.2008
12.01.2009 LUN
12.01.2010 MAR
12.01.2011 WED
12.01.2012 GAME
24 selected lines
----
Ramin Hashimzade
Maybe you are looking for
-
Satellite P200: Screensaver does not start on Vista Home Premium
Hi people! I tried to apply the option on my laptop screen saver (Satellite P200), equipped with Vista Home Premium, but it does not; the laptop everything continues to run after the game of elapsed time. Is this a known problem with Vista? All sugge
-
Satellite C670D - 11K - Toshiba recovery disks does not work
I tried to recover the Satellite C670D - 11K using new factory system recovery disks provided by Toshiba. Both drives do their thing until the end when I get these messages: Files\mcafee\virusscan/dat\000.0\avvscan.dot c/program Error (error = 1117)E
-
Tecra T9000: Hardware malfunction start-up
NMI: CHANNEL CHECK / IOCHK message When starting my laptop, a TECRA T9000 I had problems with the message... * Hardware malfunction * NMI: CHANNEL CHECK/IOCHK this system stopped *. I had the hard drive changed and again I got this message once more,
-
P100-115 linux / gpu question
Yesterday, I installed suse 10.1 on my P100-115 (T2300, nvidia 7600). Installation went smothly and everything works. But after a while I noticed that the fan on the left side never turns (I guess that's for the graphics card). I thought this nvidia
-
Touchpad mouse does not work on my Satellite L850D - 12 p
Hello I had problems with my laptop since the beginning. The hard drive failed and the carpet of mouse and keyboard have had intermittent problems since day 1. Randomly, they stop working and will not work again until you restart. Recently the mouse