Solved

SQLite Update a table from values in another table is not applying the correct source row value.

Posted on 2014-03-17
6
3,005 Views
Last Modified: 2014-03-29
I'm trying to repair a table (tblrequest) which has a date "created". Some were left null, and I want to update those with the earliest activity date for that request from an event log.
The update is working but is applying the "mindate" of the first row in the work table to all candidate updates.

How do I apply the right value to the correct tblrequest?

Working script which I highlight and run one step at a time follows:

-- select only one statement at a time to execute
-- SQLite repair of ommitted created date from earliest event log.
-- CW 18/03/2014
drop table mindates
create table mindates
(mindate date, rid int)

insert into mindates
  select Min(tblEventLog.Created) as mindat , tblrequest.id as rid
  From  tblrequest join tblEventLog where tblRequest.ID = tblEventLog.RequestID   and tblrequest.created isnull  
  group by tblrequest.id
 
Update
  tblRequest set reference = (select mindate
             from tblrequest inner join mindates on tblrequest.id = mindates.rid where tblrequest.created isnull )
where  exists (select * from mindates where mindates.rid = tblrequest.id)

Appreciate any guidance.
0
Comment
Question by:fidsplice
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:chaau
Comment Utility
You need to modify the query a bit:
Update tblRequest 
set reference = (select mindate from mindates where mindates.rid = tblRequest.id )
where  exists (select 1 from mindates where mindates.rid = tblRequest.id)

Open in new window

0
 
LVL 5

Expert Comment

by:dannygonzalez09
Comment Utility
try this

Update t
set t.reference = m.mindate 
from tblRequest t
Inner join mindates m
on t.id = m.rid
where t.created is null

Open in new window

0
 

Author Comment

by:fidsplice
Comment Utility
Thanks guys, but still no joy.  Remember this is SQLite.

Chaau, that works but gives me the same result. The inner select produces a data set, the where exists produces a true, but the update takes the first row from the produced data set.
dannygonzalez09 - the bind is that SQLite doesn't support a join in an "update".

Fortunately I've only got about 50 entries to correct so worst case is I'll do it by hand, but the apparent lack of a workable solution alarms me a bit! 'd love to find a solution.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 24

Expert Comment

by:chaau
Comment Utility
It is strange, as it should work. What version of SQLite are you using? There was an update that was something to do with the UPDATE problem. I recommend you ask SQLite team by sending your question to this mailing list: sqlite-users@sqlite.org. D Richard Hipp himself monitors this list and replies the questions directly. I have asked there a few times. People there are very helpful
0
 

Accepted Solution

by:
fidsplice earned 0 total points
Comment Utility
Thanks Chaau for the tip, The SQLite forum provided an answer and it was so simple. Where my brain lost touch with reality I don't know but the answer was simpler, not more complicated. After 45 years in this game why haven't I learned to stop and regroup!!  Oh well!

It is thus:
update tblRequest
   set reference = (select min(Created)
                      from tblEventLog
                     where RequestID = tblRequest.ID)
 where Created is null;

Boy!!!
0
 

Author Closing Comment

by:fidsplice
Comment Utility
Advice from a product specific forum, with (I suppose) a subject expert providing an elegant answer. I merely relayed his offering.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now