Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have to select data from one table where "load date" is max and then i have to paste that data into another table where "load date" is max. but the condition is, data will be available everywhere in the other columns.

Note: load date is a column name.

Please suggest on the same.

Thanks in advance.
-----------------------------------------------------------

Please find table structure below:

SQL
CREATE TCREATE TABLE [dbo].[table1](
    [ACCT_ID] [char](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [SYS_NM] [char](9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [RPT_DT] [smalldatetime] NOT NULL,
    [CYCLE_CUT_DT] [smalldatetime] NOT NULL,
    [HI_SPEND_AM] [decimal](15, 2) NOT NULL,
    [LOC_LMT_AM] [decimal](15, 2) NOT NULL,
    [LOAD_TS] [smalldatetime] NOT NULL CONSTRAINT [DF_t0705_HI_SPEND_ACCT2_LOAD_TS]  DEFAULT (getutcdate()),
 CONSTRAINT [XUZAG01] PRIMARY KEY CLUSTERED
(
    [ACCT_ID] ASC,
    [CYCLE_CUT_DT] ASC,
    [SYS_NM] ASC,
    [RPT_DT] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[table2](
    [ACCT_ID] [char](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [PREV_LOC_AM] [decimal](15, 2) NOT NULL,
    [LOC_CHNG_AM] [decimal](15, 2) NOT NULL,
    [NEW_LOC_AM] [decimal](15, 2) NOT NULL,
    [CHNG_DT] [datetime] NOT NULL,
    [SYS_NM] [char](9) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [WEB_SRVC_STA_CD] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [ALLOC_ACT_CD] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [LOC_CHNG_TYPE_CD] [char](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [OTHER_PTCP_ACCT_RISK_ROLLUP_CD] [char](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [CYC_CUT_DT] [smalldatetime] NOT NULL,
    [RPT_IN] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [LOAD_TS] [smalldatetime] NOT NULL
) ON [PRIMARY]



SQL
I have to pickup the data of "LOC_LMT_AMT" column from table 1 where "CYCLE_CUT_DT" is max and have to paste the same data into second table where [CHNG_DT] is max.


I have to paste data , where "CHNG_DT" is max, remaining columns should not be pasted.
Posted
Updated 2-Sep-14 23:03pm
v5
Comments
Magic Wonder 3-Sep-14 3:14am    
Your requirement is not complete and clear to help you. Share your table structures and your efforts for above.
OriginalGriff 3-Sep-14 3:52am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
OriginalGriff 3-Sep-14 3:52am    
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.
_Asif_ 3-Sep-14 4:14am    
Can you provide sample data along with column names?

1 solution

Hi,

Check this...


SQL
update table2
set
table2.col=table1.LOC_LMT_AMT--since you have not mentioned in which col you have to update it
from
table1
where
table1.CYCLE_CUT_DT = (SELECT MAX(CYCLE_CUT_DT) FROM table1)
AND 
table2.CHNG_DT = (SELECT MAX(CHNG_DT) FROM table2)



Hope this will help you.

Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900