473,854 Members | 1,847 Online

# Comparing data in two consecutive rows from a single table

I'm trying to come up with an elegant, simple way to compare two
consecutive values from the same table.

For instance:

SELECT TOP 2 datavalues FROM myTable ORDER BY timestamp DESC

That gives me the two latest values. I want to test the rate of
change of these values. If the top row is a 50% increase over the row
below it, I'll execute some special logic.

What are my options? The only ways I can think of doing this are
pretty ugly. Any help is very much appreciated. Thanks!

B.
Jul 20 '05
22 24680
>> So why not? I bet the underlying business problem looks a whole
like
that! <<

Copying a paper form directly into a RDBMS is not a good idea. This
problem sounds like a history; history is a series of durations.
You know, most people who are using databases are trying to solve

real-world problems, not do exercises in relational algebra. <<

That is like saying the people at Enron ignored math because they "are
trying to solve real-world problems, not do exercises in relational
algebra" :)
Jul 20 '05 #11
--CELKO-- wrote:
That is like saying the people at Enron ignored math because they "are
trying to solve real-world problems, not do exercises in relational
algebra" :)

Actually, they were trying to *avoid* real-world problems. ;)

Zach
Jul 20 '05 #12

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:18******** *************** **@posting.goog le.com...
I have to disagree here Joe. For example, one of our tables
records the time a banner ad is served. There's no "begin and end"
there's an instant. <<

Your banner ads have no duration? Wow! That means that if I want to
run 1000 ads, and a database that records times to 1/1000 of a second,
then I can put 1000 ads in each second :)

Serving them has no duration, no.

And we serve (and record the serving) of over 14 million banner ads a day.
I'll let you do the math.

Time intervals in SQL are shown as either (start, finish) and (start,
duration) -- look up the stuff for the OVERLAPS predicate in Standard
SQL.

See, I'm not talking about time intervals, you are.

Jul 20 '05 #13
>> Serving them has no duration, no. And we serve (and record the
serving) of over 14 million banner ads a day. <<

Perhaps I do not understand what "serving" means; can you give me a
scenario. I am a customer; I want to run a banner ad. My banner
needs to be up from Christmas to New Years day. It needs to run
between 0600 UTC to 1200 UTC everyday, and it is 5 seconds long. What
are you recording so that I get the exposure for which I am paying?
Jul 20 '05 #14
On 4 Jun 2004 12:25:52 -0700, --CELKO-- wrote:
Serving them has no duration, no. And we serve (and record the

serving) of over 14 million banner ads a day. <<

Perhaps I do not understand what "serving" means; can you give me a
scenario. I am a customer; I want to run a banner ad. My banner
needs to be up from Christmas to New Years day. It needs to run
between 0600 UTC to 1200 UTC everyday, and it is 5 seconds long. What
are you recording so that I get the exposure for which I am paying?

I should let Greg explain it himself, but I can't seem to hold myself back
....

The duration you describe would be just what is needed to select at any
given instant which ad(s) should be active. However, once the ad has been
selected and HTML has been emitted to have it appear on the page, the
instant that it was emitted will be recorded in the database. A time
instant, not a duration.
Jul 20 '05 #15

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:18******** *************** ***@posting.goo gle.com...
Serving them has no duration, no. And we serve (and record the

serving) of over 14 million banner ads a day. <<

Perhaps I do not understand what "serving" means; can you give me a
scenario. I am a customer; I want to run a banner ad. My banner
needs to be up from Christmas to New Years day. It needs to run
between 0600 UTC to 1200 UTC everyday, and it is 5 seconds long. What
are you recording so that I get the exposure for which I am paying?

Banners don't have a duration. (Well, active content do, but let's come
back to that.).

So, given the criteria you've given, we'd charge you probably (and this
varies but for this we simplify) on number of impressions. I.e the number
of times it's been served.

So an end user goes to a page on our site. The code (generally ASP) goes to
a DB table and queries "what banner should I show." (there may be more than
one scheduled for say the top position on the page). A "coin flip" is made
and the DB hands back a URL to the banner in question. At that point it
records in the database that your banner was served.

Now, the end user may spend 10 seconds at that page or 10 minutes. At that
point we don't really care (and have limited ways of knowing anyway since
the web is generally stateless). You're not paying for that, you're paying
for impressions served.

Now, a standard banner is generally just a gif file, or perhaps an animated
gif. Active content may be fancier, like a flash ad (yuck) or Quicktime,
etc. But again, we don't record any of that, simply the time that the
banner was handed to the user.

This is fairly standard in terms of how banner ads are served. So, no
duration, simply an impression.

