Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Max of top numbers in database

Posted on 2013-12-04
5
Medium Priority
?
149 Views
Last Modified: 2013-12-08
I have a database that contains several different columns including datatime, partnumber, metricname, and data.  I am trying to use an sqlcommand in vb.net that will give me the max(data) for just the top 30 records among many.  The where statement would include partnumber, and metricname.

How do I do this - ordering just top 30 records (desc order) and getting the max aggregate of that 'data' column?

Can I do this in one statement?

Thanks in advance for the help.
0
Comment
Question by:rckrch
  • 3
  • 2
5 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39696993
I think you can use the analytic form of MAX() here, like this
SELECT TOP 30
      datatime
    , partnumber
    , metricname
    , MAX(data) OVER (PARTITION BY (SELECT 1)) AS data
FROM your_table
WHERE partnumber = 'x'
      AND metricname = 'y'
ORDER BY
      datatime
      , partnumber

Open in new window

note the "select 1" inside the partition by is used to get the same value on each row (i.e. all rows are treated as the same "partition")
0
 

Author Comment

by:rckrch
ID: 39698554
Thanks very much for the reply PortletPaul, but this still does not work.  I inserted the code you have above, but the it still looks at all of the data to get the MAX value instead of just the top 30.

Am I using you code correctly?  I still don't understand what 'Select 1' is and how this partition works.  Should this 'Select 1' be a column name?

This is what I put in:

SELECT TOP 30 MAX(MeanValue) OVER(PARTITION BY (Select 1)) As MaxVal
FROM DataTable
WHERE DieNumber=@DieNumber, MetricName=@MetricName
ORDER BY RecordID DESC
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39699717
I thought this is what you wanted:
" it still looks at all of the data to get the MAX value instead of just the top 30."
so that is why I proposed it.

Try this:
SELECT
      datatime
    , partnumber
    , metricname
    , MAX(data) OVER (PARTITION BY (SELECT 1)) AS data
FROM (
        SELECT TOP 30
              datatime
            , partnumber
            , metricname
            , data
        FROM your_table
        WHERE partnumber = 'x'
              AND metricname = 'y'
        ORDER BY
              datatime
              , partnumber
     ) as derived

Open in new window

0
 

Author Comment

by:rckrch
ID: 39703959
This works perfectly.  Thanks for the help.  However, I would greatly appreciate it if you could explain how this works.  

1.  What does 'SELECT 1' do?  What is it selecting?
2.  What does 'as derived' do?

Thanks,
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39705197
"select 1" does exactly that, the number 1 is provided as the result.
It is used here as a "device" so that the partition by clause is valid.

I could have used any constant value
select 'x'
would have worked for example.

'as derived' is the alias I chose for the subquery, it could have been a different name e.g.

AS X

AS Y

AS abcd
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview

963 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