Home > Blockchain >  Oracle SQL query for displaying customer firstname, Lastname and total invoices from customer and in
Oracle SQL query for displaying customer firstname, Lastname and total invoices from customer and in

Time:06-22

I am trying to display Customer firstname, lastname and total invoices for each customer in my query result. What am I missing? Can someone help? This is the design for the tables: enter image description here

This is what I wrote but getting this error: "FROM keyword not found where expected"

COUNT(INV_NUMBER) AS 'Number of Invoices'
FROM  (SELECT  CUS_CODE, L.INV_NUMBER AS INV_NUMBER
FROM  INVOICE I, LINE L    
WHERE  I.INV_NUMBER = L.INV_NUMBER   
GROUP BY CUS_CODE, L.INV_NUMBER) AS IL 
GROUP BY CUS_CODE;```

Desired result: first name, Last name, Total invoices ordered in descending order

CodePudding user response:

You need to join the customer and invoice tables by the common element - the customer code.

SELECT c.cus_fname, c.cus_lname, COUNT (*) AS "Inv Count"
FROM   customer c,
       invoice i
WHERE  c.cus_code = i.cus_code
GROUP BY c.cus_fname, c.cus_lname
ORDER BY 3 DESC

Descending order of ... ? Presumably number of invoices. You can't order on the alias but you can by the column position.

  • Related