1

I'm currently trying to answer the following question:

Display the name of the customer who has purchased the most cars from Archie’s Luxury Motors.

Tables I'm working with:

Customer

(custID, name, DOB, streetAddress, suburb, postcode,
gender, phoneNo, email, type)

SalesTransaction

(VIN, custID, agentID, dateOfSale, agreedPrice)

My query

select * 
from (
select customer.name
from customer, salestransaction
where customer.custID = salestransaction.custID
group by (customer.name), salestransaction.custID
order by count(*) desc
)
where rownum=1;

Now I've found out that I cannot use analytic functions (rownum & rank)

How would I go about doing this with using pure transactional SQL only?

Rodi Marcus
  • 107
  • 1
  • 1
  • 9

1 Answers1

0

You could use MAX and COUNT aggregate functions:

WITH data AS
  (SELECT c.name cust_nm,
    COUNT(*) cnt
  FROM customer c,
    salestransaction s
  WHERE c.custID = s.custID
  GROUP BY c.NAME
  ORDER BY cnt DESC
  )
SELECT cust_nm FROM data WHERE cnt =
  (SELECT MAX(cnt) FROM DATA
  );

An example from EMP and DEPT table:

SQL> WITH data AS
  2    (SELECT e.deptno,
  3      COUNT(*) cnt
  4    FROM emp e,
  5      dept d
  6    WHERE e.deptno = d.deptno
  7    GROUP BY e.deptno
  8    ORDER BY cnt DESC
  9    )
 10  SELECT deptno FROM DATA WHERE cnt =
 11    (SELECT MAX(cnt) FROM DATA
 12    );

    DEPTNO
----------
        30

SQL>
Lalit Kumar B
  • 43,345
  • 10
  • 82
  • 112