Solved

Max of top numbers in database

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 43
sql server major issue  need help 2 80
I am new to using JSON in SQL Server 2 53
Search Text in Views 2 24
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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