Solved

SQL TOP 1 for 3 difference dates

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 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

13 Experts available now in Live!

Get 1:1 Help Now