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,355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39935924
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
ID: 39937261
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
ID: 39938455
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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 25

Expert Comment

by:chaau
ID: 39941039
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
ID: 39949870
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
ID: 39963518
Advice from a product specific forum, with (I suppose) a subject expert providing an elegant answer. I merely relayed his offering.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

622 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