| Author |
Topic |
|
Clas
Starting Member
33 Posts |
Posted - 2008-10-01 : 10:59:36
|
| I work most with typed dataset and C# .This solution is very slow with dataset/loop.Is there a better solution with sql/procedure, SQL-server 2008 ?Table with Events:CREATE TABLE #EventsSource(eventID int,IndividualID int,EventDate date,)INSERT #EventsSourceVALUES(10,100,'1922-01-01'),(11,100,'1924-05-05'),(12,100,'1925-05-06'),(13,100,'1927-04-15'),(14,200,'1926-08-09'),(15,200,'1928-10-10'),(16,200,'1928-10-11'),(17,200,'1930-01-01'),(18,300,'1926-01-01'),(19,300,'1936-02-02') SELECT * FROM #EventsSourceI have an query to find and insert Stopdate in #Spell. This query is OK.Insert all StopDate:CREATE TABLE #Spell(spellID int,IndividualID int,StartEventID int,StartDate date,StopEventID int,StopDate date)INSERT #Spell(spellID,IndividualID,StopEventID,StopDate)VALUES(1,100,11,'1924-05-05'),(2,100,13,'1927-04-15'),(4,200,15,'1928-10-10'),(5,200,17,'1930-01-01'),(6,300,19,'1936-02-02')SELECT * FROM #SpellRESULT:spellID ----- IndividualID ----- StartEventID ----- StartDate -----StopEventID ----- StopDate1 -------------- 100 ------------------------------------------------ 11 ----------- 1924-05-052 -------------- 100 ------------------------------------------------ 13 ----------- 1927-04-154 -------------- 200 ------------------------------------------------ 15 ----------- 1928-10-105 -------------- 200 ------------------------------------------------ 17 ----------- 1930-01-016 -------------- 300 ------------------------------------------------ 19 ----------- 1936-02-02Next Step is to find and update #Spell (StartEventID and StartDate) from EventsSourceORDER BY IndividualID AND EventDate IN #EventsSource :The Individuals Previous EventDate in EventsSource ( < Stopdate in Spell) = StartEventSQL ?????????????????????SELECT and UPDATE #SpellRESULT:spellID ----- IndividualID ----- StartEventID ----- StartDate -----StopEventID ----- StopDate1 -------------- 100 ---------------- 10 ----------- 1922-01-01 ------ 11 ----------- 1924-05-052 -------------- 100 ---------------- 12 ----------- 1925-05-06 ------ 13 ----------- 1927-04-154 -------------- 200 ---------------- 14 ----------- 1926-08-09 ------ 15 ----------- 1928-10-105 -------------- 200 ---------------- 16 ----------- 1928-10-11 ------ 17 ----------- 1930-01-016 -------------- 300 ---------------- 18 ----------- 1926-01-01 ------ 19 ----------- 1936-02-02DROP table #EventsSource DROP table #Spell |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 11:03:32
|
| [code]UPDATE sSET s.StartEventID=es.EventID,s.StartDate=es.EventDateFROM #Spell sINNER JOIN #EventSource esON es.IndividualID=s.IndividualIDAND es.EventID=s.StopEventID -1[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 11:07:11
|
and just in case EventIDs are not contiguosUPDATE sSET s.StartEventID=es.EventID,s.StartDate=es.EventDateFROM #Spell sCROSS APPLY(SELECT TOP 1 EventID,EventDateFROM #EventSource WHERE IndividualID=s.IndividualIDAND es.EventID<s.StopEventIDORDER BY EventDate DESC)es |
 |
|
|
Clas
Starting Member
33 Posts |
Posted - 2008-10-03 : 06:46:22
|
| Thanks ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 07:09:20
|
quote: Originally posted by Clas Thanks !
welcome |
 |
|
|
|
|
|