Solved

SQL query using OVER(PARTITION...)

Posted on 2012-03-28
7
451 Views
Last Modified: 2012-03-30
I have a database with 400 tables.  Each table has the following columns with data types listed and a sample of the values in the columns.  Table T254, for example, looks like this:

[TIME], datetime(2):  2012-01-01 08:32:09.4410000
VALUE, real:  435.3914

There are several values for each day.  I want a result that includes the VALUE associated with the latest time of the last day of each month.  In other words max([time]), but for each month.  I have been trying to use OVER(partition by...), but I have not been able to get it to work.  The result set should look something like this:

YEAR          MONTH          VALUE
2012               1                 1234.9876
2012               2                 5432.2343
2012               3                 9999.9999

Where the value for each month represents the value column for the latest time on the last day of the month.  The times are irregular.  One month the last time may be 23:59:25.00000 and the next month it may be 22:34:52.00876.

How can I write a query to accomplish this?

T
0
Comment
Question by:thoecherl
  • 4
  • 3
7 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
This query will simply do:

SELECT YEAR([TIME]), MONTH([TIME]), MAX(VALUE)
FROM T254
GROUP BY YEAR([TIME]), MONTH([TIME])
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
Comment Utility
If you want to use several other columns in the SELECT clause, then use PARTITION BY query as listed below:

SELECT *
FROM (
SELECT *, VALUE, ROW_NUMBER() OVER ( PARTITION BY YEAR([TIME]), MONTH([TIME]) ORDER BY [TIME] DESC) RNUM
FROM T254 ) temp
WHERE RNUM = 1
0
 

Author Comment

by:thoecherl
Comment Utility
Thanks rrjegan17.  I don't think your first suggestion will work because it's not the MAX(VALUE) I am after, but the VALUE associated with the MAX(TIME) for each month.  I will have thousands of rows with a TIME and a VALUE column in each row for each month.  I want the result set to show only the value associated with the last entry for the month (MAX(TIME)).

I will try the second query as soon as I get to the client site and see what that does.  Thanks.

T
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

by:thoecherl
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for thoecherl's comment #37781724

for the following reason:

Thank you rrjegan17. &nbsp;The second query seems to work. &nbsp;I missed the ROW_NUMBER trick. &nbsp;I knew the OVER clause was the answer but without ROW_NUMBER, I couldn't get it to partition the way I wanted it to. &nbsp;Thanks again.<br /><br />T
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
Since you mentioned that my second query worked, I would request you to close the question by awarding the points to me..
Hence objecting to close the question..

Thank you rrjegan17.  The second query seems to work.  I missed the ROW_NUMBER trick.  I knew the OVER clause was the answer but without ROW_NUMBER, I couldn't get it to partition the way I wanted it to.  Thanks again.
0
 

Author Closing Comment

by:thoecherl
Comment Utility
This should work and give you the points, rrjegan17.  Thanks for your help.  I didn't mean to stiff you on the points.  An honest mistake -- still getting used to the new format of the site.

T
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
thoecherl,

No issues. Even I'm slightly confused with the new site and my activity is reduced out of that. :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now