Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-05 : 22:34:49
|
Hello everyone,Because I am new with SQL Server and T-SQL, so I will need your help.I have 2 table: Realtime and EOD. To understand my question, I give example data for 2 tables:---Realtime table---Symbol Date ValueABC 1/3/2009 03:05:01 327 // this day is not existed in EOD -> insertingBBC 1/3/2009 03:05:01 458 // this day is not existed in EOD -> insertingABC 1/2/2009 03:05:01 326 // this day is new -> updatingBBC 1/2/2009 03:05:01 454 // this day is new -> updatingABC 1/2/2009 02:05:01 323BBC 1/2/2009 02:05:01 453ABC 1/2/2009 01:05:01 313BBC 1/2/2009 01:05:01 423 ---EOD table---Symbol Date ValueABC 1/2/2009 02:05:01 323BBC 1/2/2009 02:05:01 453 I will need to create a store procedure to update value of symbols. If data in day of a symbol is new (compare between Realtime & EOD), it will update value and date for EOD at that day if existing, otherwise inserting.And store will update EOD table with new data:---EOD table---Symbol Date ValueABC 1/3/2009 03:05:01 327BBC 1/3/2009 03:05:01 458ABC 1/2/2009 03:05:01 326BBC 1/2/2009 03:05:01 454 P/S: I use SQL Server 2005. And I have a similar answered question at here: http://stackoverflow.com/questions/2726369/help-to-the-way-to-write-a-query-for-the-requirementPlease help me. Thanks. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-05 : 22:47:26
|
[code]declare @Realtime table( Symbol varchar(3), [Date] datetime, Value int)declare @EOD table( Symbol varchar(3), [Date] datetime, Value int)insert into @Realtimeselect 'ABC', '1/3/2009 03:05:01', 327 union all -- this day is not existed in EOD -> insertingselect 'BBC', '1/3/2009 03:05:01', 458 union all -- this day is not existed in EOD -> insertingselect 'ABC', '1/2/2009 03:05:01', 326 union all -- this day is new -> updatingselect 'BBC', '1/2/2009 03:05:01', 454 union all -- this day is new -> updatingselect 'ABC', '1/2/2009 02:05:01', 323 union allselect 'BBC', '1/2/2009 02:05:01', 453 union allselect 'ABC', '1/2/2009 01:05:01', 313 union allselect 'BBC', '1/2/2009 01:05:01', 423insert into @EODselect 'ABC', '1/2/2009 02:05:01', 323 union allselect 'BBC', '1/2/2009 02:05:01 ', 453-- Query (Update)update eset [Date] = r.[Date], Value = r.Valuefrom ( select Symbol, [Date], Value, Dte = dateadd(day, datediff(day, 0, [Date]), 0) from @EOD ) e inner join ( select Symbol, [Date], Value, Dte = dateadd(day, datediff(day, 0, [Date]), 0), row_no = row_number() over (partition by Symbol, dateadd(day, datediff(day, 0, [Date]), 0) order by [Date] desc) from @Realtime ) r on e.Symbol = r.Symbol and e.Dte = r.Dtewhere r.row_no = 1-- Query (Insert)insert into @EOD (Symbol, [Date], Value)select Symbol, [Date], Valuefrom ( select Symbol, [Date], Value, Dte = dateadd(day, datediff(day, 0, [Date]), 0), row_no = row_number() over (partition by Symbol, dateadd(day, datediff(day, 0, [Date]), 0) order by [Date] desc) from @Realtime ) rwhere r.row_no = 1and not exists ( select * from @EOD x where x.Symbol = r.Symbol and dateadd(day, datediff(day, 0, x.[Date]), 0) = r.Dte )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-05 : 22:59:26
|
| Ok. Thank khtan. Run well. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-05 : 23:06:12
|
if you do notice, this query actually gives you the EOD, you can create a view out of it select Symbol, [Date], Valuefrom ( select Symbol, [Date], Value, -- Dte = dateadd(day, datediff(day, 0, [Date]), 0), row_no = row_number() over (partition by Symbol, dateadd(day, datediff(day, 0, [Date]), 0) order by [Date] desc) from @Realtime ) rwhere r.row_no = 1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-06 : 00:03:19
|
I have a change in my question. To optimize, I only want to get the last data from Realtime table of each symbol.Ex:---Realtime table---Symbol Date ValueABC 1/3/2009 03:05:01 327BBC 1/3/2009 03:05:01 458ABC 1/2/2009 03:05:01 326BBC 1/2/2009 03:05:01 454ABC 1/2/2009 02:05:01 323BBC 1/2/2009 02:05:01 453ABC 1/2/2009 01:05:01 313BBC 1/2/2009 01:05:01 423 I will get the last data of Realtime for each symbol:Symbol Date ValueABC 1/3/2009 03:05:01 327BBC 1/3/2009 03:05:01 458 And compare with EOD data, because '1/3/2009 03:05:01' is new and the day is not existing in EOD table -> inserting. And if '1/3/2009 03:05:01' is new and the day is existing in EOD table -> updating.Please help me. Thanks. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-06 : 00:08:30
|
just remove the dateadd-datediff from the "partition by"select Symbol, [Date], Valuefrom ( select Symbol, [Date], Value, row_no = row_number() over (partition by Symbol, dateadd(day, datediff(day, 0, [Date]), 0) order by [Date] desc) from @Realtime ) rwhere r.row_no = 1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-06 : 00:12:46
|
[code]-- Query (Update)update eset [Date] = r.[Date], Value = r.Valuefrom ( select Symbol, [Date], Value-- Dte = dateadd(day, datediff(day, 0, [Date]), 0) from @EOD ) e inner join ( select Symbol, [Date], Value, -- Dte = dateadd(day, datediff(day, 0, [Date]), 0), row_no = row_number() over (partition by Symbol --, dateadd(day, datediff(day, 0, [Date]), 0) order by [Date] desc) from @Realtime ) r on e.Symbol = r.Symbol-- and e.Dte = r.Dtewhere r.row_no = 1-- Query (Insert)insert into @EOD (Symbol, [Date], Value)select Symbol, [Date], Valuefrom ( select Symbol, [Date], Value, -- Dte = dateadd(day, datediff(day, 0, [Date]), 0), row_no = row_number() over (partition by Symbol --, dateadd(day, datediff(day, 0, [Date]), 0) order by [Date] desc) from @Realtime ) rwhere r.row_no = 1and not exists ( select * from @EOD x where x.Symbol = r.Symbol-- and dateadd(day, datediff(day, 0, x.[Date]), 0) = r.Dte )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-06 : 00:23:38
|
Wrong, the result is not correct. Your sql returns:ABC 2009-01-03 03:05:01.000 327BBC 2009-01-03 03:05:01.000 458 I don't know why your sql remove data of old day. My requirement is:ABC 2009-01-03 03:05:01.000 327BBC 2009-01-03 03:05:01.000 458ABC 2009-01-02 02:05:01.000 323BBC 2009-01-02 02:05:01.000 453 Please help me. Thanks. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-06 : 00:27:07
|
quote: Originally posted by lulu2792 Wrong, the result is not correct. Your sql returns:ABC 2009-01-03 03:05:01.000 327BBC 2009-01-03 03:05:01.000 458 I don't know why your sql remove data of old day. My requirement is:ABC 2009-01-03 03:05:01.000 327BBC 2009-01-03 03:05:01.000 458ABC 2009-01-02 02:05:01.000 323BBC 2009-01-02 02:05:01.000 453 Please help me. Thanks.
Then what do you mean by this ?quote: I have a change in my question. To optimize, I only want to get the last data from Realtime table of each symbol.
quote:
I will get the last data of Realtime for each symbol:Symbol Date ValueABC 1/3/2009 03:05:01 327BBC 1/3/2009 03:05:01 458
KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-06 : 00:30:51
|
| I'm sorry for my bad english. "I have a change in my question. To optimize, I only want to get the last data from Realtime table of each symbol." mean that: I don't need to check all data in Realtime. I only need to check the last data of Realtime for each symbol and compare with EOD. Thanks. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-06 : 00:38:32
|
you mean don't update the existing data in EOD ? Just Insert new record into EOD ?use the query i posted on 05/05/2010 : 22:47:26 and remove the UPDATE query KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-06 : 00:49:31
|
No no. Also updating & inserting. But I only interest in the last data of each symbol in Realtime table. Inserting or Updating is depending on EOD table.Example: I get the last data of symbol 'ABC' from Realtime data:ABC 1/3/2009 03:05:01 327 I check the the last data of symbol 'ABC' from EOD table:ABC 1/2/2009 03:05:01 327 because '1/3/2009 03:05:01' is newer '1/2/2009 03:05:01' and the date '1/3/2009' != '1/2/2009' -> inserting.If the the last data of symbol 'ABC' from EOD table:ABC 1/3/2009 02:05:01 327 because '1/3/2009 03:05:01' is newer '1/3/2009 02:05:01' and the date '1/3/2009' == '1/3/2009' -> updating.Do similar for all symbols. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-06 : 04:44:15
|
[code]DECLARE @Realtime TABLE( Symbol varchar(3), [Date] datetime, Value int)DECLARE @EOD TABLE( Symbol varchar(3), [Date] datetime, Value int)INSERT INTO @RealtimeSELECT 'ABC', '1/3/2009 03:05:01', 327 UNION ALL -- this day is not existed in EOD -> insertingSELECT 'BBC', '1/3/2009 03:05:01', 458 UNION ALL -- this day is not existed in EOD -> insertingSELECT 'ABC', '1/2/2009 03:05:01', 326 UNION ALL -- this day is new -> updatingSELECT 'BBC', '1/2/2009 03:05:01', 454 UNION ALL -- this day is new -> updatingSELECT 'ABC', '1/2/2009 02:05:01', 323 UNION ALLSELECT 'BBC', '1/2/2009 02:05:01', 453 UNION ALLSELECT 'ABC', '1/2/2009 01:05:01', 313 UNION ALLSELECT 'BBC', '1/2/2009 01:05:01', 423INSERT INTO @EODSELECT 'ABC', '1/2/2009 02:05:01', 323 UNION ALLSELECT 'BBC', '1/2/2009 02:05:01 ', 453-- Query (UPDATE)UPDATE eSET [Date] = r.[Date], Value = r.ValueFROM ( SELECT Symbol, [Date], Value, dte = DATEADD(DAY, DATEDIFF(DAY, 0, [Date]), 0), row_no = row_number() OVER (PARTITION BY Symbol ORDER BY [Date] DESC) FROM @EOD ) e INNER JOIN ( SELECT Symbol, [Date], Value, dte = DATEADD(DAY, DATEDIFF(DAY, 0, [Date]), 0), row_no = row_number() OVER (PARTITION BY Symbol ORDER BY [Date] DESC) FROM @Realtime ) r ON e.Symbol = r.Symbol AND e.dte = r.dteWHERE e.row_no = 1AND r.row_no = 1AND ( e.[Date] <> r.[Date] OR e.[Value] <> r.[Value] )-- Query (INSERT)INSERT INTO @EOD (Symbol, [Date], Value)SELECT Symbol, [Date], ValueFROM ( SELECT Symbol, [Date], Value, row_no = row_number() OVER (PARTITION BY Symbol ORDER BY [Date] DESC) FROM @Realtime ) rWHERE r.row_no = 1AND NOT EXISTS ( SELECT * FROM @EOD x WHERE x.Symbol = r.Symbol AND x.[Date] = r.[Date] )SELECT *FROM @EOD/*Symbol Date Value ------ ------------------------------------------------------ ----------- ABC 2009-01-02 02:05:01.000 323BBC 2009-01-02 02:05:01.000 453ABC 2009-01-03 03:05:01.000 327BBC 2009-01-03 03:05:01.000 458(4 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lulu2792
Starting Member
12 Posts |
Posted - 2010-05-06 : 05:58:55
|
| Thank sir. This is the best site for my beginning. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-06 : 06:01:10
|
you are welcome  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|