SQL: update specific rows based on a condition - sql

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;

Related

How to combine two queries where one of them results in an array and the second is the element place in the array?

I have the following two queries:
Query #1
(SELECT ARRAY (SELECT (journeys.id)
FROM JOURNEYS
JOIN RESPONSES ON scenarios[1] = responses.id) AS arry);
This one returns an array.
Query #2:
SELECT (journeys_index.j_index)
FROM journeys_index
WHERE environment = 'env1'
AND for_channel = 'ch1'
AND first_name = 'name1';
This second query returns the element index in the former array.
How do I combine the two to get only the element value?
I recreated a simpler example with a table containing an array column (the result of your first query)
create table my_array_test (id int, tst_array varchar[]);
insert into my_array_test values (1,'{cat, mouse, frog}');
insert into my_array_test values (2,'{horse, crocodile, rabbit}');
And another table containing the element position for each row I want to extract.
create table my_array_pos_test (id int, pos int);
insert into my_array_pos_test values (1,1);
insert into my_array_pos_test values (2,3);
e.g. from the row in my_array_test with id=1 I want to extract the 1st item (pos=1) and from the row in my_array_test with id=2 I want to extract the 3rd item (pos=3)
defaultdb=> select * from my_array_pos_test;
id | pos
----+-----
1 | 1
2 | 3
(2 rows)
Now the resulting statement is
select *,
tst_array[my_array_pos_test.pos]
from
my_array_test join
my_array_pos_test on my_array_test.id = my_array_pos_test.id
with the expected result
id | tst_array | id | pos | tst_array
----+--------------------------+----+-----+-----------
1 | {cat,mouse,frog} | 1 | 1 | cat
2 | {horse,crocodile,rabbit} | 2 | 3 | rabbit
(2 rows)
Now, in your case I would probably do something similar to the below, assuming your 1st select statement returns one row only.
with array_sel as
(SELECT ARRAY (SELECT (journeys.id)
FROM JOURNEYS
JOIN RESPONSES ON scenarios[1] = responses.id) AS arry)
SELECT arry[journeys_index.j_index]
FROM journeys_index cross join array_sel
WHERE environment = 'env1'
AND for_channel = 'ch1'
AND first_name = 'name1';
I can't validate fully the above sql statement since we can't replicate your tables, but should give you a hint on where to start from

How to update several rows with postgresql function

I have a table representing an object:
id|field1|field2|field3
--|------|------|------
1 | b | f | z
2 | q | q | q
I want to pass several objects to pg function which will update corresponding rows.
Now I see only one way to do it - to pass a jsonb with array of objects. For example:
[{"id":1, "field1":"foo", "field2":"bar", "field3":"baz"},{"id":2, "field1":"fooz", "field2":"barz", "field3":"bazz"}]
Is this a best way to perform an update? And what is the best way to do it with jsonb input?
I don't really like the way to convert jsonb input to rows with select * from json_each('{"a":"foo", "b":"bar"}') and operating it. I would prefer some way to execute a single UPDATE.
this can be achieved using from clause in update along with a dynamic on the fly table from input as follows assuming that the DB table and custom input will be mapped/matched with each other on basis of ID
update table as t1
set field1 = custom_input.field1::varchar,
field2 = custom_input.field2::varchar,
field3 = custom_input.field3::varchar
from (
values
(1, 'foo', 'bar', 'baz'),
(2, 'fooz', 'barz', 'bazz')
) as custom_input(id, field1, field2, field3)
where t1.id = custom_input.id::int;
You can do it in next way (using json_populate_recordset):
update test
set
field1 = data.field1,
field2 = data.field2,
field3 = data.field3
from (select * from json_populate_recordset(
NULL::test,
'[{"id":1, "field1":"f.1.2", "field2":"f.2.2", "field3":"f.3.2"},{"id":2, "field1":"f.1.4", "field2":"f.2.5", "field3":"f.3.6"}]'
)) data
where test.id = data.id;
PostgreSQL fiddle

SELECT - too many rows

let me show you a simple presentation on database structure:
Database structure
Query's parameters:
-id_category
-id_language
-id_account
I need to select all rows from wordconfigs table where id_wordconfig is diffrent from id_wordconfig stored in hiddenwords table.
For example wordconfigs table cointains:
id_wordconfig = 1 (this row is related to successively: id_account=1
(imporatant that 1), id_language=1, id_category=1)
id_wordconfig = 2 (this row is related to successively: id_account=1
(imporatant that 1), id_language=1, id_category=1)
I pass parameters to query:
-id_category = 1
-id_language = 1
-id_account = 2 (other than assigned to wordconfigs in example)
Example 1
hiddenwords table is empty -> query returns wordconfid with id 1 and 2
Example 2
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=1
query returns also wordconfig with id 1 and 2 (because id_account is diffrent from passed to query)
Example 3
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
query returns just wordconfig with id 2 (because hiddenwords contains id_wordconfig=1 and id_account=2 like passed to query)
Example 4
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
id_hiddenwords = 1; id_wordconfig=2; id_account=1 (different that passed to query)
query returns just wordconfig with id = 2
Example 5
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
id_hiddenwords = 1; id_wordconfig=2; id_account=2
query should nothing returns
I've created a simple query for this task:
SELECT wc.*
FROM categorywordconfig cwc, categories c, languages l, accounts a, wordconfigs wc
LEFT JOIN hiddenwords hw ON hw.id_wordconfig<>wc.id_wordconfig
WHERE wc.id_wordconfig=cwc.id_wordconfig
AND cwc.id_category=c.id_category
AND c.id_language=l.id_language
AND l.id_account=a.id_account
AND c.id_category=1
AND l.id_language=1
AND hw.id_account=2
The given query works for examples from 1 to 4. The Example 5 should nothing returns but with this query it's return all rows (so wordconfig with id 1 and 2) Why is that? What's wrong with it?
Edit:
I've just simple changed query to:
SELECT wc.* FROM categorywordconfig cwc, categories c, languages l, accounts a, wordconfigs wc
WHERE wc.id_wordconfig=cwc.id_wordconfig
AND cwc.id_category=c.id_category AND c.id_language=l.id_language
AND l.id_account=a.id_account AND c.id_category=1 AND l.id_language=1
AND wc.id_wordconfig NOT IN (SELECT id_wordconfig FROM hiddenwords WHERE id_account=2)
And it's working :)

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

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

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