Combine two queries to Update a table in single query.
I need to update a table in a stored procedure in following manner:
IF p_status = 'FAILED' THEN
UPDATE pms_table
SET status = p_status
WHERE quote_id = p_quote_id
IF p_status = 'COMPLETED' THEN
UPDATE pms_table
SET status = p_status
WHERE quote_id = p_quote_id
How to combine above two queried in single query having same fuctionality.
Regards,
AgrawalV
UPDATE pms_table
SET status = p_status
WHERE quote_id = p_quote_id
and p_status in ('FAILED','COMPLETED');
Similar Messages
-
Combining two queries into one query if possible
Hi there. I would like, if possible, to somehow combine the queries 1) and 2) into a single query.
1) select distinct user_id from system_user_sessions;
This query returns all unique users from the indicated table.
2) select count(session_id) from system_user_sessions where user_id = "each user_id returned from 1)";
This query will return, for each distinct user_id, the number of sessions involving that user. In other words I would like to return the user_id of each user together with the number of session_ids involving that user.
any ideas? JoeI assume you are looking for something like this:
select count(session_id)
from system_user_sessions
where user_id in ( select distinct user_id
from system_user_sessions
);HTH
Ghulam -
Two forms that update differrent tables on the same page?
I tried to add two forms that update different tables on to one page.
Problem is when I do that the forms gives me an error saying the field does not exist. It's like it is trying to update one table with the other tables fields.
As an example Say I have one forms that is for people table and another one that is the Jobs table.
When I go to update the people table, it sends the data for the jobs fields too I get an error like the field job description does not exist.
I have two different forms for these and everything.
I would hope it is possible to have two forms that update different tables on the same page.I was trying the exact same thing, but i managed to work round it by setting the steps to hide one region, so the user would enter the form see one region submit the region then direct back to the same page with the different region visible and the original hidden.
I don't know if this would be acceptable for you... -
Confused on syntax-combine two queries
I have two queries that I'm trying to combine, but can't figure out how to combine them ... successfully!?! The first query is pretty simple in that I'm looking at several fields from two different tables, no big deal.
The second query calculates the years, months, days between two dates that are used in the first query. I'm stumped on how to combine the queries so that they place nice with each other and return results.
Here's the first query ...
select
RTRIM(RTRIM(vpi.LastName) + ', ' + RTRIM(ISNULL(vpi.FirstName,''))) Employee,
convert(varchar,vpi.FromEffectiveDate,101) PositionStart,
convert(varchar,vpi.ToEffectiveDate,101) PositionChange,
convert(varchar,vpi.PositionStartDate,101) PositionStartDate,
vpi.PositionReason, vpi.PositionCode, vpc.PositionCodeDescription
from vhrl_positioninfo vpi
inner join position_codes vpc on vpi.PositionCode = vpc.PositionCode
Here's the second query ...
select
[Age] = convert(varchar, [Years]) + ' Years ' +
convert(varchar, [Months]) + ' Months ' +
convert(varchar, [Days]) + ' Days', *
from
select
[Years] = case when BirthDayThisYear <= Today
then datediff(year, BirthYearStart, CurrYearStart)
else datediff(year, BirthYearStart, CurrYearStart) - 1
end,
[Months]= case when BirthDayThisYear <= Today
then datediff(month, BirthDayThisYear, Today)
else datediff(month, BirthDayThisYear, Today) + 12
end,
[Days]= case when BirthDayThisMonth <= Today
then datediff(day, BirthDayThisMonth, Today)
else datediff(day, dateadd(month, -1, BirthDayThisMonth), Today)
end,
Birth = convert(varchar(10) ,Birth, 121),
Today = convert(varchar(10), Today, 121)
from
select BirthDayThisYear =
case when day(dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth)) <> day(Birth)
then dateadd(day, 1, dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth))
else dateadd(year, datediff(year, BirthYearStart, CurrYearStart), Birth)
end,
BirthDayThisMonth =
case when day(dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth)) <> day(Birth)
then dateadd(day, 1, dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth))
else dateadd(month, datediff(month, BirthMonthStart, CurrMonthStart), Birth)
end,
from
select BirthYearStart = dateadd(year, datediff(year, 0, Birth), 0),
CurrYearStart = dateadd(year, datediff(year, 0, Today), 0),
BirthMonthStart = dateadd(month, datediff(month, 0, Birth), 0),
CurrMonthStart = dateadd(month, datediff(month, 0, Today), 0),
from
select birth = convert(datetime, fromeffectivedate) ,
Today = case when convert(datetime, toeffectivedate) = '3000-01-01'
THEN convert(datetime, convert(int,getdate()))
else vpi.toeffectivedate
end
from vHRL_PositionInfo vpi inner join position_codes vpc
on vpi.PositionCode = vpc.PositionCode
) aaaa
) aaa
) aa
)a
Here's the sample data ...
vpi table ...
LastName FirstName FromEffectDate ToEffectDate PosStartDate PosReason PosCode
Doe John 2001-10-15 3000-01-01 10-15-2001 Transfer OperPack
Smith Tom 1994-11-28 2001-10-14 1994-11-28 New Hire OperDC
vpc table ...
PosCode PosDescription
OperPack Pack Line Operator
OperDC Descaler Operator
This is what the results should look like ...
John, Doe 2001-10-15 3000-01-01 10-15-2001 Transfer OperPack Pack Line Operator 6 Years 11 Months 16 Days
John, Doe 1994-11-28 2001-10-14 1994-11-28 New Hire OperDC Descaler Operator 6 Years 6 Months 19 Days
I know the date calculation piece adds 5 additional fields to the end, but they are not needed for the final report. Any help would be greatly appreciated! Thank you! JenaYour query suggests you're not using Oracle. Please post in your relevant database forum.
-
Combining two queries in a join
SQL> desc messages;
Name Null? Type
MESSAGEID NOT NULL NUMBER
TITLE NOT NULL VARCHAR2(50)
AUTHOR VARCHAR2(20)
BODY NOT NULL VARCHAR2(4000)
BOARD NUMBER
THREAD NOT NULL NUMBER
DATE_CREATED NOT NULL DATE
SQL>
I'm trying to combine both queries outlined below. The first query
selects the very first message created in the messages table. It does
this by checking whether thread=0. If it is that means it started a message.
The second query checks the number of replies to the thread above.
The replies to the above message
will have a thread value the same as the above messageid.
That is how a reply is identified.
I am trying to do both queries in one so that the output has
the starting message first with the name
of the person who created the new thread(author), date_created, etc....below that
then is the
number of replies to the message,the author of each reply and the date....
I'm using oracle 8i so i cant use the join key word...
any ideas would be appreciated.
----selects message that started thread---------------------------------
select b.title,b.boardid,m.messageid,m.title,m.author,
m.date_created,m.body
from messages m, boards b where b.boardid=m.board and m.thread=0 and b.boardid='198'
and m.messageid='241';
Thread title Author Starting message Last post
Austrailia noel Austrailia 04/01/2005 21:22:35
-----selects replies to the above message-----------
select author,date_created,body
from messages
where board=198 and thread=241;
AUTHOR DATE_CREATED BODY
noel 05-JAN-05 Oz is played on clay
noel 05-JAN-05 Oz played on grass
noel 05-JAN-05 Oz played on grassThis is a duplicate post of the following thread:
URGENT: combining two sql statements -
Check two columns and update other table
HI ,
I have a table called trackCenterline .Below is the table.
What i want to do is If the segmentSequenceID is 1 it should pick the corresponding SegmentID i.e 10001 and Check for the same segment id in other table called TrackSegment which is below. and pick the BeginMilepost of that segmentID and Update That
Milepost in a new table .At end SegmentSequenceID number it should pick ENDMilepost and update
TrackCenterline table.
TrackSegment table
In the below table for 10001 SegmentID it should pick BeginMilepost. For end Number of SegmentSequenceID in above table ID ends at 121 for that end sequenceID It should refer TrackSegment table below and pick EndMilepost and should be updated in another
table Milepost column.
after that a new segment starts with new sequence .and so on ...
bhavanaHi Deepa_Deepu,
According to your description, since the issue regards T-SQL. I will help you move the question in the T-SQL forums at
http://social.technet.microsoft.com/Forums/en-US/home?forum=transactsql. It is appropriate and more experts will assist you.
When you want to check two columns from two tables then return some results and update the third table. I recommend you use join function and combine two tables, then use update select from statement for modifying the Mailpost table. You can refer to the
following T-SQL Statement.
-----using join to connect to two tables
select TrackCenterline.FeatureId,TrackCenterline.SegmentId,
TrackCenterline.SegmentSequenceId,TrackSegment.BeginMilepost,TrackSegment.EndMilepost
from dbo.TrackCenterline join dbo.TrackSegment
on TrackCenterline.SegmentId=TrackSegment.SegmentId
order by TrackCenterline.SegmentId, TrackCenterline.SegmentSequenceId
---the result shows as following.
FeatureId SegmentId SegmentSequenceId BeginMilepost EndMilepost
AMK100011 10001 1 61.0000 61.3740
AMK100012 10001 2 61.0000 61.3740
AMK100013 10001 3 61.0000 61.3740
AMK1000121 10001 121 61.0000 61.3740
AMK100021 10002 1 61.1260 61.7240
AMK100023 10002 3 61.1260 61.7240
AMK100033 10003 3 61.3740 62.9530
-----Then you can use update select from statement to modify the Mailpost table, Or you can post the table structure of Mailpost
And for more information, you can review the following article about update statement.
http://www.techonthenet.com/sql/update.php
Regards,
Sofiya Li
Sofiya Li
TechNet Community Support -
I have two queries and I would like to output the results as
one and sort by Name ASC
Any help would be great thanksEither use union in your SQL query or use Coldfusion to
combine exisitng queries and create a new query resultset.
Something like this:
<CFQUERY NAME="getDetailsQuery1"
DATASOURCE="#DSNNAME#">
SELECT * FROM getDetailsA
</CFQUERY>
<CFQUERY NAME="getDetailsQuery2"
DATASOURCE="#DSNNAME#">
SELECT * FROM getDetailsB
</CFQUERY>
<CFQUERY NAME="getDetails" DBTYPE="query">
SELECT * FROM getDetailsQuery1
UNION
SELECT * FROM getDetailsQuery2
ORDER BY Name ASC
</CFQUERY>
Hope this helps!
Cheers / Manu. -
Query 1 show me a list of users, each user should tell me how many orders, how many row, and total amount they placed <b>for a certain date</b>.
Query 2 show me again a list of user, each user should me how many orders and how many row they placed <b>excluding that date above that are still open</b>.
I would like these two queries to show on one report. Is it possible?
Query 1:
SELECT COUNT(DISTINCT T0.UserSign) AS 'Today_order',
COUNT(T1.PickStatus) AS 'Today_row', SUM(T1.Price *
T1.OpenQty) AS 'Today_amount' FROM [dbo].[RDR1] T1
INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry = T0.DocEntry
WHERE <b>T0.DocDate = CONVERT(DATETIME, '20051109', 112)</b>
AND T1.OpenQty > 0
Query 2:
SELECT COUNT(DISTINCT T0.UserSign) AS 'Other_order',
COUNT(T1.PickStatus) AS 'Other_row' FROM [dbo].[RDR1]
T1 INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry =
T0.DocEntry WHERE T1.OpenQty > 0 AND <b>T0.DocDate <>
CONVERT(DATETIME, '20051109', 112)</b>Hi Laura,
It's probably easiest to use temporary tables or a cursor for this query.
This code works for me (in SQL Query Analyzer and SBO):
set nocount on
create table #users (UserSign int, UserName nvarchar(30))
create table #today (UserSign int, Today_row int, Today_amount numeric(15, 2))
create table #other (UserSign int, Other_row int)
insert into #users
select T0.USERID, T0.U_NAME from OUSR T0
insert into #today
SELECT DISTINCT T0.UserSign AS 'Today_order',
COUNT(T1.PickStatus) AS 'Today_row', SUM(T1.Price *
T1.OpenQty) AS 'Today_amount'
FROM [dbo].[RDR1] T1
INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry = T0.DocEntry
WHERE T0.DocDate = CONVERT(DATETIME, '20051109', 112)
AND T1.OpenQty > 0
GROUP BY T0.UserSign
insert into #other
SELECT DISTINCT T0.UserSign AS 'Other_order',
COUNT(T1.PickStatus) AS 'Other_row' FROM [dbo].[RDR1]
T1 INNER JOIN [dbo].[ORDR] T0 ON T1.DocEntry =
T0.DocEntry WHERE T1.OpenQty > 0 AND T0.DocDate <>
CONVERT(DATETIME, '20051109', 112)
GROUP BY T0.UserSign
set nocount off
select T0.UserSign, T0.UserName, isnull(T1.Today_row, 0) as Today_row,
isnull(T1.Today_amount, 0) as Today_amount,
isnull(T2.Other_row, 0) as Other_row
from
#users T0 left outer join #today T1 on T0.UserSign = T1.UserSign
left outer join #other T2 on T0.UserSign = T2.UserSign
drop table #users
drop table #today
drop table #other
I've added the user name from the OUSR table to make the query a bit easier to understand.
Hope this helps,
Owen -
How to Combine two queries into One
Hi Gurus,
SQL> select * from v$version;
BANNER
Oracle Database 10g Release 10.2.0.4.0 - 64bit Production
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
SQL>
I have following two queries. How I can convert these two queries into one with the same result ?
Q1
SELECT id,vdate,vendorname,venddate,vid
FROM VENDOR
WHERE processid=32
AND venddate is null
AND vid in(select vid from view_vid where type='PDX')
AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=55)
AND (id,vdate) in (select id,vidate from market_data where marketed_date is null)
AND id not in (select id from location)
Q2
SELECT id,vdate,vendorname,venddate,vid
FROM VENDOR
WHERE processid=20
AND venddate is null
AND vid in(select vid from view_vid where type='PBX')
AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=40)
AND (id,vdate) in (select id,vidate from market_data where region_date is null)
AND id not in (select id from location)
I can UNION these two queries, but, is there any way I can write a single query with which gives me same result as above two queries?
Any help would be highly appreciated
Thanks in advanceHi,
You can do something like this, which will be more efficient than a UNION:
SELECT id, vdate, vendorname, venddate, vid
FROM vendor
WHERE ( ( -- Conditions that apply only to q1
processid = 32
AND vid IN (SELECT vid FROM view_vid WHERE type = 'PDX')
AND (id,vdate) NOT IN ( SELECT id, vdate
FROM vkfl_is
WHERE task_id = 55
AND (id,vdate) IN ( SELECT id, vidate
FROM market_data
WHERE marketed_date IS NULL
OR ( -- Conditions that apply only to q2
processid = 20
AND vid IN (SELECT vid FROM view_vid WHERE type = 'PBX')
AND (id,vdate) NOT IN ( SELECT id, vdate
FROM vkfl_is
WHERE task_id = 40
AND (id,vdate) IN ( SELECT id, vidate
FROM market_data
WHERE region_date IS NULL
-- The remaining conditions apply to both q1 and q2
AND venddate IS NULL
AND id NOT IN (SELECT id FROM location)
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
See the forum FAQ: https://forums.oracle.com/message/9362002 -
Combining two queries in one..
Hi,
Can someone tell me how to combine follwoing two queries in one so that it will increase performance..
SELECT single PLNNR from AFKO
into T1
WHERE AUFNR = '007200000100'.
SELECT single STATU from PLKO
into T2
WHERE PLNNR = T1.using subqueries it is possible
select single statu from plko into t2 where plnnr in (select single plnnr from afko where aufnr eq '007200000100').
read about subqueries at this link...
http://help.sap.com/saphelp_erp2004/helpdata/en/dc/dc7614099b11d295320000e8353423/frameset.htm
regards,
PJ -
How to combine two queries in one EXCEL
hi i got one requirement
i got one excel sheet from user which contains two report s(hoe we know whether it is worknook or not) of 2011 data and now user wants 2012 data .now i icluded jan 2012 to dec 2012 in two reports and sent to user.But user is asking these two reports want to see in one sheet.
so how can i proceed: if i go for work book how can i include these two reports in workbook and here my doubt is if i create a workbook based on these two querie which name i have to give user.Please help me out on the same.
Regards,
Madhu.Hi Madhu
Run one query. Click on a empty space in the excel
From the BEx Design Toolbar -
>Insert Analysis Grid -
> Right click on the Design Item
Edit the DataProvider.....You can ran same or different query with selection.
Save the result
Check this link for detail....I am also searching for something with screenshot
Regards
Anindya
Edited by: Anindya Bose on Feb 14, 2012 3:04 AM -
How can I combine two queries ? QoQ does not work
I have one query where I just count the total qty coming in per month, something like:
<cfquery name="qryIn" datasource="dbname">
select count(orderNO) as totalIN,month
where status = "IN"
group by month
</cfquery>
I then have a second query to count the total qty going out per month
<cfquery name="qryOut" datasource="dbname">
select count(orderNO) as totalOut,month
where status = "OUT"
group by month
</cfquery>
I then use QoQ to combine both:
<cfquery="qryTotal" dbtype="query">
select
totalIN,
totalOUT
from qryIN,qryOUT
where qryIN.month = qryOUT.month
</cfquery>
The problem I am running into is that QoQ does not allow LEFT JOIN, so that if the month is in one query but not the other, it will not pick up that record. And that is throwing off my counts.
How can I combine both queries, and bypass QoQ to get a qty IN and qty Out value, per month ? and, for example, if qty in exists for one month and qty Out does not exists for that month, then qty Out will be zero for that month.
I need this data to plot a chart.
Thanks for any help provided.Do it in a single query to your database. Here is part of it.
select month
, sum(case when when status = "IN" then 1 else 0 end) total_in -
Updating lookup table using merge query
Hi Experts,
My requirement is, we have total 4 tables called x,y,z and a_lookup table. join column between all these tables is deptno.
I need to update a_lookup table based on below conditions
condition1 : Update a_lookup table with matched records of X and Y
condition2: If there is any record in X is not matching with Y, then we need to compare with Z and update the a_lookup table accordingly
Here is the table scripts and my attempt as well.
Only doubt is, is my MERGE statement looks fine or is there any other better way to update the a_lookup table ?
Please share your thoughts on this.
create table x(empno number, deptno number);
-- sample data
insert into x
select level, level * 10 from dual connect by level <= 10;
create table y(empno number, deptno number);
-- sample data
insert into y
select level, level * 10 from dual connect by level <= 5;
create table z(empno number, deptno number);
-- sample data
insert into z
select level, level * 10 from dual connect by level <= 10;
create table a_lookup(empno number, deptno_lookup number);
-- sample data
insert into a_lookup
select null, level * 10 from dual connect by level <= 10;
-- Merging records into a_lookup based on X,Y,Z. Used right outer join on X and Y
merge into a_lookup a
using ( (select x.deptno,x.empno from x,y where x.deptno=y.deptno)
union all
(select z.deptno,z.empno from z, (select x.deptno from x,y where x.deptno=y.deptno and y.deptno is null) res1
where z.deptno = res1.deptno)
) res
on( a.deptno_lookup = res.deptno)
when matched then
update set a.empno = res.empno;
Cheers,
Suri ;-)Assuming empno is unique in X, Y and Z:
merge
into a_lookup a
using (
select nvl(y.empno,z.empno) empno,
x.deptno
from x,
y,
z
where y.deptno(+) = x.deptno
and z.deptno(+) = x.deptno
) b
on (
a.deptno_lookup = b.deptno
and
b.empno is not null
when matched
then
update
set a.empno = b.empno
10 rows merged.
SCOTT@orcl > select * from a_lookup;
EMPNO DEPTNO_LOOKUP
1 10
2 20
3 30
4 40
5 50
6 60
7 70
8 80
9 90
10 100
10 rows selected.
SCOTT@orcl >
SY. -
Updating a table with a query that return multiple values
Hi,
I'm trying to update a table which contain these fields : ItemID, InventoryID, total amounts
with a query that return these values itemId, inventoryid and total amounts for each items
Mind you, not all the rows in the table need to be updated. only a few.
This what i wrote but doesn't work since the query return multiple values so i can't assign it to journalAmounts.
UPDATE [bmssa].[etshortagetemp]
SET JournalAmounts = (SELECT sum(b.BomQty) FROM [bmssa].[Bom] b
JOIN [bmssa].[SalesLine] sl ON sl.ItemBomId = b.BomId
JOIN [bmssa].[SalesTable] st ON st.SalesId = sl.SalesId
WHERE st.SalesType = 0 AND (st.SalesStatus IN (0,1,8,12,13)) AND st.DataAreaId = 'sdi'
GROUP BY b.itemid, b.inventdimid)
Any advise how to do this task?Remember that link to the documentation posted above that explains exactly how to do this. When you read it which part exactly were you having trouble with?
-
Want to combine the data different coloumn of table into single coloumn.
HI All ,
Requirement : I want to create an application which will display diffrent coloumn(data) of a table into single coloumn in web-dynpro abap .
For ex:
Table : employee
coloumn : PERSON , PAYMENT , STATUS , SUBMISSION .
RETURN = PAYMENT + STATUS + SUBMISSION
(return coloumn will contain the data of other coloumn )
Thanks in advance .
RahulIn your context, create a node 'Employee'. It should have the attributes person, payment, status, submission, return. Let return be of type string. I assume you have a ztable or some ddic structure employee, which does not have the return field.
You code will be on these lines - change it to your requirements anyway. Place the code in a method depending on your requirement of when the data should be displayed.
data: node_emp type ref to if_wd_context_node,
itab_emp type table of employee,
wa_emp type employee,
itab_node_emp type if_main=>elements_employee,
wa_node_emp type if_main=>element_employee.
node_emp = wd_context->get_child_node( name = 'EMPLOYEE' ).
<i>* Get data from your API here into itab_emp *</i>
loop at itab_emp into wa_emp.
move-corresponding wa_emp to wa_node_emp.
concatenate wa_emp-payment wa_emp-status wa_emp-submission separated by space into wa_node_emp-return.
append wa_node_emp to itab_node_emp.
endloop.
node_emp->bind_elements( itab_node_emp ).
Here, before the loop, you need to fetch the data into itab_emp using your APIs. Since you have said you want to display the data of multiple columns as is, I have just concatenated them.
In your view layout, have a table and bind the data source to be the context node Employee.
<b>The data types of itab_node_emp, wa_node_emp will differ based on your view name and node name. Use the code wizard to generate your code according to your design time.</b>
Hope this helps.
Regards,
Nithya
Maybe you are looking for
-
Jtree node refuses to collapse upon clicking handle; makeVisible() was used
Hello, While creating a new node and inserting as a leaf in the JTree, I use tree.makeVisible(newTreePath) to expand the new node and make visible. (Using expandPath() will not expand if a leaf was added). However now the jtree node refuses to collap
-
24p question dealing with capturing
friend shoots movie in 24P. I get to edit movie. as far as I know, 24P should be captured at 29.97 and FCP makes the adjustments. so. when I capture, I get some firece tracers/zebra stripes on the video. I've tried it 3 different ways and everytime,
-
Access manager policyagent 2.1 fro webspher5.0 with sun access manager in
Help It is very urgent I have installed my sun access manager and sun direcory server on same machine solaris10.SSL is diable in directory server.Access manager working on ssl mode means it is working on Http with port 80 and Https with port443.Acces
-
Why do I keep getting app error messages saying to download SW Player 10
After downloading Shockwave player 10 at Adobe's suggestion, I now constantly get an application error stating "This application requires SW Player 10 which is not installed. Click OK to down load" In fact, I downloaded it and now have downloaded the
-
HT1338 Can't upload Mac OS 10.6.8
Hello, I have a 13' aluminum body macbook from the early 2008, and I was trying to find the download for the new Mac OS 10.6.8 and for some reason my regular software updates don't show any updates available. After going online to find the update, th