0

I am having no luck attempting to get the top (x number) of rows from a joined table. I want the top 2 resources (ordered by name) which in this case should be Katie and Simon and regardless of what I've tried, I can't seem to get it right. You can see below what I've commented out - and what looks like it should work (but doesn't). I cannot use a union. Any ideas?

enter image description here

select distinct 

RTRESOURCE.RNAME as Resource,

RTTASK.TASK  as taskname, SUM(distinct SOTRAN.QTY2BILL) AS quantitytobill from SOTRAN AS SOTRAN  INNER JOIN RTTASK AS RTTASK ON sotran.taskid = rttask.taskid 

left outer JOIN RTRESOURCE AS RTRESOURCE ON rtresource.keyno=sotran.resid 

WHERE sotran.phantom<>'y'  and sotran.pgroup = 'L' and sotran.timesheet = 'y' and sotran.taskid >0 AND RTRESOURCE.KEYNO in ('193','159','200') AND ( SOTRAN.ADDDATE>='8/15/2015 12:00:00 AM' AND SOTRAN.ADDDATE<'9/3/2015 11:59:59 PM' ) 

//and RTRESOURCE.RNAME in ( select distinct top 2 RTRESOURCE.RNAME from  RTRESOURCE order by RTRESOURCE.RNAME)
//and ( select count(*) from RTRESOURCE RTRESOURCE2 where RTRESOURCE2.RNAME = RTRESOURCE.RNAME ) <= 2

GROUP BY RTRESOURCE.rname,RTTASK.task,RTTASK.taskid,RTTASK.mdsstring  ORDER BY Resource,taskname 
4
  • what makes katie and simon the "top 2 names" you can't rely on random sql ordering for "top 2". Is that number column on the left an ID column? Is that what makes them the top 2? Can you provide your table schemas for all related tables?
    – Kritner
    Sep 4, 2015 at 16:13
  • 1
    You mean top two Resources with highest quantity to bill ? usually is easy if you post your desire output. Also what is your rdbms? Sep 4, 2015 at 16:14
  • No, just the names. If I have 10 names come back I just want the first 2 (according to alpha). Sep 4, 2015 at 16:17
  • Use a where sub query like: where RTRESOURCE.RNAME in (select top 2 RTRESOURCE.RNAME from left outer JOIN RTRESOURCE AS RTRESOURCE ON rtresource.keyno=sotran.resid ...... GROUP BY RTRESOURCE.rname ORDER BY SUM(distinct SOTRAN.QTY2BILL) )
    – Jason Lou
    Sep 4, 2015 at 16:17

1 Answer 1

0

You should provide a schema.

But lets assume your query work. You create a CTE.

WITH youQuery as (
   SELECT * 
   FROM < you  big join query>
), maxBill as (
   SELECT Resource, Max(quantitytobill) as Bill
   FROM yourQuery
)
SELECT top 2 *
FROM maxBill
ORDER BY Bill

IF you want top 2 alphabetical

WITH youQuery as (
   SELECT * 
   FROM < you  big join query>
), Names as (
   SELECT distinct Resource
   FROM yourQuery
   Order by Resource
)
SELECT top 2 *
FROM Names
5
  • @vkp Done thanks. Im wondering if I can do top 2 distinct and order all in same query for alphabetical result? Sep 4, 2015 at 16:22
  • That is why I ask for your rdbms :(. is MySQL ? Sep 4, 2015 at 16:23
  • Did you solve your problem? Otherwise let me know what rdbms are you using so I can fix the query properly. Sep 8, 2015 at 20:32
  • No, " Cannot create CTE. – triplethreat77 Sep 4 at 16:22 " Sep 9, 2015 at 15:14
  • Yes, and I ask what is your rdbms. Maybe your doesnt support cte? Sep 9, 2015 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.