title: SQL Server : LIMIT not working on complex query
body: <p>I have a rather long and complex SQL query that I'm working on at the moment.</p>
  
  <p>What I want to do is just add <code>LIMIT 10</code> ... But every time I end it to the end of the query I'm getting errors.</p>
  
  <p>Any help would be amazing, the query is as follows:</p>
  
  <pre><code>sqlQuery = "
    select      DATENAME(Month,i.Datecreated) + ' ' + 
                DATENAME(day,i.Datecreated) + ' ' + 
                DATENAME(year,i.Datecreated) AS USDateCreated,
                i.imageId,
                GalleryName,Fullpath,MediumPath,ThumbPath,ViewCounter,
                i.DateCreated,ItemNumber,Gender,Minutes,
                right(convert(varchar(3), 100 + Seconds),2) as Seconds,
                FramesPerSecond,WeekNumber,Filename,
                (round(cast(Size as Decimal(16,2))/1024,2)) as Size,
                FlvFilename,FlvSize,NumberOfMovies,
                Free,Comment,
                (case when sum(rating)/count(i.imageId) is null then 0 else sum(rating)/count(i.imageId) end) as ratingResult, 
                dbo.getTagNames(i.imageid) as tagsname,'' as yourTagNames,
                dbo.getTagNames(i.imageid) as memberTagNames,max(weekNumber)-1 as lastWeek 
  
    from        images as i 
  
    left join   Imagerating as ir on i.imageId = ir.imageId
  
    left join   tag as t on i.imageId = t.imageId where 1=1 
  
    and         galleryName = 'pictures' 
  
    and         weekNumber = '4' 
  
    group by    i.imageId,GalleryName,Fullpath,MediumPath,ThumbPath,
                ViewCounter,i.DateCreated,ItemNumber,Gender,Minutes,Seconds,
                FramesPerSecond,WeekNumber,Filename,Size,FlvFilename,FlvSize,
                NumberOfMovies,Free,Comment 
  
    order by    filename
  "
  </code></pre>