Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2006, 05:55 AM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default (Resolved) Select top10 problems

I have the following data structure

Date name value
1-jan-2006 abc 200
1-jan-2006 bcd 400
etc
etc
31-mar-2006 abc 700

so I will have lots of data for each day, what I need to do is find the the min and max sum(top 10 value per day) .. It is fairly trivial to get the top 10, but I am struggeling with how to get this data on a per day basis. I am using SQL.

I was thinking that I would need to loop through the data and create a temp table containing the sum of top ten values on a daily basis, then requery this table to find the min and max.

can anyone suggest the best way to do this. can it be done in one select statement?
 
Old April 12th, 2006, 09:34 AM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Problem Sorted.. I thought I would post my result here

Code:
SELECT 
    MAX(MyVal_Sum) 'Max Top 10', 
    MIN(MyVal_Sum) 'Min Top 10' 

FROM 
    (SELECT SUM(d.MyVal) MyVal_Sum,d.MyDate 
    FROM MyTable d 
    WHERE MyIdent in (SELECT TOP 10 c.MyIdent 
                FROM 
                    MyTable c 
                WHERE c.MyDate= d.MyDate 
                ORDER BYc.MyVal desc) 

    GROUP BY d.MyDate) temptable







Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] Combine select from different platforms snufse SQL Server 2005 5 November 14th, 2008 06:08 AM
Having Problems with my Insert Command - RESOLVED Andrew.Berry ASP.NET 2.0 Basics 1 October 2nd, 2007 09:59 AM
Problems with Select & Where statement sharon5469 ASP.NET 1.0 and 1.1 Basics 3 June 11th, 2007 07:09 PM
SELECT Problems Thpuffer Classic ASP Databases 1 June 19th, 2006 05:06 PM
Problems with SELECT DISTINCT dparsons SQL Server 2000 2 April 2nd, 2006 03:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.