Solved

SQL TOP 1 for 3 difference dates

Posted on 2011-09-05
3
316 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
[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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach 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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

696 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