Simple SQLite UPDATE Statement does not work - database

I inserted a new column in my database and want to populate with data. It shall be calculated from the difference of the value of nom_arr_time and the value of nom_dep_time of the previous row.
If I try to execute the SELECT statement only everything works fine. But the update won't work, the column stays empty.
UPDATE mobile_statistik
SET nom_fahrzeit = (SELECT nom_arr_time - LAG(nom_dep_time,1,0) OVER (ORDER BY id)
FROM mobile_statistik);
Handling the task through a programming language like python is not an option because of the size, ca. 20GB of the database.

You can do it like this:
UPDATE mobile_statistik
SET nom_fahrzeit = (
SELECT t.nom_arr_time - t.prev FROM (
SELECT id, nom_arr_time, LAG(nom_dep_time,1,0) OVER (ORDER BY id) prev
FROM mobile_statistik
) t
WHERE t.id = mobile_statistik.id
)
or without LAG():
UPDATE mobile_statistik
SET nom_fahrzeit =
nom_arr_time - (
SELECT m.nom_dep_time
FROM mobile_statistik m
WHERE m.id = (SELECT MAX(t.id) FROM mobile_statistik t WHERE t.id < mobile_statistik.id)
);

Related

Updating DISTINCTROW in SQL Server [duplicate]

What would the syntax be to convert this MS Access query to run in SQL Server as it doesn't have a DistinctRow keyword
UPDATE DISTINCTROW [MyTable]
INNER JOIN [AnotherTable] ON ([MyTable].J5BINB = [AnotherTable].GKBINB)
AND ([MyTable].J5BHNB = [AnotherTable].GKBHNB)
AND ([MyTable].J5BDCD = [AnotherTable].GKBDCD)
SET [AnotherTable].TessereCorso = [MyTable].[J5F7NR];
DISTINCTROW [MyTable] removes duplicate MyTable entries from the results. Example:
select distinctrow items
items.item_number, items.name
from items
join orders on orders.item_id = items.id;
In spite of the join getting you the same item_number and name multiple times when there is more than one order for it, DISTINCTROW reduces this to one row per item. So the whole join is merely for assuring that you only select items for which exist at least one order. You don't find DISTINCTROW in any other DBMS as far as I know. Probably because it is not needed. When checking for existence, we use EXISTS of course (or IN for that matter).
You are joining MyTable and AnotherTable and expect for some reason to get the same MyTable record multifold for one AnotherTable record, so you use DISTINCTROW to only get it once. Your query would (hopefully) fail if you got two different MyTable records for one AnotherTable record.
What the update does is:
update anothertable
set tesserecorso = (select top 1 j5f7nr from mytable where mytable.j5binb = anothertable.gkbinb and ...)
where exists (select * from mytable where mytable.j5binb = anothertable.gkbinb and ...)
But this uses about the same subquery twice. So we'd want to update from a query instead.
The easiest way to get one result record per <some columns> in a standard SQL query is to aggregate data:
select *
from anothertable a
join
(
select j5binb, j5bhnb, j5bdcd, max(j5f7nr) as j5f7nr
from mytable
group by j5binb, j5bhnb, j5bdcd
) m on m.j5binb = a.gkbinb and m.j5bhnb = a.gkbhnb and m.j5bdcd = a.gkbdcd;
How to write an updateble query is different from one DBMS to another. Here is the final update statement for SQL-Server:
update a
set a.tesserecorso = m.j5f7nr
from anothertable a
join
(
select j5binb, j5bhnb, j5bdcd, max(j5f7nr) as j5f7nr
from mytable
group by j5binb, j5bhnb, j5bdcd
) m on m.j5binb = a.gkbinb and m.j5bhnb = a.gkbhnb and m.j5bdcd = a.gkbdcd;
The DISTINCTROW predicate in MS Access SQL removes duplicates across all fields of a table in join statements and not just the selected fields of query (which DISTINCT in practically all SQL dialects do). So consider selecting all fields in a derived table with DISTINCT predicate:
UPDATE [AnotherTable]
SET [AnotherTable].TessereCorso = main.[J5F7NR]
FROM
(SELECT DISTINCT m.* FROM [MyTable] m) As main
INNER JOIN [AnotherTable]
ON (main.J5BINB = [AnotherTable].GKBINB)
AND (main.J5BHNB = [AnotherTable].GKBHNB)
AND (main.J5BDCD = [AnotherTable].GKBDCD)
Another variant of the query.. (Too lazy to get the original tables).
But like the query above updates 35 rows =, so does this one
UPDATE [Albi-Anagrafe-Associati]
SET
[Albi-Anagrafe-Associati].CRegDitte = [055- Registri ditte].[CRegDitte],
[Albi-Anagrafe-Associati].NIscrTribunale = [055- Registri ditte].[NIscrTribunale],
[Albi-Anagrafe-Associati].NRegImprese = [055- Registri ditte].[NRegImprese]
FROM [055- Registri ditte]
WHERE EXISTS(
SELECT *
FROM [055- Registri ditte]-- [Albi-Anagrafe-Associati]
WHERE ([055- Registri ditte].GIBINB = [Albi-Anagrafe-Associati].GKBINB)
AND ([055- Registri ditte].GIBHNB = [Albi-Anagrafe-Associati].GKBHNB)
AND ([055- Registri ditte].GIBDCD = [Albi-Anagrafe-Associati].GKBDCD))
Update [AnotherTable]
Set [AnotherTable].TessereCorso = MyTable.[J5F7NR]
From [AnotherTable]
Inner Join
(
Select Distinct [J5BINB],[5BHNB],[J5BDCD]
,(Select Top 1 [J5F7NR] From MyTable) as [J5F7NR]
,[J5BHNB]
From MyTable
)as MyTable
On (MyTable.J5BINB = [AnotherTable].GKBINB)
AND (MyTable.J5BHNB = [AnotherTable].GKBHNB)
AND (MyTable.J5BDCD = [AnotherTable].GKBDCD)

