Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:25 AM
Alex
 
Posts: n/a
Default Average Computation Question

My table is laid out as such:

ID (int) What (varchar 20) TimeStamp (smalldatetime)
------- ------------- ---------------
73 Start <T1>
73 Misc <T2>
73 End <T3>
81 Start <T1'>
81 Misc <T2'>
81 End <T3'>
....

I need to calculate End - Start for each unique ID (i.e. T3-T1 and
T3'-T1') and then take the average of those (2 in this case) entries.

Any help is appreciated.

Alex.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:25 AM
Alex
 
Posts: n/a
Default Re: Average Computation Question

hfamili@yahoo.com (Alex) wrote in message news:<c55acd4f.0410011405.39a448aa@posting.google. com>...
> My table is laid out as such:
>
> ID (int) What (varchar 20) TimeStamp (smalldatetime)
> ------- ------------- ---------------
> 73 Start <T1>
> 73 Misc <T2>
> 73 End <T3>
> 81 Start <T1'>
> 81 Misc <T2'>
> 81 End <T3'>
> ...
>
> I need to calculate End - Start for each unique ID (i.e. T3-T1 and
> T3'-T1') and then take the average of those (2 in this case) entries.
>
> Any help is appreciated.
>
> Alex.


Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
code that'll accomplish the above.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:26 AM
Dan Guzman
 
Posts: n/a
Default Re: Average Computation Question

Try something like:

CREATE TABLE MyTable
(
ID int NOT NULL,
What varchar(5) NOT NULL,
MyTimeStamp smalldatetime NOT NULL,
CONSTRAINT PK_MyTable PRIMARY KEY (ID, What)
)
GO

INSERT INTO MyTable VALUES(73, 'Start', '20040901')
INSERT INTO MyTable VALUES(73, 'Misc', '20040905')
INSERT INTO MyTable VALUES(73, 'End', '20040909')
INSERT INTO MyTable VALUES(81, 'Start', '20040915')
INSERT INTO MyTable VALUES(81, 'Misc', '20040917')
INSERT INTO MyTable VALUES(81, 'End', '20040919')
GO

CREATE PROCEDURE GetAverageMinutes
AS
SELECT
AVG(DATEDIFF(mi, a.MyTimeStamp, b.MyTimeStamp)) AS AverageMinutes
FROM MyTable a
JOIN MyTable b ON
b.ID = a.ID AND
a.What = 'Start' AND
b.What = 'End'
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Alex" <hfamili@yahoo.com> wrote in message
news:c55acd4f.0410012006.456a44d7@posting.google.c om...
> hfamili@yahoo.com (Alex) wrote in message
> news:<c55acd4f.0410011405.39a448aa@posting.google. com>...
>> My table is laid out as such:
>>
>> ID (int) What (varchar 20) TimeStamp (smalldatetime)
>> ------- ------------- ---------------
>> 73 Start <T1>
>> 73 Misc <T2>
>> 73 End <T3>
>> 81 Start <T1'>
>> 81 Misc <T2'>
>> 81 End <T3'>
>> ...
>>
>> I need to calculate End - Start for each unique ID (i.e. T3-T1 and
>> T3'-T1') and then take the average of those (2 in this case) entries.
>>
>> Any help is appreciated.
>>
>> Alex.

>
> Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
> code that'll accomplish the above.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:26 AM
Joe Celko
 
Posts: n/a
Default Re: Average Computation Question

Your design is fundamentally wrong. The flaw is called "attribute
splitting" and you can Google it. Time comes in durations and not
points (see Einstein and Zeno for the details). The DDL that you did
not post should have looked more like this:

CREATE TABLE Foobar
(event_id INTEGRR NOT NULL PRIMARY KEY,
event-description VARCHAR(20) NOT NULL,
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP NOT NULL,
CHECK (start_time < end_time));

>> I need to calculate End - Start for each unique ID (i.e. T3-T1 and

T3'-T1') and then take the average of those (2 in this case) entries. <<

Since you used Standard SQL TIMESTAMP in your pseudo-code, here is the
trivial answer:

SELECT AVG(INTERVAL (end_time - start_time) SECONDS)
FROM Foobar;

A proper design saves orders of magnitude in the queries.

Your problem is that: (1) you do not understand time; no great shame
there, since most people get it messed up (2) You designed a table to
mimick a paper form, namely the list you used for keeping track of
things. Think more abstractly; one attribute can be split in many
fields on the non-relational side, but must bre put into one and only
one column when it gets to the database.

