How to update rows in BigQuery based on value in repeated field - sql

I need to update the values in a repeated field for certain rows in a BigQuery table. The rows to be updated are selected based on values in the repeated field. I can't figure out how to do accomplish this. For example, let's say I have a schema like this:
move_id: integer
color: string
moved_quants: repeated
moved_quants.quantity = integer
moved_quants.value = float
And the following data:
My goal is to update the value of moved_quants.value to 0.6 * the quantity for any row that contains at least one moved_quants.value = 0. Previously, I had to perform this same task based on the color, and that was relatively simple:
UPDATE
`testing`
SET
moved_quants = ARRAY(
SELECT AS STRUCT * REPLACE(0.6 * quantity AS value)
FROM
UNNEST(moved_quants)
)
WHERE color = 'red'
However, now I need to update the rows that have at least one moved_quants.value = 0 and I can't figure out how to do this. Can anyone help?

I think I may have figured it out:
UPDATE
`testing`
SET
moved_quants = ARRAY(
SELECT AS STRUCT * REPLACE(0.6 * quantity AS value)
FROM
UNNEST(moved_quants)
)
WHERE
ARRAY_LENGTH(ARRAY(
(SELECT AS STRUCT * FROM UNNEST(moved_quants) WHERE inventory_value = 0)
)) > 0
Is that right?

Related

Why I Can't show only row who has max value in Sql Server?

