Average Computation Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    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.
  • Alex

    #2
    Re: Average Computation Question

    hfamili@yahoo.c om (Alex) wrote in message news:<c55acd4f. 0410011405.39a4 48aa@posting.go ogle.com>...[color=blue]
    > 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.[/color]

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

    Comment

    • Dan Guzman

      #3
      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 GetAverageMinut es
      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.0 410012006.456a4 4d7@posting.goo gle.com...[color=blue]
      > hfamili@yahoo.c om (Alex) wrote in message
      > news:<c55acd4f. 0410011405.39a4 48aa@posting.go ogle.com>...[color=green]
      >> 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.[/color]
      >
      > Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
      > code that'll accomplish the above.[/color]


      Comment

      • Joe Celko

        #4
        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));
        [color=blue][color=green]
        >> I need to calculate End - Start for each unique ID (i.e. T3-T1 and[/color][/color]
        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!

        Comment

        • Alex

          #5
          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@nospa m-online.sbcgloba l.net> wrote in message news:<ZeA7d.660 $Al3.412@newssv r30.news.prodig y.com>...[color=blue]
          > 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 GetAverageMinut es
          > 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.0 410012006.456a4 4d7@posting.goo gle.com...[color=green]
          > > hfamili@yahoo.c om (Alex) wrote in message
          > > news:<c55acd4f. 0410011405.39a4 48aa@posting.go ogle.com>...[color=darkred]
          > >> 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.[/color]
          > >
          > > Ps: I am running SQL 2000 SP3 and am looking for the stored procedure
          > > code that'll accomplish the above.[/color][/color]

          Comment

          • Gert-Jan Strik

            #6
            Re: Average Computation Question

            Try this:

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

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

            Gert-Jan

            Alex wrote:[color=blue]
            >
            > 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@nospa m-online.sbcgloba l.net> wrote in message news:<ZeA7d.660 $Al3.412@newssv r30.news.prodig y.com>...[color=green]
            > > 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 GetAverageMinut es
            > > 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.0 410012006.456a4 4d7@posting.goo gle.com...[color=darkred]
            > > > hfamili@yahoo.c om (Alex) wrote in message
            > > > news:<c55acd4f. 0410011405.39a4 48aa@posting.go ogle.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.[/color][/color][/color]

            --
            (Please reply only to the newsgroup)

            Comment

            • Alex

              #7
              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@toomuchs pamalready.nl> wrote in message news:<4161A2F4. FC5373CB@toomuc hspamalready.nl >...[color=blue]
              > Try this:
              >
              > SELECT AVG(Duration)
              > FROM (
              > SELECT DATEDIFF(minute ,MIN(MyTimeStap ),MAX(MyTimeSta mp)) AS Duration
              > FROM MyTable
              > GROUP BY ID
              > ) AS T1
              >
              > By the way: this is not a homework assignment, is it?
              >
              > Gert-Jan
              >
              > Alex wrote:[color=green]
              > >
              > > 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@nospa m-online.sbcgloba l.net> wrote in message news:<ZeA7d.660 $Al3.412@newssv r30.news.prodig y.com>...[color=darkred]
              > > > 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 GetAverageMinut es
              > > > 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.0 410012006.456a4 4d7@posting.goo gle.com...
              > > > > hfamili@yahoo.c om (Alex) wrote in message
              > > > > news:<c55acd4f. 0410011405.39a4 48aa@posting.go ogle.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.[/color][/color][/color]

              Comment

              • Alex

                #8
                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@eart hlink.net> wrote in message news:<4160450e$ 0$26142$c397aba @news.newsgroup s.ws>...[color=blue]
                > 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));
                >[color=green][color=darkred]
                > >> I need to calculate End - Start for each unique ID (i.e. T3-T1 and[/color][/color]
                > 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![/color]

                Comment

                • Joe Celko

                  #9
                  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.
                  [color=blue][color=green]
                  >> I am not sure if you tried to solve my problem or found it easier[/color][/color]
                  (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 :)
                  [color=blue][color=green]
                  >> Ps: you were right about the DDL. I should have posted one. Sorry and[/color][/color]
                  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!

                  Comment

                  Working...