| Author |
Topic |
|
tamancha.1
Starting Member
37 Posts |
Posted - 2010-04-23 : 16:19:29
|
| Table 1:user time_stamp Ad Imp_numA 3/14/2010 20:40:08 AA 1A 3/14/2010 20:40:02 AA 2A 3/15/2010 22:57:00 AA 3A 3/16/2010 11:40:03 AA 4A 3/16/2010 11:40:03 AB 5A 3/20/2010 10:36:00 AA 6A 3/22/2010 7:26:48 AA 7B 4/4/2010 8:03:38 AA 1B 4/4/2010 8:05:38 AA 2B 4/4/2010 8:06:29 AA 3B 4/4/2010 8:07:25 AA 4B 4/4/2010 8:08:19 AA 5C 4/3/2010 20:05:46 AB 1C 4/3/2010 20:07:13 AB 2D 3/19/2010 0:26:36 AA 1D 3/19/2010 0:27:41 AA 2D 3/19/2010 0:30:05 AA 3D 3/19/2010 0:31:22 AA 4D 3/19/2010 0:32:29 AA 5D 3/19/2010 0:33:47 AA 6D 3/19/2010 0:34:22 AB 7D 3/19/2010 0:36:25 AB 8Table 2:user time_stamp AdA 3/14/2010 20:40:06 AAB 4/4/2010 8:08:25 AAB 4/4/2010 8:08:42 AAD 3/19/2010 0:38:28 AAD 3/19/2010 0:38:37 AAD 3/19/2010 0:38:38 AAC 4/3/2010 20:07:24 ABJoined Table:user time_stamp Ad Imp_num click click_timeA 3/14/2010 20:40:08 AA 1 0 NULLA 3/14/2010 20:40:02 AA 2 1 3/14/2010 20:40:06A 3/15/2010 22:57:00 AA 3 0 NULLA 3/16/2010 11:40:03 AA 4 0 NULLA 3/16/2010 11:40:03 AB 5 0 NULLA 3/20/2010 10:36:00 AA 6 0 NULLA 3/22/2010 7:26:48 AA 7 0 NULLB 4/4/2010 8:03:38 AA 1 0 NULLB 4/4/2010 8:05:38 AA 2 0 NULLB 4/4/2010 8:06:29 AA 3 0 NULLB 4/4/2010 8:07:25 AA 4 0 NULLB 4/4/2010 8:08:19 AA 5 1 4/4/2010 8:08:25B 4/4/2010 8:08:19 AA 5 1 4/4/2010 8:08:42C 4/3/2010 20:05:46 AB 1 1 NULLC 4/3/2010 20:07:13 AB 2 1 4/3/2010 20:07:24D 3/19/2010 0:26:36 AA 1 0 NULLD 3/19/2010 0:27:41 AA 2 0 NULLD 3/19/2010 0:30:05 AA 3 0 NULLD 3/19/2010 0:31:22 AA 4 0 NULLD 3/19/2010 0:32:29 AA 5 0 NULLD 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:28D 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:37D 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:38D 3/19/2010 0:34:22 AB 7 0 NULLD 3/19/2010 0:36:25 AB 8 0 NULLJoin table 1 and table2 where:user.table1=user.table2,ad.table1=ad.table2,Assign click=1 and table2.time_stamp in joined table if user and ad are same table1.time_stamp <= table2.time_stamp and day.table1=day.table2 (day in time_stamp)and assign this to the latest time stamp satisfying all these condition, otherwise assign 0 and null value in joined table. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-24 : 00:45:41
|
| [code]SELECT t1.*,CASE WHEN t2.time_stamp IS NOT NULL 1 ELSE 0 END,t2.time_stampFROM Table1 t1OUTER APPLY(SELECT TOP 1 time_stamp FROM Table2 WHERE user=t1.user AND Ad = t1.Ad AND time_stamp> t1.time_stamp AND time_stamp < DATEADD(dd,DATEDIFF(dd,0,t1.time_stamp)+1,0) )t2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-24 : 01:04:36
|
CASE expression should be:quote: Originally posted by visakh16
SELECT t1.*,CASE WHEN t2.time_stamp IS NOT NULL THEN 1 ELSE 0 END,t2.time_stampFROM Table1 t1OUTER APPLY(SELECT TOP 1 time_stamp FROM Table2 WHERE user=t1.user AND Ad = t1.Ad AND time_stamp> t1.time_stamp AND time_stamp < DATEADD(dd,DATEDIFF(dd,0,t1.time_stamp)+1,0) )t2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-24 : 01:25:44
|
| oops ...missed that thanks for the catch------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-24 : 01:35:01
|
| oops ...missed that thanks for the catch------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-24 : 01:59:57
|
quote: Originally posted by visakh16 oops ...missed that thanks for the catch------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Welcome. Why did you send two same post? |
 |