This is my query :
WITH TABLE as (
SELECT distinct STJH.[META_CODE_ORIGINE] AS Salle_TMD_JOUR_HISTORIQUE_ID
,STJH.[STJH_DATE_ACTION]
,STJH.[STJH_TYPE_ACTION]
, STH.ST_ID,
STJH.[STJ_JOUR]
,STJH.[STJ_HEURE_DEBUT]
,STJH.[STJ_HEURE_FIN]
,STH.[META_CODE_ORIGINE] AS SALLE_TMD_HISTORIQUE_ID
,STH.[ST_DATE_DEBUT]
,STH.[ST_DATE_FIN]
,STH.[STH_DATE_ACTION]
,MAX (sth.STH_DATE_ACTION) over (partition by STH.[META_CODE_ORIGINE]) as max_date_action,
FROM [BI_EASILY].[bloc].[D_TMD_JOUR_HISTORIQUE] STJH
left JOIN [BI_EASILY].[bloc].[D_TMD_HISTORIQUE] STH ON STJH.[ST_ID] = STH.[ST_ID]
WHERE STJH.[STJH_TYPE_ACTION] <> 3
AND STJH.ST_ID = 37 and STJH.ST_ID = 37
AND STJ_JOUR = 1 )
select * from table where table.max_date_action <= stjh_date_action
This is my result ( sorry i didn't copy manualy a lot of data)
I want to show only last row whose has max value of STJH_Date_Action. When i will remove my filter « Where STJ_JOUR =1 (Monday). I want to show only one row having STJ_JOUR_VALUE = 1. I tried to do this by this condition « where max_date_action< = STJH_DATE_ACTION » but it doesn’t work. Can you help me please.
for all the records in the result (max_date_action<=stjh_date_action) as you partition the data by (STH.[META_CODE_ORIGINE])

Complex INSERT INTO SELECT statement in SQL

I have two tables in SQL. I need to add rows from one table to another. The table to which I add rows looks like:
timestamp, deviceID, value
2020-10-04, 1, 0
2020-10-04, 2, 0
2020-10-07, 1, 1
2020-10-08, 2, 1
But I have to add a row to this table if a state for a particular deviceID was changed in comparison to the last timestamp.
For example this record "2020-10-09, 2, 1" won't be added because the value wasn't changed for deviceID = 2 and last timestamp = "2020-10-08". In the same time record "2020-10-09, 1, 0" will be added, because the value for deviceID = 1 was changed to 0.
I have a problem with writing a query for this logic. I have written something like this:
insert into output
select *
from values
where value != (
select value
from output
where timestamp = (select max(timestamp) from output) and output.deviceID = values.deviceID)
Of course it doesn't work because of the last part of the query "and output.deviceID = values.deviceID".
Actually the problem is that I don't know how to take the value from "output" table where deviceID is the same as in the row that I try to insert.
I would use order by and something to limit to one row:
insert into output
select *
from values
where value <> (select o2.value
from output o2
where o2.deviceId = v.deviceId
order by o2.timestamp desc
fetch first 1 row only
);
The above is standard SQL. Specific databases may have other ways to express this, such as limit or top (1).

How to replace a value in one field by a value from another field (different column) within the same view table (SQL)?

I'd like to know if it is possible to replace a value from one field by using a value from another column and a different row.
For Example, click this to view the table image.
I'd like the SumRedeemed value 400 in row 15 to be replaced by the value -1*(-395); the value -395 comes from the EarnPointsLeft in row 6 (both have the same CID meaning that they are the same person). Any suggestions?
You need this update statement:
update t
set t.sumredeemed = (-1) * (select earnpointsleft from FifoPtsView where cid = t.cid)
from FifoPtsView t
where t.cid = 5000100008 and t.earnpointsleft = 0
This will work if the select statement will return only 1 row.
you can simply update your table
update t
set t.sumredeemed = ABS(t2.earnpointsleft )
from FifoPtsView t join FifoPtsView t2 on t.cid = t.cid and isnull(t2.earnpointsleft,0)>0
if you want negative values you can remove ABS ,
please give me your feedbacks

update column with numbers in sqlite table

This should be very simple but I cannot figure out how to do it. I would like to modify the values of two different columns. One from 1 to the total number of rows and the other one from the total of rows to one (basically increasing and decreasing number). I tried:
start = 0
end = number_of_rows + 1
c.execute('SELECT * FROM tablename')
newresult=c.fetchall()
for row in newresult:
start += 1
end -= 1
t = (start,)
u = (end,)
c.execute("UPDATE tablename SET Z_PK = ?", t) ---> this will transform all rows with Z_PK since there is no where statement to limit
c.execute("UPDATE tablename SET Z_OPT = ?", u)
The thing is that I don't know how I can add the "where" statement since I have no values I am sure for rows (like IDs number). A possibility would be to return the current row as the argument for "where" but I don't know how to do it...
Without an INTEGER PRIMARY KEY column, your table has an internal rowid column, which already contains the values you want:
UPDATE MyTable
SET Z_PK = rowid,
Z_OPT = (SELECT COUNT(*) FROM MyTable) + 1 - rowid

SQL, return a row multiple times from single table based on variable

I'm creating some box labels using iReport and need to multiply out the data for each box label.
I have 2 parameters, #id for the record id and #typ for the package type required
SELECT
dr_id,
dr_to_customer,
dr_company_name,
dr_address_0,
dr_address_1,
dr_address_2,
dr_address_3,
dr_postcode,
dr_contact,
dr_per_packs,
dr_per_boxes,
dr_per_pallets
FROM delreq
WHERE dr_id = #id AND ??timesTOrepeat?? = #typ (a string)
I need to return each row x times based on the qty's in either packs, boxes or pallets.
So, I need to first select the qty based on #typ then work out how to multiply out the rows.
Help.
You can try to achieve this using outer join. Try this:
SELECT
dr_id,
dr_to_customer,
dr_company_name,
dr_address_0,
dr_address_1,
dr_address_2,
dr_address_3,
dr_postcode,
dr_contact,
dr_per_packs,
dr_per_boxes,
dr_per_pallets
FROM delreq a, (select -1 id from all_objects where rownum < #typ) b
WHERE dr_id = #id AND a.dr_id(+) = b.id;
I am assuming here that dr_id will not have value -1 also assuming db is oracle [not sure abut other DB.