Re-writting query
Hi,
I have one report which i running slow. I know the problem this is due to a query which is called every time for each record. So, i need help to rewrite it.
1. This is the first query
SELECT id,
t_id,
p_id,
a_date,
d_date,
u_dt,
f_id,
FROM xyz
WHERE t_ind = 'N'
AND s_cd IN (3, 5)
AND p_id = '100177'
AND d_date BETWEEN to_date('10/01/2008', 'MM/DD/YYYY') AND to_date('12/31/2008', 'MM/DD/YYYY')
AND t_id IN (1,4)
Now, for every id it calls same table in another function to get s_id. (2)
2.
SELECT s_id
FROM (SELECT lead(m.s_id, 0) over(PARTITION BY m.p_id, m.t_id, m.a_date, m.d_date ORDER BY m.u_dt DESC, m.f_id DESC) s_id,
row_number() over(PARTITION BY m.p_id, m.t_id, m.a_date, m.d_date ORDER BY m.u_dt DESC, m.f_id DESC) row_num
FROM xyz
WHERE m.p_id = '100177'
AND m.t_id = 4
AND m.px_id = '001'
AND m.a_date = to_date('01/25/2009', 'MM/DD/YYYY')
AND m.d_date = to_date('02/07/2009', 'MM/DD/YYYY')
AND m.s_id IS NOT NULL)
WHERE row_num = 1;
3. So, for every call for 1st query it calls second one and this is where problem lies. How can we combine this both into one so that we have only one call to this table to avoide multiple calls.
so that we can include s_id into same query.
maybe this?
select xyz3,id,
xyz3.t_id,
xyz3.p_id,
xyz3.a_date,
xyz3.d_date,
xyz3.u_dt,
xyz3.f_id,
xyz3.s_id
from (SELECT xyz1.id,
xyz1.t_id,
xyz1.p_id,
xyz1.a_date,
xyz1.d_date,
xyz1.u_dt,
xyz1.f_id,
lead(xyz2.s_id, 0) over(PARTITION BY xyz2.p_id, xyz2.t_id, xyz2.a_date, xyz2.d_date
ORDER BY xyz2.u_dt DESC, xyz2.f_id DESC) s_id,
row_number() over(PARTITION BY xyz2.p_id, xyz2.t_id, xyz2.a_date, xyz2.d_date
ORDER BY xyz2.u_dt DESC, xyz2.f_id DESC) row_num
FROM xyz xyz1,
xyz xyz2
WHERE ((xyz1.t_ind = 'N') AND
(xyz1.s_cd IN (3, 5)) AND
(xyz1.p_id = '100177') AND
(xyz1.d_date BETWEEN to_date('10/01/2008', 'MM/DD/YYYY') AND to_date('12/31/2008', 'MM/DD/YYYY')) AND
(xyz1.t_id IN (1,4)))
AND ((xyz2.p_id = xyz1.p_id) AND
(xyz2.t_id = xyz1.t_id) AND
(xyz2.px_id = xyz1.px_id) AND
(xyz2.a_date = xyz1.a_date) AND
(xyz2.d_date = xyz1.d_date) AND
(xyz2.s_id IS NOT NULL))) xyz3
where xyz3.row_num = 1note: untested
Similar Messages
-
What to write query to get know if a checkbox is checked
hello,
I have created a checkbox with name Entertainment containing static values Movie,Play,music.
Now i have to write query to check which value of checkbox is checked ,means play or movie or music.
According to that i have to mail a particular link to a user.
I have seen in how to (checkbox help) but not getting it.
please anyone can help me out.HI marc,
I think so u are not able to get my problem,i will explain it in detail.
I have a table USERINFO which has variables of type varchar2,these variable are used as a checkbox on page name 'ENTER INFORMATION',these checkbox have some static values,every time a user checks checkbox values it get stored in database,suppose there is a checkbox name sport with static values cricket,football,now if any user select cricket this value get stored in database with username ,mailid.
Now i have a page on this i want to give user facility to enter name of checkbox to create and its corresponding static values,and these checkbox should get display on page 'ENTER INFORMATION'and a variable of type varchar2() with checkbox name should get create in tableUSERINFO .
now can u suggest me how can i achieve this. -
Regarding using text editor in 11g for writting query
how can i use text editor in oracle 11g for writting query as in oracle 9i in which we use 'ed' command.
user13063489 wrote:
how can i use text editor in oracle 11g for writting query as in oracle 9i in which we use 'ed' command.This is really for asking in the database forum.
However I assume you are using sqlplus
If this is the case define the _EDITOR variable to one of the following as appropriate ....
DEFINE _EDITOR=notepad
DEFINE _EDITOR=vi
For more assistance use the database general forum. -
Can we write query for fomatted search without from clause
can we write query for fomatted search without from clause as below.
SELECT (($(u_amt)*14)/100)
here U_amt is a UDF .I want to assign this to another field .
Rgds,
RajeevHi Rajeev,
You can write query for FMS without from. That is because you can omit it when you get value from active form by default. The grammar of it is:
Select $[$38.u_amt.0\] * 14/100 in your case if you have item type marketing document @line level.
From View-System Information, you can get the info you need for your FMS query at the left bottom of your screen.
Thanks,
Gordon -
How to write query group by time(minutes)
Hi,
i am writting query like this.
select host_id,pd_user, pdUserUtil,
timestamp from
(select host_id,pd_user,
avg(pd_cpu) pdUserUtil,
to_Char(PD_REPORTED_TIMESTAMP,'yyyy-mm-dd hh:MI') timestamp
from sp_process_detail_current SP
where exists (select * from
sp_process_detail_current
having
max(PD_REPORTED_TIMESTAMP)-sp.PD_REPORTED_TIMESTAMP<5/1440)
group by pd_user,host_Id,to_Char(PD_REPORTED_TIMESTAMP,'yyyy-mm-dd hh:MI'))
where host_id='DSCP04881' and pd_user='TOMCAT'
TIMESTAMP PD_USER
2006-06-30 08:07 TOMCAT
2006-06-30 08:08 TOMCAT
2006-06-30 08:10 TOMCAT
BUT WHILE ACCESSING IN JAVA
java.lang.NumberFormatException: For input string: "30:08:07"
at java.lang.NumberFormatException.forInputString(NumberFormatException.Question: What datatype in Java are you using as placeholder for the timestamp column?
It's only taking the "2006-06-" part.
Post here the java code (an exceprt of course), though I feel there's better forums for this subject. -
Hi,
I need expert advice.
on daily basis we are updating meter reading ( meter reading is nothing but "meter number / unit / date".
Let say there are 10 apartment and each apartment has separate meter.
i need to write query get missing meter reading on a specific date.
ThanksUnit Master
401
402
403
404
Daily Meter Reading
date unit meter
01-01-01 1 401
01-01-01 1 402
01-01-01 1 404 ( on 1st of jan user update the meter reading but if you notice unit number 403 is missing)
02-01-01 1 401
02-01-01 402 ( on 2nd of jan user update the meter reading but if you notice this time 402 is meter unit is missing)
02-01-01 1 403
02-01-01 1 404
03-01-01 401
03-01-01 1 402
03-01-01 1 403 ( on 3rd of jan user update the meter reading but if you notice this time meter unit is missing for 401 and 404 is unit is missing)
We want the following output
if user run report on 1st of jan system should return value of 403 unit number
if user run report on 2nd of jan then system should return value of 402 unit number
if user run report on 3rd of jan then system should return value of 401/404 unit number
Hope this help to get my expected answer. -
How to write query for this in TopLink ?
I am doing a simple search in jsp where the search will the based on the choices chosen by user.
I had given 3 check boxes for those choices.
The problem is, query will be based on the choice or choices chosed by the user.
How to write query for this in TopLink ?
Thanks in Advance..
JayaganeshTry below solution, it is NOT best solution but might work:
Declare @Questions TABLE (QuestionID INT, QuestionText Varchar(100))
INSERT INTO @Questions
VALUES (1, 'Comment'), (2, 'Score')
DECLARE @Answers TABLE (authkey INT, QuestionID INT, questiontext VARCHAR(100), answertext VARCHAR(100))
INSERT INTO @Answers
VALUES (101, 1, 'comment', 'hi!!'), (101, 2, 'score', '4'), (102, 1, 'comment', 'excellent'), (102, 2, 'score', '5'), (103, 2, 'score', '6'), (104, 2, 'score', '8')
SELECT
A.AuthKey
,Q.QuestionID
,Q.QuestionText
,A.AnswerText
FROM
@Questions Q
INNER JOIN @Answers A ON Q.QuestionID = A.QuestionID
UNION
SELECT
A.AuthKey
,Q.QuestionID
,Q.QuestionText
,Null
FROM
@Questions Q
CROSS JOIN @Answers A
WHERE
NOT EXISTS (SELECT 1 FROM @Answers SubQry WHERE SubQry.AuthKey = A.AuthKey AND SubQry.QuestionID = Q.QuestionID)
Output
AuthKey | QuestionID
| QuestionText
| AnswerText
101 | 1 | Comment | hi!!
101 | 2 | Score | 4
102 | 1 | Comment | excellent
102 | 2 | Score | 5
103 | 1 | Comment | NULL
103 | 2 | Score | 6
104 | 1 | Comment | NULL
104 | 2 | Score | 8
Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered. -
Need help to get alternate or better way to write query
Hi,
I am on Oracle 11.2
DDL and sample data
create table tab1 -- 1 millions rows at any given time
id number not null,
ref_cd varchar2(64) not null,
key varchar2(44) not null,
ctrl_flg varchar2(1),
ins_date date
create table tab2 -- close to 100 million rows
id number not null,
ref_cd varchar2(64) not null,
key varchar2(44) not null,
ctrl_flg varchar2(1),
ins_date date,
upd_date date
insert into tab1 values (1,'ABCDEFG', 'XYZ','Y',sysdate);
insert into tab1 values (2,'XYZABC', 'DEF','Y',sysdate);
insert into tab1 values (3,'PORSTUVW', 'ABC','Y',sysdate);
insert into tab2 values (1,'ABCDEFG', 'WYZ','Y',sysdate);
insert into tab2 values (2,'tbVCCmphEbOEUWbxRKczvsgmzjhROXOwNkkdxWiPqDgPXtJhVl', 'ABLIOWNdj','Y',sysdate);
insert into tab2 values (3,'tbBCFkphEbOEUWbxATczvsgmzjhRQWOwNkkdxWiPqDgPXtJhVl', 'MQLIOWNdj','Y',sysdate);I need to get all rows from tab1 that does not match tab2 and any row from tab1 that matches ref_cd in tab2 but key is different.
Expected Query output
'ABCDEFG', 'WYZ'
'XYZABC', 'DEF'
'PORSTUVW', 'ABC'Existing Query
select
ref_cd,
key
from
select
ref_cd,
key
from
tab1, tab2
where
tab1.ref_cd = tab2.ref_cd and
tab1.key <> tab2.key
union
select
ref_cd,
key
from
tab1
where
not exists
select 1
from
tab2
where
tab2.ref_cd = tab1.ref_cd
);I am sure there will be an alternate way to write this query in better way. Appreciate if any of you gurus suggest alternative solution.
Thanks in advance.Hi,
user572194 wrote:
... DDL and sample data ...
create table tab2 -- close to 100 million rows
id number not null,
ref_cd varchar2(64) not null,
key varchar2(44) not null,
ctrl_flg varchar2(1),
ins_date date,
upd_date date
insert into tab2 values (1,'ABCDEFG', 'WYZ','Y',sysdate);
insert into tab2 values (2,'tbVCCmphEbOEUWbxRKczvsgmzjhROXOwNkkdxWiPqDgPXtJhVl', 'ABLIOWNdj','Y',sysdate);
insert into tab2 values (3,'tbBCFkphEbOEUWbxATczvsgmzjhRQWOwNkkdxWiPqDgPXtJhVl', 'MQLIOWNdj','Y',sysdate);
Thanks for posting the CREATE TABLE and INSERT statments. Remember why you go to all that trouble: so the people whop want to help you can re-create the problem and test their ideas. When you post statemets that don't work, it's just a waste of time.
None of the INSERT statements for tab2 work. Tab2 has 6 columns, but the INSERT statements only have 5 values.
Please test your code before you post it.
I need to get all rows from tab1 that does not match tab2 WHat does "match" mean in this case? Does it mean that tab1.ref_cd = tab2.ref_cd?
and any row from tab1 that matches ref_cd in tab2 but key is different.
Existing Query
select
ref_cd,
key
from
select
ref_cd,
key
from
tab1, tab2
where
tab1.ref_cd = tab2.ref_cd and
tab1.key <> tab2.key
union
select
ref_cd,
key
from
tab1
where
not exists
select 1
from
tab2
where
tab2.ref_cd = tab1.ref_cd
Does that really work? In the first branch of the UNION, you're referencing a column called key, but both tables involved have columns called key. I would expect that to cause an error.
Please test your code before you post it.
Right before UNION, did you mean
tab1.key != tab2.key? As you may have noticed, this site doesn't like to display the <> inequality operator. Always use the other (equivalent) inequality operator, !=, when posting here.
I am sure there will be an alternate way to write this query in better way. Appreciate if any of you gurus suggest alternative solution.Avoid UNION; it can be very inefficient.
Maybe you want something like this:
SELECT tab1.ref_cd
, tab1.key
FROM tab1
LEFT OUTER JOIN tab2 ON tab2.ref_cd = tab1.ref_cd
WHERE tab2.ref_cd IS NULL
OR tab2.key != tab1.key
; -
Write query for Customer recievable Report.
*1.No concrete solution given to take project wise, bill wise. 2. Opening dues break up - project wise. Bill wise. 3. Aging days not able to select the required interval. Only standard intervals are available. 4. How to find the exact payment receivable for an customer. Pl tell me. How write a query in query generator..Sl no V.Code Customer Name Project Code Prj Type Series In no Posting Date Due Date Future Remit 0 - 30 30 - 60 60 - 90 90 + Total
For getting Customer receivable Aging Reports. Its SAP B1 2005 Version.*Hi,
Try this. May be this will help you.
select OCRD.cardcode 'Supplier Code',OCRD.cardname 'Name',sysdeb 'Debit GBP',syscred 'Credit GBP',
case JDT1.transtype
when '-2' then 'OB'
when '13' then 'IN'
when '16' then 'DN'
when '24' then 'RC'
else 'Other'
end 'Type',
Ref1,
fccurrency 'BP Currency',
CONVERT(VARCHAR(10), refdate, 103)'Posting Date' ,
CONVERT(VARCHAR(10), duedate, 103) 'Due Date',
CONVERT(VARCHAR(10), taxdate, 103) 'Doc Date' ,
CASE
when DATEDIFF(dd,taxdate,current_timestamp) < 31
then
case
when syscred <> 0 then syscred * - 1
else sysdeb
end
end "0-30 days",
case when (datediff(dd,taxdate,current_timestamp) > 30
and datediff(dd,taxdate,current_timestamp)< 61)
then
case
when syscred <> 0 then syscred * - 1
else sysdeb
end
end "31 to 60 days",
case when (datediff(dd,taxdate,current_timestamp) > 60
and datediff(dd,taxdate,current_timestamp)< 91)
then
case
when syscred <> 0 then syscred * - 1
else sysdeb
end
end "61 to 90 days",
CASE
when DATEDIFF(dd,taxdate,current_timestamp) > 90
then
case
when syscred 0 then syscred * - 1
else sysdeb
end
end "90 + days"
from JDT1,OCRD where JDT1.shortname = OCRD.cardcode and cardtype = 'C' and intrnmatch = '0'
ORDER BY OCRD.CARDCODE, taxdate
Thanks
Pradnya -
Write Query Results into a text file
Hello All,
In SAP B1 Query Generator I'm trying to write the results of a query into a text file (.txt). I've tryed with queryout but it doesn't work.
What I want is something like this:
SELECT * FROM TABLE queryout "C:/textfile.txt"
Any sugestions?
Thanks in advance,
Miguel BarbosaHello All,
Since yesterday and due to your help, I've found how to solve my problem.
This post is only to share with you and with the others users of Expert Forums how to Write the results of an SQL Query in SAP Business One to a Text File without making it by exportation.
Requested Text File by customer:
Document type in R/3 If Invoice -- 'Z100' - Position 1 to 4 - 4 characters
Supplier Code -- dbo.Table.CardCode -- Position 5 to 15 - 10 characters
Item Code -- dbo.Table.ItemCode -- Position 11 to 25 - 14 characters
Client Code -- dbo.Table.CardCode -- Position 26 to 36 - 10 characters
Client Name -- dbo.Table.CardName -- Posiiton 37 to 100 - 63 characters
Fixed Blank Field -- ' ' -- Position 101 to 120 - 19 blank spaces
Quantity -- dbo.Table.Quantity -- Position 121 to 140 -- 19 characters
Unit Price -- dbo.Table.PriceBefDi -- Position 141 to 160 -- 19 characters
If Invoice Fixed identifier -- 'Z01' -- Position 161 to 163 -- 3 characters
Fixed Blank Field -- ' ' -- Position 164 to 220 -- 56 characters
Document Date -- dbo.Table.DocDate -- Position 221 to 229 - 8 characters
Delivery Date -- dbo.Table.ActDelDate -- Position 230 to 238 - 8 characters
Sales Order Number - dbo.Table.DocNum -- Position 239 to 260 - 21 characters
Delivery Number - dbo.Table.DocNum -- Position 261 to 282 - 21 characters
Follows the code:
EXEC master..xp_cmdshell'bcp
SELECT DISTINCT
''Z100''
+ ''ZP00002091''
+ IsNull(Convert(nVarChar(44), Cast(T0.DocNum As Numeric(38, 0))), '''')
+ ''PTAP''
+ '' ''
+ ((LTRIM(RTRIM(T1.U_NumConc)))) + SPACE(10-LEN(LTRIM(RTRIM(T1.U_NumConc)))) + ((LTRIM(RTRIM(T3.BaseRef)))) + SPACE(10-LEN(LTRIM(RTRIM(T3.BaseRef))))
+ '' ''
+ ''Z02''
+ CONVERT(varchar(10), T1.ActDelDate, 112)
+ CONVERT(varchar(10), T0.DocDate, 112)
+ '' ''
+ IsNull(Convert(nVarChar(44), Cast(T1.BaseRef As Numeric(38, 0))), '''')
+ ((LTRIM(RTRIM(T1.ItemCode)))) + SPACE(18-LEN(LTRIM(RTRIM(T1.ItemCode))))
+ ''0'' + REPLACE(SPACE(12-LEN(CONVERT(varchar(12), CAST(T1.Quantity AS NUMERIC (38,3))))),'' '',''0'') + REPLACE(((LTRIM(RTRIM(CONVERT(NUMERIC(38,3),T1.Quantity))))),''.'','''')
+ ''0'' + REPLACE(SPACE(12-LEN(CONVERT(varchar(12), CAST(T1.PriceBefDi AS NUMERIC (38,2))))),'' '', ''0'') + REPLACE(((LTRIM(RTRIM(CONVERT(NUMERIC(38,2),T1.PriceBefDi))))) ,''.'','''')
+ ''ZPHA''
+ '' ''
+ '' ''
+ '' ''
+ ((LTRIM(RTRIM(T4.CardName)))) + SPACE(69-LEN(LTRIM(RTRIM(T4.CardName))))
+ '' ''
+ CONVERT(varchar(10), T0.DocDate, 112)
+ '' ''
FROM
JFOTST.dbo.OINV T0
INNER JOIN JFOTST.dbo.INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN JFOTST.dbo.ODLN T2 ON T2.DocNum=T1.BaseRef
INNER JOIN JFOTST.dbo.DLN1 T3 ON T3.DocEntry=T2.DocEntry
INNER JOIN JFOTST.dbo.OCRD T4 ON T1.U_NumConc=T4.AddId
WHERE T0.CardCode=''C52''
queryout "c:\text1.txt" -c -T -x'
Note that in this code everytime you need to use the symbol ' you have to doble it like ''Fixed Text''.
Be aware that there is one UDF so if you try to make this query run in your SB1 you will have to create a UDF called U_NumConc in the document lines.
Database usage:
JFOTST.dbo.OINV T0
JFOTST.dbo - Database Name.
OINV - Table
T0 - Alias
For any doubts I'll be available to help.
Once again thank you very much for all your help.
Together we are stronger!
Kind Regards,
Miguel Barbosa
Edited by: Miguel Barbosa on Aug 18, 2009 12:09 PM
Edited by: Miguel Barbosa on Aug 18, 2009 12:10 PM -
Reg.write query for report
Dear Experts
I am MM consultant and I am in need of generate reports.I don't know how to get the data from two table. Pls help me
with some codings
Thanks
Rajakumar.KHi.....
Check this... Here Iam joining MARA and MAKT tables.....
data: begin of itab occurs 0,
matnr like mara-matnr, <----like this you can select no.of fields from MARA
spras like makt-spras, <----like this you declare ur internal table with no.of fields of MAKT table
end of itab.
parameters: p_matnr like mara-matnr. <------ Later according to this material number you can display details
select p~matnr q~spras <----with p~ you can declare no.of from MARA, but those should me mention in above ITAB , also MAKT with q~
from
mara as p <---- p is for first table fileds
inner join <----- key word
makt as q <----- q is for second table fields
on p~matnr = q~matnr <----this is for link
into corresponding fields of table itab
where p~matnr = p_matnr. <------checking where condition with parameter
loop at itab.
write:/ itab-matnr, <--------here you can display the fields which you defined in ITAB
itab-spras.
endloop.
Also check this query....
Here you can display all the details of that tables...
data: begin of itab occurs 0,
matnr like mara-matnr,
spras like makt-spras,
end of itab.
data: p_matnr like mara-matnr.
select-options: g_matnr for p_matnr. <------First change
select p~matnr q~spras from mara as p inner join makt as q
on p~matnr = q~matnr
into corresponding fields of table itab
where p~matnr in g_matnr. <----second change
loop at itab.
write:/ itab-matnr,
itab-spras.
endloop.
Also check this code and get an idea about for all entries method and joining of more than two tables...
data: begin of itab occurs 0,
matnr like mara-matnr,
spras like makt-spras,
end of itab.
data: begin of jtab occurs 0,
matnr like marc-matnr,
werks like marc-werks,
end of jtab.
data: begin of t_out occurs 0,
matnr like mara-matnr,
spras like makt-spras,
werks like marc-werks,
end of t_out.
data: p_matnr like mara-matnr.
select-options: g_matnr for p_matnr.
select p~matnr q~spras from mara as p inner join makt as q
on p~matnr = q~matnr
into corresponding fields of table itab
where p~matnr in g_matnr.
if itab[] is not initial.
select * from marc into corresponding fields of table jtab
for all entries in itab
where matnr = itab-matnr.
endif.
loop at itab.
t_out-matnr = itab-matnr.
t_out-spras = itab-spras.
read table jtab with key matnr = itab-matnr.
if sy-subrc = 0.
t_out-werks = jtab-werks.
endif.
append t_out.
clear t_out.
endloop.
loop at t_out.
write:/ t_out-matnr,
t_out-spras,
t_out-werks.
endloop.
All the best!!
Thanks,
Naveen.I -
SQL Developer not fetching data tables while writting query
HI , I have recently started using SQL developer 2.1, the problem i face is that SQL DEVELOPER is not showing the tables names while writting the query, we have more than 5000 tables and each table name cannot be remembered.
Thanks
EssaI'm using the Mac version of SQL Developer (Version 2.1.1.64 | Build MAIN-64.45) and I'm having similar problems. I'm currently using OS X Snow Leopard.
The issue does not happen consistently: If I start writing an anonymous block in a new SQL window, the Completion Insight features works well. I can start typing a name of a package (of which there are many in our db) and I'm quickly provided a list matching what I've typed so far. This is also true of variables, functions and procedures within the packages after I type a period following the package name.
However, if I open an existing package body and start editing, the Completion Insight feature becomes less reliable. If I move my cursor to the beginning of the main block of one of the package body's internal procedures, Completion Insight works. As I move down the package and try to insert new code, Completion Insight stops working. I should note that this package body I'm working with has no errors and compiles fine. I'm not sure what this indicates...whether it's an issue with the editors internal parser or some other issue.
I'm not sure the best method for starting SQL Developer via the console window on the Mac. I found the following sh file which does launch it from terminal:
/Applications/SQLDeveloper.app/Contents/MacOS/sqldeveloper.sh
This does launch the application. As I navigate around and follow the instructions you had given (including ctrl-space) I see no output generated out to the console (errors or other messages).
I'm happy to assist in further debugging. Having Completion Insight work consistently will certainly increase my productivity with the tool.
Thanks in advance for any help you may provide. -
How to conditionally write query ?
i have one use case -
on the basis of enter value i need to execute query .
for example
select * from employees em where em. empId=? and em.firstname=? and em.jobid=?
select * from employees em where em. empId=? and em.firstname=?
i need to the write one query which is internally used this both condition and return the records
select * from employees em where
if(em.jobid==null)
select * from employees em where em. empId=? and em.firstname=?
else
select * from employees em where em. empId=? and em.firstname=? and em.jobid=?
how could i write this problem in sql ?Note the name of this forum is "SQL Developer *(Not for general SQL/PLSQL questions)*" - so only for issues with the SQL Developer tool. Please post these questions under the dedicated SQL And PL/SQL forum.
Regards,
K. -
How to write query for this scenario
Hi -
I have two table like this:
Question:
questionid questiontext
1 comment
2 score
Answer:
authkey questionid
questiontext answertext
101 1 comment hi!!
101 2 score 4
102 1 comment excellent
102 2 score 5
103 2 score 6
104 2 score 8
Here there are two question (score and comment) and answer is stored in answer table. there are case when there is no comment and only answer. but answer would always be there in answer table for each authkey.
I want to write the query that gives the result that if no comment is given for authkey then return null as answer. something like below:
Desired Result:
authkey questionid questiontext
answertext
101 1 comment hi!!
101 2 score 4
102 1 comment excellent
102 2 score 5
103 2 score 6
103 1 comment null
104 2 score 8
104 1 comment null
what query can i write to get the above desired result.
Thanks in advanceTry below solution, it is NOT best solution but might work:
Declare @Questions TABLE (QuestionID INT, QuestionText Varchar(100))
INSERT INTO @Questions
VALUES (1, 'Comment'), (2, 'Score')
DECLARE @Answers TABLE (authkey INT, QuestionID INT, questiontext VARCHAR(100), answertext VARCHAR(100))
INSERT INTO @Answers
VALUES (101, 1, 'comment', 'hi!!'), (101, 2, 'score', '4'), (102, 1, 'comment', 'excellent'), (102, 2, 'score', '5'), (103, 2, 'score', '6'), (104, 2, 'score', '8')
SELECT
A.AuthKey
,Q.QuestionID
,Q.QuestionText
,A.AnswerText
FROM
@Questions Q
INNER JOIN @Answers A ON Q.QuestionID = A.QuestionID
UNION
SELECT
A.AuthKey
,Q.QuestionID
,Q.QuestionText
,Null
FROM
@Questions Q
CROSS JOIN @Answers A
WHERE
NOT EXISTS (SELECT 1 FROM @Answers SubQry WHERE SubQry.AuthKey = A.AuthKey AND SubQry.QuestionID = Q.QuestionID)
Output
AuthKey | QuestionID
| QuestionText
| AnswerText
101 | 1 | Comment | hi!!
101 | 2 | Score | 4
102 | 1 | Comment | excellent
102 | 2 | Score | 5
103 | 1 | Comment | NULL
103 | 2 | Score | 6
104 | 1 | Comment | NULL
104 | 2 | Score | 8
Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered. -
Help to write query to get records from specified time TO specified time?
I have a table which has large data and the table is updated every min,
Suppose the Table comp(timeid date,sessions varchar2,sid varchar2)
Here time needs to be checked
So now i have to write a query which displays records that have been in the table
for every 5 mins
default timeframe is sysdate-5 then the lower and upper bound is multiple of 5 i.e
Suppose now Time and sysdate: 12-sep-2007 1:22:00pm
From sysdate: 12-sep-2007 1:15:01pm TO sysdate: 12-sep-2007 1:20:00pm
So i need a query to get the records from 1:15:01pm to 1:20pm.
Message was edited by:
user594721Explanation is not clear.Please give sample data and expected o/p.
If you need the data of previous five minutes, do like this...
SQL> select id, to_char(time,'dd/mm/yyyy hh:mi:ss') time
2 from test;
ID TIME
1 13/09/2007 10:29:55
1 13/09/2007 10:29:22
1 13/09/2007 10:20:28
SQL> select id, to_char(time,'dd/mm/yyyy hh:mi:ss') time,
2 to_char(sysdate,'dd/mm/yyyy hh:mi:ss') current_time
3 from test
4 where time between (sysdate - (5/24/60)) and sysdate;
ID TIME CURRENT_TIME
1 13/09/2007 10:29:55 13/09/2007 10:32:45
1 13/09/2007 10:29:22 13/09/2007 10:32:45 -
How to write query to list all the employees of deptno 20
Hi ,
I am new to Oracle...Recently I got job.... Could you please help me....
How to write a query for
1) List all the Clerks of Deptno 20 and
2) List the emps along with their exp and whose daily salary is more than 100rsits not wrong that you ask , but this is basic question and my suggestion is to start reading some manual specially since you got job , Read oracle documentation gogole what you want if you didn't find any answer post here and we will help you .
Check this link
http://www.oracle-dba-online.com/sql/oracle_sql_tutorial.htm
http://www.w3schools.com/sql/default.asp
http://www.java2s.com/Tutorial/Oracle/CatalogOracle.htm
And you will find more & more .. create your own vmware oracle provide with learning database called XE (express edition) test it and hope this information is useful for you
Maybe you are looking for
-
I have around 22GB of music in my ITunes library on my MAC and would love to be able to transfer it to a USB drive. I tried to transfer it to a 32GB drive and I receive a notice stating that the drive is too small. Why is that? Any helpful info would
-
How do I print a mirror image on my HP D110?
I am unable to print photos in a mirror image for producing iron on transfers. The "Mirror" image is not available in the features or advanced tabs. If I select "Iron On Transfer" as my paper it still prints normally, not in a mirror fashion.
-
HT201343 Can't use airplay mirroring
My MacBook Pro is (13-inch, Mid 2012) version: (10.7.5 Lion ) intel Core i5, But i can find the airplay mirroring and i can't find it on the system preferences Display setting ??? what can I do ?
-
Some of the songs in my Itunes library jump to the next song before it finish. What can I do to fix this problem? Please help!!
-
How to eliminate unwanted addresses in Adressbook
how can I eliminate unwanted addresses in Addressbook V 4.0.6 ?