Solved

SQL TOP 1 for 3 difference dates

Posted on 2011-09-05
3
315 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 143

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 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