?
Solved

SQL query using OVER(PARTITION...)

Posted on 2012-03-28
7
Medium Priority
?
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 37779984
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 2000 total points
ID: 37779989
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
ID: 37781724
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:thoecherl
ID: 37783025
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
ID: 37783026
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
ID: 37783494
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
ID: 37786600
thoecherl,

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

765 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