Jul 20 '05 #16
>> Banners don't have a duration. (Well, active content do, but let's
come
back to that.) <<

Isn't that content and its duration what the buyer is paying for?
we'd charge you probably on number of impressions. I.e the number of times it's been served. <<

I understand billing by the number of hits. But if you can put 1000
impressions of my ad in banner in one minute, I am not as happy as I
would be having them spaced out and retained at lest long enough for a
human being to read. When I record the hits, I record them in a time
slot -- you can only hit the banner when it is displayed.
So an end user goes to a page on our site. The code (generally

ASP) goes to
a DB table and queries "what banner should I show." A "coin flip" is
and the DB hands back a URL to the banner in question. At that point
it
records in the database that your banner was served. Now, the end
user may spend 10 seconds at that page or 10 minutes. <<

At that point, I don't care; the user has left the banner and is in
the target URL. I then have to model his URL behavior in a new set of
tables.

But has happened back at the banner, which is what I was modeling? I
hope the banner was there for a duration greater than zero time units.
When my time slot of duration (t1) was used up, can I assume another
banner got a time slot of (t2) in that banner? You do not "machine
gun" banners so fast that they are not readable.

What you are saying is that "the half of the fact" you see is like a
shipping clerk -- packages only leave in his world view. Likewise a
receiving clerk only sees packages arriving in his world view. But
the whole fact is that package makes a trip that takes time until it
arrives someplace (or is declared lost in transit). The correct model
is global, not local.
Jul 20 '05 #17

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:18******** *************** ***@posting.goo gle.com...
Banners don't have a duration. (Well, active content do, but let's come
back to that.) <<

Isn't that content and its duration what the buyer is paying for?
No, since duration can't be measured via the web since it's stateless.

we'd charge you probably on number of impressions. I.e the number
of times it's been served. <<

I understand billing by the number of hits. But if you can put 1000
impressions of my ad in banner in one minute, I am not as happy as I
would be having them spaced out and retained at lest long enough for a
human being to read. When I record the hits, I record them in a time
slot -- you can only hit the banner when it is displayed.

Because that's all we can know.

But has happened back at the banner, which is what I was modeling? I
hope the banner was there for a duration greater than zero time units.
When my time slot of duration (t1) was used up, can I assume another
banner got a time slot of (t2) in that banner? You do not "machine
gun" banners so fast that they are not readable.

Banners are delivered as often as the clicks the page. If they go to a
page, read the page, walk away and come back 3 weeks later, the same banner
is still displayed. If they go to a page, quickly see it's not what they
want and click a link to another page, then they'll get a different banner.

You may HOPE the banner was there for duration greater than zero time units,
but generally the web doesn't work that way.

What you are saying is that "the half of the fact" you see is like a
shipping clerk -- packages only leave in his world view. Likewise a
receiving clerk only sees packages arriving in his world view. But
the whole fact is that package makes a trip that takes time until it
arrives someplace (or is declared lost in transit). The correct model
is global, not local.

No it's not. That may be what you want it to be, but that's not the way
banner traffic works.

Jul 20 '05 #18
Greg D. Moore (Strider) (mo************ ****@greenms.co m) writes:
So an end user goes to a page on our site. The code (generally ASP)
goes to a DB table and queries "what banner should I show." (there may
be more than one scheduled for say the top position on the page). A
"coin flip" is made and the DB hands back a URL to the banner in
question. At that point it records in the database that your banner was
served.

But, of course, at my end, I am running a banner-filter proxy, so I never
see the bastard anyway. :-)

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #19
>> Banners are delivered as often as the clicks the page. If they go
to a page, read the page, walk away and come back 3 weeks later, the
same banner is still displayed. If they go to a page, quickly see
it's not what they want and click a link to another page, then they'll
get a different banner. <<

I would make a big distiction between the banner (the thing that leads
to my Christmas sale) and the image of the banner that has presisted
for weeks after the holidays in the local storage of a particular
machine. My contract was for a duration (Christmas season) and was
with the website that offered to run my banner. They were to display
from one date to another. If it got clicked (n) times between
December 01 and December 25, then I owe them according to some
formula. Maybe if the click leads to sale between December 01 and
December 25, then I owe them according to another formula. But
outside that (possibly open ended) duration, there is no obligation.

Years ago in Los Angeles, I worked on a data model for a cable TV
shopping network for a major department store chain. This was even
worse because each time a commerical played, we had to compute the
actor's residuals, the assorted union pay rates, and how to credit the
purchase to the nearest local store, and the right department within
that store. Arrgh!
Jul 20 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.