SQL Update query using select statement

I am trying to update a column in a table where the another column matches and selecting the top 1 for that column as the value to update.
Hard to explain, but this is what I wrote:
UPDATE CameraSpecifications AS a
SET a.Variant = (
SELECT TOP 1 GTIN
FROM CameraSpecifcations
WHERE b.ModelGroup = a.ModelGroup )
Hopefully that explains what I am trying to do.
I have a select statement that might also help:
SELECT
(
SELECT TOP 1 b.GTIN
FROM CameraSpecifications AS b
WHERE b.ModelGroup = a.ModelGroup
) AS Gtin,
a.ModelGroup,
COUNT(a.ModelGroup)
FROM CameraSpecifications AS a
GROUP BY a.ModelGroup
We can try doing an update join from CameraSpecifications to a CTE which finds the top GTIN value for each model group. Note carefully that I use an ORDER BY clause in ROW_NUMBER. It makes no sense to use TOP 1 without ORDER BY, so you should at some point update your question and mention TOP 1 with regard to a certain column.
WITH cte AS (
SELECT ModelGroup, GTIN,
ROW_NUMBER() OVER (PARTITION BY ModelGroup ORDER BY some_col) rn
FROM CameraSpecifications
)
UPDATE cs
SET Variant = t.GTIN
FROM CameraSpecifcations cs
INNER JOIN cte t
ON cs.ModelGroup = t.ModelGroup
WHERE
t.rn = 1;

How to apply order by on Update?

