Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Max of top numbers in database

Posted on 2013-12-04
5
Medium Priority
?
148 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 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