Solved

Max of top numbers in database

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

Accepted Solution

by:
PortletPaul earned 500 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 48

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …

680 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