I am trying to update values in Temporary table but before update i want to order by the records on the basis of date.
UPDATE INS
set ins.PrefferedEmail = IC.CntcInfoTxt
From #Insured INS
Inner Join InsuredContact IC
on IC.InsuredId = INS.Insuredid and IC.ExpDt < Getdate() And (INS.InsuredStatus = 'Expired' or INS.InsuredStatus = 'Merged')
Where IC.CntcTypeCd = 'EML' and IC.InsuredId = #InsuredId and MAX(IC.ExpDt) ExpDt
I want to update on the basis of this column IC.ExpDt
Thanks in advance
I think you are confusing an UPDATE with SELECT(ing) the correct data to update with.
I solved this problem with a common table expression (cte) and the rank() function. The cte is a nice way to get a sub-query results. The rank is needed to find the most recent contact info.
-- 1 - Get id, contact text, expired date, with a rank by expired date
-- 2 - Join with table to update, select rank = 1
;
WITH cteRecentContactInfo
AS
(
SELECT
ic.InsuredId,
ic.CntcInfoTxt,
ic.ExpDt,
RANK() OVER (ORDER BY ic.ExpDt DESC) as RankByDt
FROM
InsuredContact as ic
WHERE
ic.CntcTypeCd = 'EML' and ic.ExpDt < getdate()
)
UPDATE ins
FROM #Insured ins INNER JOIN cteRecentContactInfo rci
ON ins.Insuredid = rci.Insuredid and ins.ExpDt = rci.ExpDt
WHERE
(ins.InsuredStatus = 'Expired' OR ins.InsuredStatus = 'Merged') AND
rci.RankByDt = 1
Update and sorting doesn't work that way. The rows are not necessarily stored in any particular order, so sorting and updating are completely independent.
If you only want to update based on MAX(ExpDt) you need a subquery that pulls that up.
UPDATE INS
SET INS.PrefferedEmail = tempOutside.CntcInfo
FROM (SELECT IC.CntcInfo,IC.InsuredID FROM
InsuredContact IC INNER JOIN
(SELECT MAX(IC.ExpDt) AS MaxExpDt,IC.InsuredID
FROM IC
WHERE IC.ExpDt < Getdate()
AND IC.CntcTypeCd = 'EML'
GROUP BY IC.InsuredID) AS tempInside
ON tempInside.InsuredID = IC.InsuredID
AND IC.ExpDt = tempInside.MaxExpDt) AS tempOutside
INNER JOIN INS ON tempOutside.InsuredID = INS.InsuredId
WHERE (INS.InsuredStatus = 'Expired' OR INS.InsuredStatus = 'Merged')
AND INS.InsuredID = #InsuredID
On unrelated notes, for the good of whoever is doing maintenance on your code you might want to consider fixing the spelling errors (e.g. should be Preferred instead of 'Preffered') and giving the tables more meaningful names. Also since you're only working with one ID from #InsuredID you could simplify the code and remove an inner join or two but what I have should work for updating several records at once not just the one selected by #InsuredID.
Thanks for your time and comments. I have done this and its working fine
UPDATE INS
set ins.PrefferedEmail = ICC.CntcInfoTxt
From #Insured INS
Inner Join
(
SELECT
InsuredId,
CntcInfoTxt,
CntcTypeCd
From InsuredContact ICC
Where ExpDt = (select MAX(ExpDt) from InsuredContact where ExpDt < GETDATE() and CntcTypeCd = 'EML' and InsuredId = 10)
) As ICC
on ICC.InsuredId = INS.InsuredId And (INS.InsuredStatus = 'Expired' or INS.InsuredStatus = 'Merged')
Where ICC.InsuredId = #InsuredId

TSQL - While loop within select?

