Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2008

Tip: Looking for answers? Try searching our database.

Dynamic date range for each row?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lee.richmond - 29 Jan 2008 18:46 GMT
Hi,

I'm trying to group data by date range, but each row of data could
have a different date range based on a variable.

I want to say "look at the date range the paste five orders were
placed" for each row individually. As an example, think of the rows as
keywords in a Search Marketing program. Keyword X had 5 orders placed
in the last week, Keyword Y had 5 orders placed in the last 2 weeks. I
want each keyword to display its average impressions over the course
of its respective date range.

Is this possible?

Thanks in advance!
Roy Harvey (SQL Server MVP) - 29 Jan 2008 20:08 GMT
I assume you are looking for something beyond just retrieving the last
five orders - say using the data in the Orders table to control the
data being retrieved from some other table entirely.

This gets the date range for the most recent five orders by customer.

SELECT CustomerID,
      MIN(OrderDate) as StartDate,
      MAX(OrderDate) as EndDate
 FROM Orders as A
WHERE OrderID IN
      (SELECT TOP 5 B.OrderID
         FROM Orders as B
        WHERE A.CustomerID = B.CustomerID
        ORDER BY B.OrderDate DESC)
GROUP BY CustomerID

I will leave it to you to apply that data to whatever table is
required.

Roy Harvey
Beacon Falls, CT

>Hi,
>
[quoted text clipped - 11 lines]
>
>Thanks in advance!
jefftyzzer - 30 Jan 2008 17:55 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> Thanks in advance!

Lee,

If (your version of) SQL Server implements them, you may want to look
at the windowing functions, and specifically the framing clause.
Here's an example of a simple moving average:

SELECT
  keyword,
  avg(qty) over (order by orderdate range between 5 preceding and
current row) as n
from
  orders

The important part is the "range between 5 preceding and current row"

--Jeff
Gert-Jan Strik - 30 Jan 2008 19:30 GMT
> > Hi,
> >
[quoted text clipped - 28 lines]
>
> --Jeff

Jeff,

According to SQL Server 2005's Books Online, aggregate window functions
only support partitioning by a column. IOW, AFAIK SQL Server does not
(yet?) support PRECEDING or CURRENT ROW as windowing selectors.

Signature

Gert-Jan

Erland Sommarskog - 30 Jan 2008 22:23 GMT
> According to SQL Server 2005's Books Online, aggregate window functions
> only support partitioning by a column. IOW, AFAIK SQL Server does not
> (yet?) support PRECEDING or CURRENT ROW as windowing selectors.

Yep. There is a request on ORDER BY for aggregates on Connect:
https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254387

There is also one for RANGE and ROWS
https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=254392

There are also a whole more requests for enhancements to the OVER clause,
most of them taken from the ANSI standard. You view a list on:
https://connect.microsoft.com/SQLServer/feedback/SearchResults.aspx?SearchQuery=
%22OVER+clause+enhancement+request%22


Would this come in SQL 2008 (it does not seem so), SQL 2008 would be
really hot.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.