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 |
|
MrSethT
Starting Member
3 Posts |
Posted - 2007-11-07 : 12:44:37
|
| Let’s say I have these 2 tables Events TimeStamp------------- ---------------- seth logged on 11/7/07 1:00pmRick logged on 11/7/07 1:05pmseth logged on 11/7/07 10:23pm and Data TimeStamp--------- ---------------- 5 11/7/07 1:01pm6 11/7/07 1:02pm7 11/7/07 1:03pm8 11/7/07 1:03pm9 11/7/07 1:08pm9 11/7/07 1:15pm10 11/7/07 1:30pm3 11/7/07 4:06pm3 11/7/07 4:07pm3 11/7/07 4:08pm3 11/7/07 4:30pm3 11/7/07 10:24pm3 11/7/07 10:25pm3 11/7/07 10:27pm3 11/7/07 10:27pm44 11/7/07 10:45pm5 11/7/07 11:03pm56 11/7/07 11:42pm6 11/7/07 11:43pm How do you merget them to look like this? Data TimeStamp Events--------- ---------------- ------------5 11/7/07 1:01pm seth logged on6 11/7/07 1:02pm seth logged on7 11/7/07 1:03pm seth logged on8 11/7/07 1:03pm seth logged on9 11/7/07 1:08pm Rick logged on9 11/7/07 1:15pm Rick logged on10 11/7/07 1:30pm Rick logged on3 11/7/07 4:06pm Rick logged on3 11/7/07 4:07pm Rick logged on3 11/7/07 4:08pm Rick logged on3 11/7/07 4:30pm Rick logged on3 11/7/07 10:24pm seth logged on3 11/7/07 10:25pm seth logged on3 11/7/07 10:27pm seth logged on3 11/7/07 10:27pm seth logged on44 11/7/07 10:45pm seth logged on5 11/7/07 11:03pm seth logged on56 11/7/07 11:42pm seth logged on6 11/7/07 11:43pm seth logged onI just can't seem to get it myselfThanks for anything,-Seth |
|
|
nathans
Aged Yak Warrior
938 Posts |
|
|
anonymous1
Posting Yak Master
185 Posts |
Posted - 2007-11-07 : 13:07:49
|
| as long as the event timestamp is unique you can do something like this...declare @sample table (person varchar(10), logon datetime)insert @sampleselect 'seth', '11/7/07 1:00pm'union allselect 'Rick', '11/7/07 1:05pm'union allselect 'seth', '11/7/07 10:23pm'declare @sample2 table (data int, time_stamp datetime)insert @sample2select 5, '11/7/07 1:01pm'union allselect 6 ,'11/7/07 1:02pm'union allselect 7 ,'11/7/07 1:03pm'union allselect 8 ,'11/7/07 1:03pm'union allselect 9 ,'11/7/07 1:08pm'union allselect 9 ,'11/7/07 1:15pm'union allselect 10 ,'11/7/07 1:30pm'union allselect 3 ,'11/7/07 4:06pm'union allselect 3 ,'11/7/07 4:07pm'union allselect 3 ,'11/7/07 4:08pm'union allselect 3 ,'11/7/07 4:30pm'union allselect 3 ,'11/7/07 10:24pm'union allselect 3 ,'11/7/07 10:25pm'union allselect 3 ,'11/7/07 10:27pm'union allselect 3 ,'11/7/07 10:27pm'union allselect 44 ,'11/7/07 10:45pm'union allselect 5 ,'11/7/07 11:03pm'union allselect 56 ,'11/7/07 11:42pm'union allselect 6 ,'11/7/07 11:43pm'select data.data, data.time_stamp, person.personfrom @sample2 as datajoin(select sample2.time_stamp, max(sample.logon) as last_logonfrom @sample2 as sample2join @sample as sampleon sample2.time_stamp > sample.logongroup by sample2.time_stamp) as laston last.time_stamp = data.time_stampjoin @sample as personon person.logon = last.last_logon |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-07 : 13:10:17
|
[code]DECLARE @event TABLE( Events varchar(15), timestamp datetime)INSERT INTO @eventSELECT 'seth logged ON', '11/7/07 1:00pm' UNION ALLSELECT 'Rick logged ON', '11/7/07 1:05pm' UNION ALLSELECT 'seth logged ON', '11/7/07 10:23pm'DECLARE @data TABLE( Data int, timestamp datetime)INSERT INTO @dataSELECT 5, '11/7/07 1:01pm' UNION ALLSELECT 6, '11/7/07 1:02pm' UNION ALLSELECT 7, '11/7/07 1:03pm' UNION ALLSELECT 8, '11/7/07 1:03pm' UNION ALLSELECT 9, '11/7/07 1:08pm' UNION ALLSELECT 9, '11/7/07 1:15pm' UNION ALLSELECT 10, '11/7/07 1:30pm' UNION ALLSELECT 3, '11/7/07 4:06pm' UNION ALLSELECT 3, '11/7/07 4:07pm' UNION ALLSELECT 3, '11/7/07 4:08pm' UNION ALLSELECT 3, '11/7/07 4:30pm' UNION ALLSELECT 3, '11/7/07 10:24pm' UNION ALLSELECT 3, '11/7/07 10:25pm' UNION ALLSELECT 3, '11/7/07 10:27pm' UNION ALLSELECT 3, '11/7/07 10:27pm' UNION ALLSELECT 44, '11/7/07 10:45pm' UNION ALLSELECT 5, '11/7/07 11:03pm' UNION ALLSELECT 56, '11/7/07 11:42pm' UNION ALLSELECT 6, '11/7/07 11:43pm'SELECT d.Data, d.timestamp, e.EventsFROM @data d INNER JOIN ( SELECT *, next_TimeStamp = (SELECT MIN(timestamp) FROM @event x WHERE x.timestamp > e.timestamp) FROM @event e ) e ON d.timestamp >= e.timestamp AND ( d.timestamp < e.next_TimeStamp OR e.next_TimeStamp IS NULL )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
MrSethT
Starting Member
3 Posts |
Posted - 2007-11-07 : 13:12:57
|
| The real tables are really complicated and it wouldn't help for you to know all of the other columns and tables that are involved. What it comes down to is I have to be able to see who wrote each data point and all I have to go on is a table telling me when they logged in. So, I made up these guys off the top of my head. What I have so far is something like....SELECT Comment_ as User_, max(fact_event.Stime) as Time_, fact_data.data_uid FROM fact_data left join FACT_Event on fact_event.Stime<fact_data.STime group by comment_, fact_data.data_uidbut if you really want all the other junk I guess I can give that to |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2007-11-07 : 13:29:37
|
| Ok, i see what youre after now... i assumed the data point was a Login Audit, not a data point post-login.Nathan Skerl |
 |
|
|
MrSethT
Starting Member
3 Posts |
Posted - 2007-11-07 : 14:21:16
|
| great reply khtan!!exactly what I was looking for, and it works. That was impressiveThanks to you all,-Seth |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-07 : 15:01:18
|
SQL Server 2005?SELECT d.Data, d.Timestamp, d.EventsFROM ( SELECT d.Data, d.Timestamp, e.Events, DENSE_RANK() OVER (PARTITION BY d.Timestamp ORDER BY e.Timestamp DESC) AS RecID FROM @Data AS d INNER JOIN @Event AS e ON e.Timestamp <= d.Timestamp ) AS dWHERE d.RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|