What SQL Query will be useful on this? ( ORACLE) - sql

I want to get the value of column and set it to the other column but this is the challenge to me.
Both are in the same table
DATA 1 DATA 2
Prev.No = 789 App.No = 789
UserRefNo. = 23 RenewdNo = ?
Question is: what query will I use to set the RenewdNo of Data 2 from the UserRefNo of data 1?
There will be a lot of rows to update.
Expected results:
Data 2
App.No = 789
RenewdNo = 23

You need an update query like:
Update d2
set d2.RenewdNo = d.UserRefNo
from Data2 d2
inner join Data1 d on d.Prev_no=d2.Appno

Related

sum of query result form two different table that have nothing in common

hi i have two query result:
Table A
Id u
1 50,00
2 60,00
3 70,00
and
Table B
id c
4 110,00
5 120,01
6 130,02
Now i have doing two query on this table and i want sum their query result.
I want update column c from table B with 160 that is sum of(110+50).
Table B
Id c
4 160,00
Table B and Table A they have nothing in common.
Now i have doing two query for select their value ed for sum two data:
$data=number_format($row['c']+$row1['u']);
$query_updatee="update B set c= (integer)$data where c=110,00";
Can i sum the data from two different table that don't have nothing in comon?
My output is pg_query(): Query failed: ERRORE: syntax error at or near "1" LINE 1: update B set punti = (integer)1680,00 where c=110,00 ^ in C:\xampp\htdocs\table_A.php on line 81
You can use a subquery to fetch the increment. The rest seems to just be filtering:
update B
set c = c + (select a.u from a where a.id = 1)
where B.id = 4;
Obviously, you can use where b.c = 110.00 if you want to filter by a number (or use a comma if that is how the database is set up).

SQL: update specific rows based on a condition

So i have two tables as the following:
- T_Sample
ws_id|date|depth|number_l
| | |
and
- T_Sample_value
ws_id|parameter|value
| |
I have some rows in the T_sample table, which have negative depth values and in the T_sample_value table they have some data; what i am trying to do is for these rows i would like to copy their data (present in T_sample_value) in the row which of T_sample which has 0 depth value.
I tried to do an update set query with subqueries but i get the error that the subquery does return multiple rows and cannot update the fields. What i tried looks pretty much like this:
UPDATE T_sample_value
SET T_sample_value.ws_id = (select blah blah where depth is <0)
WHERE T_sample_value.ws_id = (select blah blah where depth is = 0)
You would like to do a update-join like
UPDATE a
SET depth = b.value
FROM T_Sample a
JOIN T_Sample_value b
ON a.ws_id = b.ws_id
WHERE a.depth = 0;

Join two file data in pig to the output in a desired format

File 1 has the data:
Name ID
-------
Mark 1
Gary 2
Robert 3
File 2 has the data:
ID result
----------
1 success
2 Fail
3 success
I loaded the data into two variables a & b now I want to join the data in based on ID for which result is success. I am able to join but I am getting the data in an improper format.
a = load '/file1' as (Name:chararray,ID:int);
b = load '/file2' as (ID:int,result:chararray);
c = join a by a2, b by b1;
When I dump c I am getting the output in the format of (name,id,id,result)... How I need join a & b such that I can get the output in the format of (name,id,result)
You can't. What you have to do is project the fields that you want to keep using a FOREACH. You can do something like this:
D = FOREACH C GENERATE a::Name as Name, a::ID as ID, b::result as result ;
You can filter b before joining.
a = load '/file1' as (Name:chararray,ID:int);
b = load '/file2' as (ID:int,result:chararray);
z = FILTER b BY b2 == 'success';
Then join a and z.
c = join a by a2, z by b1;
Later you need to do something as mentioned by #m2ert in previous answer.

SQL update statement from a history table based on timestamp

I'm trying to write an update statement in Oracle that will find an attribute from a history table based on a timestamp. So, for example the data looks like:
TABLE A
A_ID TIMESTAMP ATTR
---------------------------------
1 5/27/2012 10:30:00 AM ?
TABLE B
B_ID A_ID TIMESTAMP ATTR
---------------------------------------
1 1 5/26/2012 9:01:08 AM W
2 1 5/27/2012 8:38:21 AM X
3 1 5/28/2012 9:01:01 AM Y
4 1 5/29/2012 11:37:54 PM Z
The lower bound is >= B.TIMESTAMP, but I'm not sure how to write the upper bound as < B."the next TIMESTAMP". So, in the example above the attribute on table A should update to "X".
This seems like a fairly common use case. I've seen this post, but it looks like a satisfactory answer was never reached, so I thought I'd post again.
UPDATE A SET attr = (
SELECT b1.attr
FROM B b1
INNER JOIN (
SELECT MAX(b3.timestamp) mx FROM B b3
WHERE b3.timestamp < A.timestamp
) b2 ON b1.timestamp = b2.mx
)
I can't remember if Oracle will allow me to use table A within the inner join sub query... Would you mind trying it?

Mysql query to fetch data

i have a table "request" with 4 columns namely:
1.recId :long primary key
2.interactionId:long
3.requestedBy:boolean
4.requestedType:boolean
and data is as follows:
VALUES
(185,455699,0,5),
(186,455746,0,1),
(187,455746,1,1),
(188,455752,0,1),
(189,455753,0,1),
(190,455753,1,1),
(191,455754,1,1)
i want a query to fetch all the rows where interactionId is same and having requestedBy both 1 and 0 values and requestType=1;
regards,
Nihar
Your question was a little difficult to understand. I'm assuming you meant this:
I want a query to fetch all pairs of rows from the request table where:
the interactionId is same in both rows
requestType = 1 for both rows
requestedBy is 1 in one row and 0 in the other row
If so, then try this:
SELECT *
FROM request T1
JOIN request T2
ON T1.interactionId = T2.interactionId
AND T1.requestType = 1
AND T2.requestType = 1
AND T1.requestedBy = 0
AND T2.requestedBy = 1