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 |
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-07-29 : 12:49:24
|
| Hello All,I have this following Table1:ID StartTime StopTime8 2005-10-04 22:35:09.330 2005-10-05 07:00:46.4979 2005-10-11 22:39:56.900 2005-10-12 07:31:51.37310 2005-10-11 21:33:57.470 2005-10-12 07:00:27.59711 2005-10-18 17:03:29.790 2005-10-19 07:33:18.79012 2005-11-15 23:06:41.350 2005-11-16 07:19:14.51013 2005-11-15 22:12:53.930 2005-11-16 07:01:58.36714 2005-11-01 22:26:31.100 2005-11-02 07:00:44.57015 2005-11-08 21:12:10.170 2005-11-09 07:08:47.34016 2005-11-09 21:33:54.780 2005-11-10 07:01:40.070and it goes on for awhile.I know how to find all the different possible start times but running a DISTINCT statement.I am curious as to how to ONLY return all duplicate start times BASED ON THE SAME DAY, MONTH, YR not the time.Result:9 2005-10-11 22:39:56.900 2005-10-12 07:31:51.37310 2005-10-11 21:33:57.470 2005-10-12 07:00:27.59712 2005-11-15 23:06:41.350 2005-11-16 07:19:14.51013 2005-11-15 22:12:53.930 2005-11-16 07:01:58.367Thanks,Dasman==========================Pain is Weakness Leaving the Body. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-29 : 13:09:45
|
| [code]SELECT ID, StartTime, StopTimeFROM(SELECT COUNT(ID) OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,StartTime),0) AS cnt,ID, StartTime, StopTime)tWHERE cnt>1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-07-29 : 13:16:02
|
| Hello Visakh,Thanks for the reply. One followup: Where does the table name "Table1" go?I ran this:SELECT ID, StartTime, StopTimeFROM(SELECT COUNT(ID) OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,StartTime),0) AS cnt,ID, StartTime, StopTime)Table1WHERE cnt>1and it gave me this error message:Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'AS'.Thanks,Dasman==========================Pain is Weakness Leaving the Body. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-29 : 13:23:34
|
| [code]SELECT ID, StartTime, StopTimeFROM(SELECT COUNT(ID) OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,StartTime),0) AS cnt,ID, StartTime, StopTimeFROM Table1)tWHERE cnt>1[/code]t is just a alias for the inner query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-07-29 : 13:52:00
|
| I am using SQL Server 2005.When I run this: SELECT ID, StartTime, StopTimeFROM(SELECT COUNT(ID) OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,StartTime),0) AS cnt,ID, StartTime, StopTimeFROM Tablename)tWHERE cnt>1I still get this error:Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'AS'.Whats wrong?Thanks,Dasman==========================Pain is Weakness Leaving the Body. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-29 : 13:57:26
|
missed a bracesSELECT ID, StartTime, StopTimeFROM(SELECT COUNT(ID) OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,StartTime),0)) AS cnt,ID, StartTime, StopTimeFROM Tablename)tWHERE cnt>1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|