|
|
tamancha.1
Starting Member
37 Posts |
Posted - 2010-04-27 : 10:49:20
|
| Thanks for your reply. But this does not work.It does not give me the same output as I mentioned in the post. Can you check please? |
 |
|
|
tamancha.1
Starting Member
37 Posts |
Posted - 2010-04-27 : 10:55:22
|
| We need to take the top timestamp from table 1, not table 2 and if everything matches, (add two new columns from )table2 records to table 1 with the top time stamp. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:33:36
|
| [code]SELECT t1.*,CASE WHEN t2.[ time_stamp] IS NOT NULL THEN 1 ELSE 0 END,t2.[ time_stamp]FROM TableA t1OUTER APPLY(SELECT [ time_stamp] FROM TableB WHERE [user]=t1.[user] AND Ad = t1.Ad AND [ time_stamp]> t1.[ time_stamp] AND [ time_stamp] < DATEADD(mi,DATEDIFF(mi,0,t1.[ time_stamp])+1,0) )t2[/code]i dont understand last part you get for D. it doesnt fall within the 1 minute slot as others so not sure how to generalise------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tamancha.1
Starting Member
37 Posts |
Posted - 2010-04-27 : 14:31:19
|
| No, we cannot generalize it on 1 or 2 minute. We want to assign two new columns to the last timestamp if everything matches. I have 2 million records in some cases the difference is 1 hour, some 1 minutes,...so I dont want to generalize on time...I want to add 1 to the latest time, when everything matches. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 05:50:48
|
| i want to add 1does that mean add a single time. i dont think so as you're associating three different values as per sampleD 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:28D 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:37D 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:38------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tamancha.1
Starting Member
37 Posts |
Posted - 2010-04-28 : 09:49:52
|
| I am adding three different values from table 2 into table 1 latest time, which is 3/19/2010 0:33:47 |
 |
