473,809 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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',GET DATE()+5,'11111 111')
INSERT INTO TEST1 VALUES('C1',GET DATE()+10,'2222 2222')
INSERT INTO TEST1 VALUES('C1',GET DATE()+15,'4444 4444')
INSERT INTO TEST1 VALUES('C1',GET DATE()+16,'3333 3333')

DELETE FROM TEST2
INSERT INTO TEST2 VALUES('C1',GET DATE(),'')
INSERT INTO TEST2 VALUES('C1',GET DATE()+2,'')
INSERT INTO TEST2 VALUES('C1',GET DATE()+11,'')
INSERT INTO TEST2 VALUES('C1',GET DATE()+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 1251
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_D ATE > TEST2.ENTRY_DAT E
ORDER BY TEST1.BOOKING_D ATE)

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_D ate > Test2.Entry_Dat e
ORDER BY Test1.Booking_D ate),
NewPhone_no =
(SELECT TOP 1 Test1.NewPhone_ no
FROM Test1
WHERE Test1.Booking_D ate > Test2.Entry_Dat e
ORDER BY Test1.Booking_D ate)
(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_Dat e > 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
10392
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 out the Age (in years) and number of DaysJoined (in days). It works fine but it does not automatically update the Age or DaysJoined columns on opening the form. Can anyone make a suggestion as to how I could say, open up the Switchboard frm,...
9
4362
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 predecessor, I hasten to add) so that each day it creates a copy of the record for each company, changes the date to today's date, and prompts the user for any changes of ratings on that day. The resulting data table grows by approx 600 records per...
4
2404
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. The data which I would like to write is Date(). Please can someone help me with this simple code? Many Thanks, John
8
3728
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: "Date","P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11","P12","P13","P14","P15","P16","P17","P18","P19","P20","P21" 1/1/2005,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 1/2/2005,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
4
5313
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 are the ones that are of interest. These two columns have "date" type values and shows short time (i.e. 11:39). The program updates the database if I change the values in these columns, or add a new record, or delete a record. But, if one of...
5
5613
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 uses very high volume DB transactions - in the order of tens of millions per day . . . Anyway, the current database which will remain nameless, but begins with O and rymes with debacle (sorta), has a problem with high volume work when it comes to...
2
19619
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 field in table2. What I have is a form that is linked to Table2. If the users want to change a field in the main database (table1), they fill the change in to the form (which is linked to table2) If there is no change to the field, they simply...
16
3504
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 in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown list and checkbox so that they can make changes. I named these elements as arrays and wish to run an...
2
2642
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 update the information which is stored in a SQL database. In testing we noticed that the form was updating correctly but the update mechanism was also updating the first record of the table in the sql database every time. No error messages are on...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.