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,189 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
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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 24

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

820 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