The other answers you get will be fancy self-joins that bring the
durations make from the attribute split.

--CELKO--
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 04:26 AM
Alex
 
Posts: n/a
Default Re: Average Computation Question

Dan, that's beautiful. Thanks. I have a follow up question:

If the "End" time stamp were not unique, meaning that the "End" time
stamp could occur multiple times and I had to take the last one for
the purposes of the average computation what would the SQL look like
then?

Thanks again.

Alex.

"Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message news:<ZeA7d.660$Al3.412@newssvr30.news.prodigy.com >...
> Try something like:
>
> CREATE TABLE MyTable
> (
> ID int NOT NULL,
> What varchar(5) NOT NULL,
> MyTimeStamp smalldatetime NOT NULL,
> CONSTRAINT PK_MyTable PRIMARY KEY (ID, What)
> )
> GO
>
> INSERT INTO MyTable VALUES(73, 'Start', '20040901')
> INSERT INTO MyTable VALUES(73, 'Misc', '20040905')
> INSERT INTO MyTable VALUES(73, 'End', '20040909')
> INSERT INTO MyTable VALUES(81, 'Start', '20040915')
> INSERT INTO MyTable VALUES(81, 'Misc', '20040917')
> INSERT INTO MyTable VALUES(81, 'End', '20040919')
> GO
>
> CREATE PROCEDURE GetAverageMinutes
> AS
> SELECT
> AVG(DATEDIFF(mi, a.MyTimeStamp, b.MyTimeStamp)) AS AverageMinutes
> FROM MyTable a
> JOIN MyTable b ON
> b.ID = a.ID AND
> a.What = 'Start' AND
> b.What = 'End'
> GO
>
> --
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP
>
> "Alex" <hfamili@yahoo.com> wrote in message
> news:c55acd4f.0410012006.456a44d7@posting.google.c om...
> > hfamili@yahoo.com (Alex) wrote in message
> > news:<c55acd4f.0410011405.39a448aa@posting.google. com>...
> >> My table is laid out as such:
> >>
> >> ID (int) What (varchar 20) TimeStamp (smalldatetime)
> >> ------- ------------- ---------------
> >> 73 Start <T1>
> >> 73 Misc <T2>
> >> 73 End <T3>
> >> 81 Start <T1'>
> >> 81 Misc <T2'>
> >> 81 End <T3'>
> >> ...
> >>
> >> I need to calculate End - Start for each unique ID (i.e. T3-T1 and
> >> T3'-T1') and then take the average of those (2 in this case) entries.
> >>
> >> Any help is appreciated.
> >>
> >> Alex.

> >
> > Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
> > code that'll accomplish the above.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 04:26 AM
Gert-Jan Strik
 
Posts: n/a
Default Re: Average Computation Question

Try this:

SELECT AVG(Duration)
FROM (
SELECT DATEDIFF(minute,MIN(MyTimeStap),MAX(MyTimeStamp)) AS Duration
FROM MyTable
GROUP BY ID
) AS T1

By the way: this is not a homework assignment, is it?

Gert-Jan