|
|
tamancha.1
Starting Member
37 Posts |
Posted - 2010-04-28 : 09:50:58
|
| Hello Visakhm! Thanks for taking time to answer my query. Here is the idea:Records from Table 1:user time_stamp Ad Imp_numD 3/19/2010 0:26:36 AA 1D 3/19/2010 0:27:41 AA 2D 3/19/2010 0:30:05 AA 3D 3/19/2010 0:31:22 AA 4D 3/19/2010 0:32:29 AA 5D 3/19/2010 0:33:47 AA 6D 3/19/2010 0:34:22 AB 7D 3/19/2010 0:36:25 AB 8Records from Table 2user time_stamp AdD 3/19/2010 0:38:28 AAD 3/19/2010 0:38:37 AAD 3/19/2010 0:38:38 AAMerged Tableuser time_stamp Ad Imp_num click click_timeD 3/19/2010 0:26:36 AA 1 0 NULLD 3/19/2010 0:27:41 AA 2 0 NULLD 3/19/2010 0:30:05 AA 3 0 NULLD 3/19/2010 0:31:22 AA 4 0 NULLD 3/19/2010 0:32:29 AA 5 0 NULLD 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:28D 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:37D 3/19/2010 0:33:47 AA 6 1 3/19/2010 0:38:38D 3/19/2010 0:34:22 AB 7 0 NULLD 3/19/2010 0:36:25 AB 8 0 NULLFor Imp_num=7,8 the ad in table1 does not match ad in table 2 so we add 0 and null value.For Imp_num 1 to 6, the ad in table 1 matches the ad in table 2, so we go to time stamp.If user_id and ad matches then we make sure time_stamp in table 1 is less than timestamp in table2. If no, we assign click=0 click_time=NULLIf yes, we further assign the click=1 and timestamp from table 2 to the latest time in table 1.Since there are 3 values in table 2 that satisfies all these conditions we add all of them in merged table.3/19/2010 0:33:47 is the latest time(or largest time) in table 1, user and ad is same and tbl1_time_stamp is less than table2 timestamp so we assign all |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 11:43:51
|
| [code]SELECT [user],[time_stamp],Ad,Imp_num,CASE WHEN Seq=1 THEN 1 ELSE 0 END AS click,CASE WHEN Seq=1 THEN timeafter ELSE NULL END AS click_timeFROM (SELECT *,DENSE_RANK() OVER (PARTITION BY [user],Ad ORDER BY CASE WHEN t2.timeafter IS NULL THEN '1900-01-01' ELSE t1.[time_stamp] END DESC ) AS Seq FROM TableA t1OUTER APPLY(SELECT [time_stamp] as timeafter FROM TableB WHERE [user]=t1.[user] AND Ad = t1.Ad AND [time_stamp]> t1.[time_stamp] )t2)tORDER BY [user],Ad,Imp_numoutput--------------------------------------------------user time_stamp Ad Imp_num click click_timeA 2010-03-14 20:40:08.000 AA 1 0 NULLA 2010-03-14 20:40:02.000 AA 2 1 2010-03-14 20:40:06.000A 2010-03-15 22:57:00.000 AA 3 0 NULLA 2010-03-16 11:40:03.000 AA 4 0 NULLA 2010-03-20 10:36:00.000 AA 6 0 NULLA 2010-03-22 07:26:48.000 AA 7 0 NULLA 2010-03-16 11:40:03.000 AB 5 1 NULLB 2010-04-04 08:03:38.000 AA 1 0 NULLB 2010-04-04 08:03:38.000 AA 1 0 NULLB 2010-04-04 08:05:38.000 AA 2 0 NULLB 2010-04-04 08:05:38.000 AA 2 0 NULLB 2010-04-04 08:06:29.000 AA 3 0 NULLB 2010-04-04 08:06:29.000 AA 3 0 NULLB 2010-04-04 08:07:25.000 AA 4 0 NULLB 2010-04-04 08:07:25.000 AA 4 0 NULLB 2010-04-04 08:08:19.000 AA 5 1 2010-04-04 08:08:25.000B 2010-04-04 08:08:19.000 AA 5 1 2010-04-04 08:08:42.000C 2010-04-03 20:05:46.000 AB 1 0 NULLC 2010-04-03 20:07:13.000 AB 2 1 2010-04-03 20:07:24.000D 2010-03-19 00:26:36.000 AA 1 0 NULLD 2010-03-19 00:26:36.000 AA 1 0 NULLD 2010-03-19 00:26:36.000 AA 1 0 NULLD 2010-03-19 00:27:41.000 AA 2 0 NULLD 2010-03-19 00:27:41.000 AA 2 0 NULLD 2010-03-19 00:27:41.000 AA 2 0 NULLD 2010-03-19 00:30:05.000 AA 3 0 NULLD 2010-03-19 00:30:05.000 AA 3 0 NULLD 2010-03-19 00:30:05.000 AA 3 0 NULLD 2010-03-19 00:31:22.000 AA 4 0 NULLD 2010-03-19 00:31:22.000 AA 4 0 NULLD 2010-03-19 00:31:22.000 AA 4 0 NULLD 2010-03-19 00:32:29.000 AA 5 0 NULLD 2010-03-19 00:32:29.000 AA 5 0 NULLD 2010-03-19 00:32:29.000 AA 5 0 NULLD 2010-03-19 00:33:47.000 AA 6 1 2010-03-19 00:38:28.000D 2010-03-19 00:33:47.000 AA 6 1 2010-03-19 00:38:37.000D 2010-03-19 00:33:47.000 AA 6 1 2010-03-19 00:38:38.000D 2010-03-19 00:34:22.000 AB 7 1 NULLD 2010-03-19 00:36:25.000 AB 8 1 NULL[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|