Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2008, 02:09 AM
Registered User
 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Query for getting latest date and following ..

hi all,

i have a table TABLE with 10 fields: INT1, DATE1, DATE2, etc etc.

INT1 DATE1 DATE2 ...
1 02-Jan-07 02-Oct-07 ...
2 02-Jan-07 02-Oct-07 ...
1 02-Feb-07 02-Oct-07 ...
1 20-Feb-07 10-Oct-07 ...

The user chooses which INT1 he likes to update(suppose he chooses 1) & then the DATE2 field corresponding to that INT1 is updated whose DATE1 field is latest(like here since user chose INT1=1, & DATE1 field of last row is latest(20-Feb-07), its DATE2 is updated from 10-Oct-07 to some other inputted date value say 15-Oct-07. How do i do that?

I was trying to do something like:

update
(select DATE2, max(DATE1) as dt from TABLE where INT1 = 1 group by DATE1)
set DATE2 = 15-Oct-07;

Can someone help forming a working query?
thank you
 
Old February 28th, 2008, 02:19 AM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

You need to first create query string dynamically
you can write you query in following manner, please take care of your front end syntax, here i m ignoring it

intvalue="1"
datevalue="20-feb-2008"
sSqltext="UPDATE tablename set DATE" & intvalue & "='" & datevalue "' WHERE INT1=" & intvalue

I also think there is some problem in database design. Any way I hope above query will help.

urt

Help yourself by helping someone.
 
Old February 28th, 2008, 02:28 AM
Registered User
 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well, there can be many rows with same INT1 field so the above query is not suitable. So, 1st getting the row with latest DATE1 field is required.
 
Old February 28th, 2008, 05:02 AM
Registered User
 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did:

update
(select * from TABLE1 where DATE1 =
(select Max(DATE1) from TABLE1 where INT1 = 1)
)
set DATE2 = '15-Oct-08' ;

The result shows "1 row updated" but infact the updation takes place of Temporary table, not of the main table, TABLE1. The update is not reflected in TABLE1

How do i update the main table?
 
Old February 28th, 2008, 08:10 AM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

I am not able to understand your case

Try following syntax for updating table from table

update table1 set table1.col2=b.col2
from (select col1,col2 from table2) b
where table1.col1=b.col1

urt

Help yourself by helping someone.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select the latest date, then the lowest price. Stuart Stalker SQL Server DTS 2 October 12th, 2006 03:42 AM
Query to Select latest X entries acdsky Classic ASP Databases 3 July 6th, 2004 01:41 AM
two tables, one query, getting the latest for each eln MySQL 2 November 17th, 2003 11:15 PM
Latest Date with Inner Join Query rstelma SQL Server 2000 3 November 5th, 2003 07:32 AM
Returning ONLY the latest records in a query kilkerr1 Access 3 November 3rd, 2003 01:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.