Alex wrote:
>
> Dan, that's beautiful. Thanks. I have a follow up question:
>
> If the "End" time stamp were not unique, meaning that the "End" time
> stamp could occur multiple times and I had to take the last one for
> the purposes of the average computation what would the SQL look like
> then?
>
> Thanks again.
>
> Alex.
>
> "Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message news:<ZeA7d.660$Al3.412@newssvr30.news.prodigy.com >...
> > Try something like:
> >
> > CREATE TABLE MyTable
> > (
> > ID int NOT NULL,
> > What varchar(5) NOT NULL,
> > MyTimeStamp smalldatetime NOT NULL,
> > CONSTRAINT PK_MyTable PRIMARY KEY (ID, What)
> > )
> > GO
> >
> > INSERT INTO MyTable VALUES(73, 'Start', '20040901')
> > INSERT INTO MyTable VALUES(73, 'Misc', '20040905')
> > INSERT INTO MyTable VALUES(73, 'End', '20040909')
> > INSERT INTO MyTable VALUES(81, 'Start', '20040915')
> > INSERT INTO MyTable VALUES(81, 'Misc', '20040917')
> > INSERT INTO MyTable VALUES(81, 'End', '20040919')
> > GO
> >
> > CREATE PROCEDURE GetAverageMinutes
> > AS
> > SELECT
> > AVG(DATEDIFF(mi, a.MyTimeStamp, b.MyTimeStamp)) AS AverageMinutes
> > FROM MyTable a
> > JOIN MyTable b ON
> > b.ID = a.ID AND
> > a.What = 'Start' AND
> > b.What = 'End'
> > GO
> >
> > --
> > Hope this helps.
> >
> > Dan Guzman
> > SQL Server MVP
> >
> > "Alex" <hfamili@yahoo.com> wrote in message
> > news:c55acd4f.0410012006.456a44d7@posting.google.c om...
> > > hfamili@yahoo.com (Alex) wrote in message
> > > news:<c55acd4f.0410011405.39a448aa@posting.google. com>...
> > >> My table is laid out as such:
> > >>
> > >> ID (int) What (varchar 20) TimeStamp (smalldatetime)
> > >> ------- ------------- ---------------
> > >> 73 Start <T1>
> > >> 73 Misc <T2>
> > >> 73 End <T3>
> > >> 81 Start <T1'>
> > >> 81 Misc <T2'>
> > >> 81 End <T3'>
> > >> ...
> > >>
> > >> I need to calculate End - Start for each unique ID (i.e. T3-T1 and
> > >> T3'-T1') and then take the average of those (2 in this case) entries.
> > >>
> > >> Any help is appreciated.
> > >>
> > >> Alex.
> > >
> > > Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
> > > code that'll accomplish the above.


--
(Please reply only to the newsgroup)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 04:27 AM
Alex
 
Posts: n/a
Default Re: Average Computation Question

Got it going. Thanks. The insight was really the self-join that Dan
mentioned. It was a minor tweak to get the rest working. Thanks for
all your help. And no it's not a homework assignment.

Gert-Jan Strik <sorry@toomuchspamalready.nl> wrote in message news:<4161A2F4.FC5373CB@toomuchspamalready.nl>...
> Try this:
>
> SELECT AVG(Duration)
> FROM (
> SELECT DATEDIFF(minute,MIN(MyTimeStap),MAX(MyTimeStamp)) AS Duration
> FROM MyTable
> GROUP BY ID
> ) AS T1
>
> By the way: this is not a homework assignment, is it?
>
> Gert-Jan
>
> Alex wrote:
> >
> > Dan, that's beautiful. Thanks. I have a follow up question:
> >
> > If the "End" time stamp were not unique, meaning that the "End" time
> > stamp could occur multiple times and I had to take the last one for
> > the purposes of the average computation what would the SQL look like
> > then?
> >
> > Thanks again.
> >
> > Alex.
> >
> > "Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message news:<ZeA7d.660$Al3.412@newssvr30.news.prodigy.com >...
> > > Try something like:
> > >
> > > CREATE TABLE MyTable
> > > (
> > > ID int NOT NULL,
> > > What varchar(5) NOT NULL,
> > > MyTimeStamp smalldatetime NOT NULL,
> > > CONSTRAINT PK_MyTable PRIMARY KEY (ID, What)
> > > )
> > > GO
> > >
> > > INSERT INTO MyTable VALUES(73, 'Start', '20040901')
> > > INSERT INTO MyTable VALUES(73, 'Misc', '20040905')
> > > INSERT INTO MyTable VALUES(73, 'End', '20040909')
> > > INSERT INTO MyTable VALUES(81, 'Start', '20040915')
> > > INSERT INTO MyTable VALUES(81, 'Misc', '20040917')
> > > INSERT INTO MyTable VALUES(81, 'End', '20040919')
> > > GO
> > >
> > > CREATE PROCEDURE GetAverageMinutes
> > > AS
> > > SELECT
> > > AVG(DATEDIFF(mi, a.MyTimeStamp, b.MyTimeStamp)) AS AverageMinutes
> > > FROM MyTable a
> > > JOIN MyTable b ON
> > > b.ID = a.ID AND
> > > a.What = 'Start' AND
> > > b.What = 'End'
> > > GO
> > >
> > > --
> > > Hope this helps.
> > >
> > > Dan Guzman
> > > SQL Server MVP
> > >
> > > "Alex" <hfamili@yahoo.com> wrote in message
> > > news:c55acd4f.0410012006.456a44d7@posting.google.c om...
> > > > hfamili@yahoo.com (Alex) wrote in message
> > > > news:<c55acd4f.0410011405.39a448aa@posting.google. com>...
> > > >> My table is laid out as such:
> > > >>
> > > >> ID (int) What (varchar 20) TimeStamp (smalldatetime)
> > > >> ------- ------------- ---------------
> > > >> 73 Start <T1>
> > > >> 73 Misc <T2>
> > > >> 73 End <T3>
> > > >> 81 Start <T1'>
> > > >> 81 Misc <T2'>
> > > >> 81 End <T3'>
> > > >> ...
> > > >>
> > > >> I need to calculate End - Start for each unique ID (i.e. T3-T1 and
> > > >> T3'-T1') and then take the average of those (2 in this case) entries.
> > > >>
> > > >> Any help is appreciated.
> > > >>
> > > >> Alex.
> > > >
> > > > Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
> > > > code that'll accomplish the above.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 04:27 AM
Alex
 