In SQL server
Ok, so I'm working with a database table in which rows can have parent rows, which can then have parent rows of their own. I need to select the root 'row'. I don't know the best way to do this.
There is a field called ParentId, which links the row to the row with that ID. When the ParentId = 0, it is the root row.
This is my query now:
SELECT Releases.Name,WorkLog.WorkLogId
FROM WorkLog,Releases
WHERE
Releases.ReleaseId = WorkLog.ReleaseId
and WorkLogDateTime >= #StartDate
and WorkLogDateTime <= #end
I don't really need the Release Name of the child releases, I want only the root Release Name, so I want to select the result of a While loop like this:
WHILE (ParentReleaseId != 0)
BEGIN
#ReleaseId = ParentReleaseId
END
Select Release.Name
where Release.RealeaseId = #ReleaseId
I know that syntax is horrible, but hopefully I'm giving you an idea of what I'm trying to acheive.
Here is an example, which could be usefull:
This query is getting a lower element of a tree, and searching up to the parent of parents.
Like I have 4 level in my table -> category 7->5, 5->3, 3-> 1. If i give it to the 5 it will find the 1, because this is the top level of the three.
(Changing the last select you can have all of the parents up on the way.)
DECLARE #ID int
SET #ID = 5;
WITH CTE_Table_1
(
ID,
Name,
ParentID
)
AS(
SELECT
ID,
Name,
ParentID
FROM Table_1
WHERE ID = #ID
UNION ALL
SELECT
T.ID,
T.Name,
T.ParentID
FROM Table_1 T
INNER JOIN CTE_Table_1 ON CTE_Table_1.ParentID = T.ID
)
SELECT * FROM CTE_Table_1 WHERE ParentID = 0
something like this
with cte as
(
select id,parent_id from t where t.id=#myStartingValue
union all
select t.id,t.parent_id
from cte
join t on cte.parent_id = t.id where cte.parent_id<>0
)
select *
from cte
join t on cte.id=t.id where cte.parent_id = 0
and with fiddle : http://sqlfiddle.com/#!3/a5fa1/1/0
Using Andras approach, I edited the final select to directly give me the ID of the root release
WITH cte_Releases
(
ReleaseId,
ParentReleaseID
)
AS(
SELECT
ReleaseId,
ParentReleaseID
FROM Releases
Where ReleaseId = 905
UNION ALL
SELECT
R.ReleaseId,
R.ParentReleaseID
FROM Releases R
INNER JOIN cte_Releases ON cte_Releases.ParentReleaseID = R.ReleaseId
)
SELECT max(ReleaseId) as ReleaseId, min(ReleaseId) as RootReleaseId FROM cte_Releases
My problem now is I want to run through all #IDs (905 in that code) and join each record to a result

Select and update; SQL Server

I need to loop table tab_A entry by entry and update table tab_B according to content in each returned entry:
SELECT item, regBy, MAX(regTime) AS latestUpdateTime
FROM tab_A
GROUP BY item, regBy;
The result must be looped entry by entry and the following UPDATE must be performed for each entry:
UPDATE tab_B
SET lastUpdated = "data from latestUpdateTime in SQL above"
lastUpdBy = "data from regBy in SQL above"
WHERE item = "data from item in SQL above"
I'm not familiar with Transact SQL, so any help would be appreciated.
You can turn a regular select statement into an update statement quite easily by replacing the SELECT * with UPDATE table...
This allows you to first verify what will get updated before actually applying the update.
Select statement
SELECT *
FROM tab_B b
INNER JOIN (
SELECT item
, regBy
, MAX(regTime) AS latestUpdateTime
FROM tab_A
GROUP BY
item
, regBy
) a ON a.item = b.item
Update statement
UPDATE tab_B
SET lastUpdated = a.latestUpdateTime
, lastUpdBy = a.regBy
FROM tab_B b
INNER JOIN (
SELECT item
, regBy
, MAX(regTime) AS latestUpdateTime
FROM tab_A
GROUP BY
item
, regBy
) a ON a.item = b.item
Another way to verify the update and depending on your version would be by starting a transaction and using the OUTPUT clause.
BEGIN TRAN
UPDATE tab_B
SET lastUpdated = a.latestUpdateTime
, lastUpdBy = a.regBy
OUTPUT INSERTED.*
FROM tab_B b
INNER JOIN (
SELECT item
, regBy
, MAX(regTime) AS latestUpdateTime
FROM tab_A
GROUP BY
item
, regBy
) a ON a.item = b.item
ROLLBACK TRAN
If you're using SQL Server 2005 or newer, you can do something like this:
;WITH TableAData AS
(
SELECT
item, regBy, regTime,
RowNum = ROW_NUMBER() OVER(PARTITION BY item ORDER BY regTime DESC)
FROM dbo.tab_A
)
UPDATE dbo.tab_B
SET
lastUpdated = a.regTime,
lastUpdBy = a.regby
FROM TableAData a
WHERE
tab_B.item = a.item
AND a.RowNum = 1
Basically, this CTE (Common Table Expression) orders your data in such a way that for each item, a RowNum is calculated (with the most recent one getting RowNum = 1).
With this, you can easily update your tab_B in a single statement - no row-by-agonizing-row looping needed!

Resources