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 |
|
starnold
Yak Posting Veteran
83 Posts |
Posted - 2008-10-28 : 10:47:46
|
| I have the below date query that should give me the number of hours by month/year and userid. The result gives me 811 hours for october which is impossible! Doing the calculation in Excel with the data below gives me about 10.5 hours, can anyone help?SELECT USERID,DATEPART(month,TimeAllocated) as "Month",DATEPART(year,TimeAllocated) as "Year",SUM(datediff(ss,TimeAllocated,TimeLastAccessed)/360) as "NumberOfHours"FROM SESSIONSwhere timeallocated > '01 october 2008' and userid = '3'GROUP BY USERID,DATEPART(month,TimeAllocated),DATEPART(year,TimeAllocated)order by 3,2 descTimeAllocated UserID TimeLastAccessed 2008-10-16 11:46:28.910 3 2008-10-16 16:29:30.9372008-10-07 13:40:57.803 3 2008-10-07 16:59:14.0632008-10-09 18:07:36.310 3 2008-10-09 19:10:53.4502008-10-09 10:25:42.460 3 2008-10-09 16:14:30.9002008-10-13 16:49:29.437 3 2008-10-13 17:43:10.0632008-10-28 10:28:26.630 3 2008-10-28 10:28:26.6302008-10-14 13:10:06.690 3 2008-10-14 16:07:33.9072008-10-09 14:12:26.683 3 2008-10-09 14:14:43.6232008-10-24 18:23:33.820 3 2008-10-24 19:57:55.2472008-10-08 12:27:20.000 3 2008-10-08 13:20:57.0602008-10-13 09:33:18.080 3 2008-10-13 11:02:34.5232008-10-27 15:18:18.567 3 2008-10-27 15:18:18.5672008-10-21 19:52:35.743 3 2008-10-21 19:52:35.7432008-10-06 13:39:38.773 3 2008-10-06 14:59:52.0302008-10-06 15:19:09.617 3 2008-10-06 17:32:04.5772008-10-27 12:31:38.223 3 2008-10-27 12:31:38.2232008-10-08 10:07:25.020 3 2008-10-08 11:49:38.0702008-10-21 14:37:57.180 3 2008-10-21 14:38:01.1032008-10-07 17:25:41.363 3 2008-10-07 17:59:59.5902008-10-14 10:41:49.557 3 2008-10-14 15:39:58.7202008-10-10 08:31:55.483 3 2008-10-10 08:32:04.9702008-10-24 11:58:56.780 3 2008-10-24 18:03:11.2332008-10-15 10:03:00.143 3 2008-10-15 13:34:16.0072008-10-17 12:22:15.383 3 2008-10-17 15:05:16.5832008-10-15 13:41:59.423 3 2008-10-15 14:25:24.6872008-10-08 09:20:52.827 3 2008-10-08 12:07:04.0102008-10-22 12:46:34.877 3 2008-10-22 13:01:00.7302008-10-07 09:05:00.367 3 2008-10-07 14:12:56.0102008-10-22 09:33:36.633 3 2008-10-22 17:30:39.1072008-10-17 09:50:49.710 3 2008-10-17 10:15:58.4502008-10-07 13:20:46.153 3 2008-10-07 13:40:46.8032008-10-02 11:15:50.327 3 2008-10-02 12:14:11.2702008-10-24 20:14:00.987 3 2008-10-24 21:14:33.8002008-10-09 19:09:56.853 3 2008-10-09 19:09:58.8532008-10-07 11:33:07.070 3 2008-10-07 12:43:06.5372008-10-10 17:22:10.650 3 2008-10-10 19:52:04.1732008-10-10 12:25:33.967 3 2008-10-10 17:12:17.9372008-10-21 14:22:28.553 3 2008-10-21 18:09:38.8972008-10-24 09:56:40.023 3 2008-10-24 11:34:18.6902008-10-10 08:45:33.907 3 2008-10-10 12:12:27.5002008-10-10 11:28:01.520 3 2008-10-10 11:28:10.3172008-10-28 11:13:20.817 3 2008-10-28 11:13:20.817 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-28 : 10:54:01
|
| FYI: 3600 seconds = 1 hour |
 |
