Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

update one table with the max value of another table

$
0
0
CREATE TABLE [dbo].[General](
	[a_id] int NOT NULL,
	[a_date] datetime NOT NULL,
	[c_date] datetime NULL)


CREATE TABLE [dbo].[Specific](
	[a_id] int NOT NULL,
	[a_date] datetime NOT NULL,
	[b_id] int NOT NULL,
	[d_date] datetime NULL)

insert into General values (111,'Jan 1 2013', null)
insert into General values (112,'Jan 1 2013', 'Mar 10 2014')
insert into General values (113,'Jan 1 2013', null)
insert into General values (114,'Feb 1 2013', null)
insert into General values (115,'Feb 1 2013', 'Apr 1 2013')
insert into General values (116,'Feb 1 2013', 'Jan 1 1970')

insert into Specific values (111,'Jan 1 2013',1, 'Jan 1 2013')
insert into Specific values (111,'Jan 1 2013',2, 'Feb 1 2013')
insert into Specific values (111,'Jan 1 2013',3, 'Mar 1 2013')
insert into Specific values (112,'Jan 1 2013',1, 'Jan 1 2013')
insert into Specific values (112,'Jan 1 2013',2, 'Feb 1 2013')
insert into Specific values (113,'Jan 1 2013',1, 'Jan 1 1970')
insert into Specific values (114,'Feb 1 2013',1, null)
insert into Specific values (115,'Feb 1 2013',1, 'Jan 15 2013')
insert into Specific values (115,'Feb 1 2013',2, 'Feb 15 2013')
insert into Specific values (116,'Feb 1 2013',1, 'Jan 30 2013')
insert into Specific values (116,'Feb 1 2013',2, 'Jan 1 1970')

I want to update General.c_date only when the values are null or Jan 1 1970 from the maximum value of Specific.d_date as long as the maximum is not null or Jan 1 1970.    The two tables join on a_id and a_date.  In the example  above  a_id = 111 and a_date = Jan 1 2013 should have c_date = Mar 1 2013 and a_id = 116 and a_date = Feb 1 2013 should have c_date = Jan 30 2013.

lg


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>