473,383 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

update fields with searched first date record fields

Hello !
I'm trying to update one table field with another table searched first
date record.
getting some problem.
If anyone have experience similar thing or have any idea about it,
please guide.

Sample case is given below.

Thanks in adv.
T.S.Negi
--Sample case

DROP TABLE TEST1
DROP TABLE TEST2

CREATE TABLE TEST1
(
CUST_CD VARCHAR(10),
BOOKING_DATE DATETIME,
BOOKPHONE_NO VARCHAR(10)
)

CREATE TABLE TEST2
(
CUST_CD VARCHAR(10),
ENTRY_DATE DATETIME,
FIRSTPHONE_NO VARCHAR(10)
)

DELETE FROM TEST1
INSERT INTO TEST1 VALUES('C1',GETDATE()+5,'11111111')
INSERT INTO TEST1 VALUES('C1',GETDATE()+10,'22222222')
INSERT INTO TEST1 VALUES('C1',GETDATE()+15,'44444444')
INSERT INTO TEST1 VALUES('C1',GETDATE()+16,'33333333')

DELETE FROM TEST2
INSERT INTO TEST2 VALUES('C1',GETDATE(),'')
INSERT INTO TEST2 VALUES('C1',GETDATE()+2,'')
INSERT INTO TEST2 VALUES('C1',GETDATE()+11,'')
INSERT INTO TEST2 VALUES('C1',GETDATE()+12,'')

--SELECT * FROM TEST1
--SELECT * FROM TEST2
/*
Sample data
TEST1
CUST_CD BOOKING_DATE BOOKPHONE_NO
C1 2005-04-08 21:46:47.780 11111111
C1 2005-04-13 21:46:47.780 22222222
C1 2005-04-18 21:46:47.780 44444444
C1 2005-04-19 21:46:47.780 33333333

TEST2
CUST_CD ENTRY_DATE FIRSTPHONE_NO
C1 2005-04-03 21:46:47.800
C1 2005-04-05 21:46:47.800
C1 2005-04-14 21:46:47.800
C1 2005-04-15 21:46:47.800

DESIRED RESULT
CUST_CD ENTRY_DATE FIRSTPHONE_NO
C1 2005-04-03 21:46:47.800 11111111
C1 2005-04-05 21:46:47.800 11111111
C1 2005-04-14 21:46:47.800 44444444
C1 2005-04-15 21:46:47.800 44444444
*/

Jul 23 '05 #1
3 1236
On 3 Apr 2005 09:26:57 -0700, ti********@mind-infotech.com wrote:
Hello !
I'm trying to update one table field with another table searched first
date record.
getting some problem.
If anyone have experience similar thing or have any idea about it,
please guide.

Sample case is given below.

(snip)

Hi T.S.Negi,

Thanks for the create table and insert statements and expected output.
This query provides the desired results:

UPDATE TEST2
SET FIRSTPHONE_NO =
(SELECT TOP 1 TEST1.BOOKPHONE_NO
FROM TEST1
WHERE TEST1.BOOKING_DATE > TEST2.ENTRY_DATE
ORDER BY TEST1.BOOKING_DATE)

It does make use of proprietary T-SQL syntax (TOP 1), so it's not very
portable. Let me know if you prefer an ANSI-standard version.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Thanks for for your reply.
Please suggest, if there are more then 1 fields to update.

for e.g. newphone_no field in both table. and I want to update
t2.newphone_no with t1.newphone_no

Thanks,
T.S.Negi

Jul 23 '05 #3
On 3 Apr 2005 22:17:10 -0700, ti********@mind-infotech.com wrote:
Thanks for for your reply.
Please suggest, if there are more then 1 fields to update.

for e.g. newphone_no field in both table. and I want to update
t2.newphone_no with t1.newphone_no

Thanks,
T.S.Negi


Hi T.S.Negi,

This is the easiest way (though probably not the fastest):

UPDATE Test2
SET FirstPhone_no =
(SELECT TOP 1 Test1.BookPhone_no
FROM Test1
WHERE Test1.Booking_Date > Test2.Entry_Date
ORDER BY Test1.Booking_Date),
NewPhone_no =
(SELECT TOP 1 Test1.NewPhone_no
FROM Test1
WHERE Test1.Booking_Date > Test2.Entry_Date
ORDER BY Test1.Booking_Date)
(untested)

And this version gets rid of the proprietary TOP 1 syntax but uses the
proprietary UPDATE FROM syntax instead - probably faster than the
previous version, but test it to know for sure:

UPDATE T2
SET FirstPhone_no = T1.BookPhone_no,
NewPhone_no = T1.NewPhone_no
FROM Test2 AS T2
INNER JOIN Test1 AS T1
ON T1.Booking_Date = (SELECT MIN(T1b.Booking_Date)
FROM Test1 AS T1b
WHERE T1b.Booking_Date > T2.Entry_Date)
(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MVA | last post by:
Hi all I have a database where in a table (tbl_Members), there are 2 date fields (DateOfBirth and DateJoined), and also 2 fields which upon entering the data in the form, it automatically works...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
4
by: news.btinternet.com | last post by:
I have a very simple database. I have information in a form that I would like to write to a table using some code. The table is called tblTest. The field I would like to write to is called Date....
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
4
by: Reney | last post by:
I have a very weird problem in updating my datagrid. Please help me to solve it. The datagrid is tied to a dataset table with five columns. Three of them are primary key and the other two columns...
5
by: Louis LeBlanc | last post by:
Hey folks. I'm new to the list, and not quite what you'd call a DB Guru, so please be patient with me. I'm afraid the lead up here is a bit verbose . . . I am working on an application that...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.