Posts: n/a
Default Re: Average Computation Question

Joe, although you seem to have a good grasp of time and its nuances, I
am not sure if you tried to solve my problem or found it easier (i.e.
less time) to solve your own. But thanks for the brief time you
alloted to my post and replying. A.

Ps: you were right about the DDL. I should have posted one. Sorry
and will do better next time.

Joe Celko <jcelko212@earthlink.net> wrote in message news:<4160450e$0$26142$c397aba@news.newsgroups.ws> ...
> Your design is fundamentally wrong. The flaw is called "attribute
> splitting" and you can Google it. Time comes in durations and not
> points (see Einstein and Zeno for the details). The DDL that you did
> not post should have looked more like this:
>
> CREATE TABLE Foobar
> (event_id INTEGRR NOT NULL PRIMARY KEY,
> event-description VARCHAR(20) NOT NULL,
> start_time TIMESTAMP NOT NULL,
> end_time TIMESTAMP NOT NULL,
> CHECK (start_time < end_time));
>
> >> I need to calculate End - Start for each unique ID (i.e. T3-T1 and

> T3'-T1') and then take the average of those (2 in this case) entries. <<
>
> Since you used Standard SQL TIMESTAMP in your pseudo-code, here is the
> trivial answer:
>
> SELECT AVG(INTERVAL (end_time - start_time) SECONDS)
> FROM Foobar;
>
> A proper design saves orders of magnitude in the queries.
>
> Your problem is that: (1) you do not understand time; no great shame
> there, since most people get it messed up (2) You designed a table to
> mimick a paper form, namely the list you used for keeping track of
> things. Think more abstractly; one attribute can be split in many
> fields on the non-relational side, but must bre put into one and only
> one column when it gets to the database.
>
> The other answers you get will be fancy self-joins that bring the
> durations make from the attribute split.
>
> --CELKO--
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, datatypes, etc. in your
> schema are. Sample data is also a good idea, along with clear
> specifications.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 04:27 AM
Joe Celko
 
Posts: n/a
Default Re: Average Computation Question

>> Joe, although you seem to have a good grasp of time and its nuances,
<<

Lord, no! That guy is Rick Snodgrass. his book on temproal queries in
SQL is on-line at his website at the University of AZ. I can simply
recognize the most common basic problems in DDL by sight now; I make a
part of my living fixing databases that look like what you posted.

>> I am not sure if you tried to solve my problem or found it easier

(i.e.less time) to solve your own. <<

I published this kind of solution in SQL FOR SMARTIES, SQL PUZZLES and
several magazine columns years ago when I was still thinking of time as
points and not durations. How else would I know that there would have
to be an elaborate and error-prone self-join in whatever kludge got
posted?

Your problem *is* the design and that is the root of the difficulty in
even this simple query. It will get orders of magntiude worse. The
elaborate self-joins eat up time exponentially with DB size. A single
missing row throws reports off. Gaps are hard to detect.

I know; I have been paid to fix it before at a research company working
with a bank to look for patterns in checking account and credit card
balances. Hiring me for a month is expensive

>> Ps: you were right about the DDL. I should have posted one. Sorry and

will do better next time. <<

Nada. The number of frequent posters who have been asked over and over
and still will not post DDL is remarkable. Then of course there are the
guys who push a button and dump code in a format that only a machine
could love ..

--CELKO--
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:29 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556