|
|
bjoerns
Posting Yak Master
154 Posts |
Posted - 2008-10-28 : 10:55:01
|
| My first idea is that an hour has 3600 seconds. But that would only improve by a factor of ten ... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-28 : 11:05:58
|
quote: Originally posted by bjoerns My first idea is that an hour has 3600 seconds. But that would only improve by a factor of ten ...
not exactly. you've applying sum() over it...so result might change more. Try running changed query and see. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-28 : 11:07:06
|
A factor of ten is ok, if you also think about integer rounding.DECLARE @Sample TABLE ( timeAllocated DATETIME, userID INT, timeLastAccessed DATETIME )INSERT @SampleSELECT '2008-10-16 11:46:28.910', 3, '2008-10-16 16:29:30.937' UNION ALLSELECT '2008-10-07 13:40:57.803', 3, '2008-10-07 16:59:14.063' UNION ALLSELECT '2008-10-09 18:07:36.310', 3, '2008-10-09 19:10:53.450' UNION ALLSELECT '2008-10-09 10:25:42.460', 3, '2008-10-09 16:14:30.900' UNION ALLSELECT '2008-10-13 16:49:29.437', 3, '2008-10-13 17:43:10.063' UNION ALLSELECT '2008-10-28 10:28:26.630', 3, '2008-10-28 10:28:26.630' UNION ALLSELECT '2008-10-14 13:10:06.690', 3, '2008-10-14 16:07:33.907' UNION ALLSELECT '2008-10-09 14:12:26.683', 3, '2008-10-09 14:14:43.623' UNION ALLSELECT '2008-10-24 18:23:33.820', 3, '2008-10-24 19:57:55.247' UNION ALLSELECT '2008-10-08 12:27:20.000', 3, '2008-10-08 13:20:57.060' UNION ALLSELECT '2008-10-13 09:33:18.080', 3, '2008-10-13 11:02:34.523' UNION ALLSELECT '2008-10-27 15:18:18.567', 3, '2008-10-27 15:18:18.567' UNION ALLSELECT '2008-10-21 19:52:35.743', 3, '2008-10-21 19:52:35.743' UNION ALLSELECT '2008-10-06 13:39:38.773', 3, '2008-10-06 14:59:52.030' UNION ALLSELECT '2008-10-06 15:19:09.617', 3, '2008-10-06 17:32:04.577' UNION ALLSELECT '2008-10-27 12:31:38.223', 3, '2008-10-27 12:31:38.223' UNION ALLSELECT '2008-10-08 10:07:25.020', 3, '2008-10-08 11:49:38.070' UNION ALLSELECT '2008-10-21 14:37:57.180', 3, '2008-10-21 14:38:01.103' UNION ALLSELECT '2008-10-07 17:25:41.363', 3, '2008-10-07 17:59:59.590' UNION ALLSELECT '2008-10-14 10:41:49.557', 3, '2008-10-14 15:39:58.720' UNION ALLSELECT '2008-10-10 08:31:55.483', 3, '2008-10-10 08:32:04.970' UNION ALLSELECT '2008-10-24 11:58:56.780', 3, '2008-10-24 18:03:11.233' UNION ALLSELECT '2008-10-15 10:03:00.143', 3, '2008-10-15 13:34:16.007' UNION ALLSELECT '2008-10-17 12:22:15.383', 3, '2008-10-17 15:05:16.583' UNION ALLSELECT '2008-10-15 13:41:59.423', 3, '2008-10-15 14:25:24.687' UNION ALLSELECT '2008-10-08 09:20:52.827', 3, '2008-10-08 12:07:04.010' UNION ALLSELECT '2008-10-22 12:46:34.877', 3, '2008-10-22 13:01:00.730' UNION ALLSELECT '2008-10-07 09:05:00.367', 3, '2008-10-07 14:12:56.010' UNION ALLSELECT '2008-10-22 09:33:36.633', 3, '2008-10-22 17:30:39.107' UNION ALLSELECT '2008-10-17 09:50:49.710', 3, '2008-10-17 10:15:58.450' UNION ALLSELECT '2008-10-07 13:20:46.153', 3, '2008-10-07 13:40:46.803' UNION ALLSELECT '2008-10-02 11:15:50.327', 3, '2008-10-02 12:14:11.270' UNION ALLSELECT '2008-10-24 20:14:00.987', 3, '2008-10-24 21:14:33.800' UNION ALLSELECT '2008-10-09 19:09:56.853', 3, '2008-10-09 19:09:58.853' UNION ALLSELECT '2008-10-07 11:33:07.070', 3, '2008-10-07 12:43:06.537' UNION ALLSELECT '2008-10-10 17:22:10.650', 3, '2008-10-10 19:52:04.173' UNION ALLSELECT '2008-10-10 12:25:33.967', 3, '2008-10-10 17:12:17.937' UNION ALLSELECT '2008-10-21 14:22:28.553', 3, '2008-10-21 18:09:38.897' UNION ALLSELECT '2008-10-24 09:56:40.023', 3, '2008-10-24 11:34:18.690' UNION ALLSELECT '2008-10-10 08:45:33.907', 3, '2008-10-10 12:12:27.500' UNION ALLSELECT '2008-10-10 11:28:01.520', 3, '2008-10-10 11:28:10.317' UNION ALLSELECT '2008-10-28 11:13:20.817', 3, '2008-10-28 11:13:20.817'SELECT userID, DATEPART(MONTH, timeAllocated) AS [Month], DATEPART(YEAR, timeAllocated) AS [Year], SUM(DATEDIFF(SECOND, timeAllocated, timeLastAccessed) / 360) AS NumberOfHours, SUM(DATEDIFF(SECOND, timeAllocated, timeLastAccessed) / 3600.0) AS PesoFROM @SampleWHERE timeAllocated > '01 october 2008' AND userID = 3GROUP BY userID, DATEPART(MONTH, timeAllocated), DATEPART(YEAR, timeAllocated)ORDER BY 3, 2 DESCuserID Month Year NumberOfHours Peso3 10 2008 811 82.703598 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-28 : 11:07:50
|
Excel is wrong as well... for example take a look at record #4. that's 5 hours along. It's somewhere around 82SELECT USERID,DATEPART(month,TimeAllocated) as "Month",DATEPART(year,TimeAllocated) as "Year",SUM(datediff(ss,TimeAllocated,TimeLastAccessed))/3600 as "NumberOfHours"FROM SESSIONSwhere timeallocated > '01 october 2008' and userid = '3'GROUP BY USERID,DATEPART(month,TimeAllocated),DATEPART(year,TimeAllocated)order by 3,2 desc |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-28 : 11:09:34
|
[code]timeAllocated userID timeLastAccessed Seconds Minutes Hours2008-10-16 11:46:28.910 3 2008-10-16 16:29:30.937 16982 283.033333 4.7172222008-10-07 13:40:57.803 3 2008-10-07 16:59:14.063 11897 198.283333 3.3047222008-10-09 18:07:36.310 3 2008-10-09 19:10:53.450 3797 63.283333 1.0547222008-10-09 10:25:42.460 3 2008-10-09 16:14:30.900 20928 348.800000 5.8133332008-10-13 16:49:29.437 3 2008-10-13 17:43:10.063 3221 53.683333 0.8947222008-10-28 10:28:26.630 3 2008-10-28 10:28:26.630 0 0.000000 0.0000002008-10-14 13:10:06.690 3 2008-10-14 16:07:33.907 10647 177.450000 2.9575002008-10-09 14:12:26.683 3 2008-10-09 14:14:43.623 137 2.283333 0.0380552008-10-24 18:23:33.820 3 2008-10-24 19:57:55.247 5662 94.366666 1.5727772008-10-08 12:27:20.000 3 2008-10-08 13:20:57.060 3217 53.616666 0.8936112008-10-13 09:33:18.080 3 2008-10-13 11:02:34.523 5356 89.266666 1.4877772008-10-27 15:18:18.567 3 2008-10-27 15:18:18.567 0 0.000000 0.0000002008-10-21 19:52:35.743 3 2008-10-21 19:52:35.743 0 0.000000 0.0000002008-10-06 13:39:38.773 3 2008-10-06 14:59:52.030 4814 80.233333 1.3372222008-10-06 15:19:09.617 3 2008-10-06 17:32:04.577 7975 132.916666 2.2152772008-10-27 12:31:38.223 3 2008-10-27 12:31:38.223 0 0.000000 0.0000002008-10-08 10:07:25.020 3 2008-10-08 11:49:38.070 6133 102.216666 1.7036112008-10-21 14:37:57.180 3 2008-10-21 14:38:01.103 4 0.066666 0.0011112008-10-07 17:25:41.363 3 2008-10-07 17:59:59.590 2058 34.300000 0.5716662008-10-14 10:41:49.557 3 2008-10-14 15:39:58.720 17889 298.150000 4.9691662008-10-10 08:31:55.483 3 2008-10-10 08:32:04.970 9 0.150000 0.0025002008-10-24 11:58:56.780 3 2008-10-24 18:03:11.233 21855 364.250000 6.0708332008-10-15 10:03:00.143 3 2008-10-15 13:34:16.007 12676 211.266666 3.5211112008-10-17 12:22:15.383 3 2008-10-17 15:05:16.583 9781 163.016666 2.7169442008-10-15 13:41:59.423 3 2008-10-15 14:25:24.687 2605 43.416666 0.7236112008-10-08 09:20:52.827 3 2008-10-08 12:07:04.010 9972 166.200000 2.7700002008-10-22 12:46:34.877 3 2008-10-22 13:01:00.730 866 14.433333 0.2405552008-10-07 09:05:00.367 3 2008-10-07 14:12:56.010 18476 307.933333 5.1322222008-10-22 09:33:36.633 3 2008-10-22 17:30:39.107 28623 477.050000 7.9508332008-10-17 09:50:49.710 3 2008-10-17 10:15:58.450 1509 25.150000 0.4191662008-10-07 13:20:46.153 3 2008-10-07 13:40:46.803 1200 20.000000 0.3333332008-10-02 11:15:50.327 3 2008-10-02 12:14:11.270 3501 58.350000 0.9725002008-10-24 20:14:00.987 3 2008-10-24 21:14:33.800 3633 60.550000 1.0091662008-10-09 19:09:56.853 3 2008-10-09 19:09:58.853 2 0.033333 0.0005552008-10-07 11:33:07.070 3 2008-10-07 12:43:06.537 4199 69.983333 1.1663882008-10-10 17:22:10.650 3 2008-10-10 19:52:04.173 8994 149.900000 2.4983332008-10-10 12:25:33.967 3 2008-10-10 17:12:17.937 17204 286.733333 4.7788882008-10-21 14:22:28.553 3 2008-10-21 18:09:38.897 13630 227.166666 3.7861112008-10-24 09:56:40.023 3 2008-10-24 11:34:18.690 5858 97.633333 1.6272222008-10-10 08:45:33.907 3 2008-10-10 12:12:27.500 12414 206.900000 3.4483332008-10-10 11:28:01.520 3 2008-10-10 11:28:10.317 9 0.150000 0.0025002008-10-28 11:13:20.817 3 2008-10-28 11:13:20.817 0 0.000000 0.000000[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|