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. 8 1610 hf*****@yahoo.com (Alex) wrote in message news:<c5**************************@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.
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" <hf*****@yahoo.com> wrote in message
news:c5**************************@posting.google.c om... hf*****@yahoo.com (Alex) wrote in message news:<c5**************************@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.
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!
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" <gu******@nospam-online.sbcglobal.net> wrote in message news:<Ze***************@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" <hf*****@yahoo.com> wrote in message news:c5**************************@posting.google.c om... hf*****@yahoo.com (Alex) wrote in message news:<c5**************************@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.
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" <gu******@nospam-online.sbcglobal.net> wrote in message news:<Ze***************@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" <hf*****@yahoo.com> wrote in message news:c5**************************@posting.google.c om... hf*****@yahoo.com (Alex) wrote in message news:<c5**************************@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)
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 <so***@toomuchspamalready.nl> wrote in message news:<41***************@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" <gu******@nospam-online.sbcglobal.net> wrote in message news:<Ze***************@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" <hf*****@yahoo.com> wrote in message news:c5**************************@posting.google.c om... > hf*****@yahoo.com (Alex) wrote in message > news:<c5**************************@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.
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 <jc*******@earthlink.net> wrote in message news:<41**********************@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!
>> 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J |
last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller
program.
Heres's what is supposed to happen:
Roll 2 6-sided dies. Add rolls...
|
by: johnywalkyra |
last post by:
Hello,
first of all sorry for crossposting, but I could not decide which group
is more appropriate. To my question: Recently I've came across...
|
by: C++Geek |
last post by:
I need to get this program to average the salaries. What am I doing
wrong?
//Program to read in employee data and calculate the average...
|
by: sbowman |
last post by:
I have a table with help desk ticketing information. There is a
Date/Time open, Date/Time closed field both formatted as: MM/DD/YYYY
hh:nn:ss
I...
|
by: mochatrpl |
last post by:
I am looking for a way to make a query / report display the running
average for total dollars.
I have already set up a query to provide totals...
|
by: gaga |
last post by:
hi guys,
a part of my program requires me to calculate an average of items that
are sold. the easiest way to do that would be writing a function,...
|
by: Salad |
last post by:
http://www.mathwords.com/w/weighted_average.htm
At the above link gives an example of a weighted average. It uses the
following example:
...
|
by: vincex200 |
last post by:
My group needs help with this program. We attempted to start it and got no where. Please help us.
Write a C++ program that will read data from a...
|
by: Luna Moon |
last post by:
Dear all,
Can C++/STL/Boost do the vectorized calculation as those in Matlab?
For example, in the following code, what I really want to do is...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |