1

Hoe can I do following thing without using a VIEW from one Query.

!--CREATE THE VIEW

CREATE OR REPLACE VIEW BDGTMGR
AS
  SELECT MANAGERID,SUM(BUDGET) AS BDGT FROM 
  N_DEPT GROUP BY MANAGERID ;

!-- THEN GET RESULT FROM THE VIEW

SELECT MANAGERID FROM BDGTMGR WHERE BDGT = (select MAX(BDGT) FROM BDGTMGR);

Here N_DEPT is may original Table which has columns named DID, MANAGERID and BUDGET.

I want to get MANAGERID who controls Maximum Budget. A Manager can control more than one Department. DID is the primary key for this table.

How can I do this?

3 Answers 3

2
select   MANAGERID,SUM(BDGT) 
from     N_DEPT 
group by MANAGERID 
order by SUM(BDGT) desc limit 1
Sign up to request clarification or add additional context in comments.

1 Comment

assuming you are using mysql placed limit, if you are using sql server use top 1 or if you are using oracle use rownum=1
1

You can do it like this:

SELECT aux.MANAGERID
FROM
  (SELECT MANAGERID,
         SUM(BUDGET) AS BDGT
   FROM N_DEPT
   GROUP BY MANAGERID) aux
INNER JOIN BDGTMGR b ON b.MANAGERID = aux.MANAGERID
WHERE b.BDGT = (select MAX(BDGT) FROM BDGTMGR);

Comments

0

The following query will work (modify depending on your flavour of SQL):

SELECT TOP 1 MANAGERID FROM N_DEPT GROUP BY MANAGERID ORDER BY SUM(BUDGET) DESC

1 Comment

this will get all managerid's, possibly you need to make a top 1 ;) (or limit)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.