Solved

SQL TOP 1 for 3 difference dates

Posted on 2011-09-05
3
312 Views
Last Modified: 2012-06-27
the following sql server 2008 command will produce 17 records for each dates ,  29-03-2011 (17 records), 30-03-2011 (17 records), 31-03-2011 (17 records),

trying to find a method to select TOP 1 for each of the 3 dates.
 
select smp.starttime as starttime, smp.id as smpid from StudentModulePeriod smp 
                                 INNER JOIN Lesson l ON smp.lid = l.id 
                                 INNER JOIN Module m ON smp.mid = m.id 
                                 INNER JOIN Period p ON smp.pid = p.id 
                                 INNER JOIN Student s ON smp.sid = s.id 
                                 INNER JOIN Trainer tr ON smp.trid = tr.id 
                                 WHERE DATEPART(month, smp.onmonth) = 03 AND 
                                 p.intakename = 'March 2011';

Open in new window

0
Comment
Question by:doramail05
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36483351
you can read this article to see suggestions/solutions ...
http://www.experts-exchange.com/A_3203.html

you shall probably use the ROW_NUMBER() partitioning clause in a inner query to get this working
0
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 500 total points
ID: 36483385
You need to think about the basis for selecting the top3.

The following query uses the ROW_NUMBER function to allocate an index (rn) to each record.  We then select just those records <=3.

>  In my example the row numbering resets (to 1) whenever the day part of the onMonth value changes - this is the PARTITION BY clause.
>  The ordering is determined (unsurprisingly:))  by the ORDER BY clause.

You should tailor the call to ROW_NUMBER to implement your logic for selecting the top 3

Select * FROM
(
  select smp.starttime as starttime, smp.id as smpid
  , ROW_NUMBER() OVER (PARTITION BY DATEPART(day,smp.onmonth) ORDER BY p.id) as rn
  from StudentModulePeriod smp
  INNER JOIN Lesson l ON smp.lid = l.id
  INNER JOIN Module m ON smp.mid = m.id
  INNER JOIN Period p ON smp.pid = p.id
  INNER JOIN Student s ON smp.sid = s.id
  INNER JOIN Trainer tr ON smp.trid = tr.id
  WHERE DATEPART(month, smp.onmonth) = 03 AND
  p.intakename = 'March 2011';
) ILV
Where rn<=3
0
 
LVL 1

Author Comment

by:doramail05
ID: 36483522
should be select top 1 for each threes,
putting  rn <= 1;

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

17 Experts available now in Live!

Get 1:1 Help Now