Solved

Max of top numbers in database

Posted on 2013-12-04
5
142 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 48

Expert Comment

by:PortletPaul
Comment Utility
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
Comment Utility
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 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
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
Comment Utility
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 48

Expert Comment

by:PortletPaul
Comment Utility
"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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now