Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First as DCookie suggested, ensure you have enabled output</p> <pre><code> set serveroutput on </code></pre> <p>If you dont get the result you are expecting, try breaking down the query until you find where the data is missing e.g. if there are no deals in the last year, you wont get anything back so select just one deal record which meets the criteria and then check the other tables for matching records. Also confirm there is data in each of the tables as if any of them is empty, again the result set will be empty.</p> <p>e.g. </p> <pre><code>select pno from deal where (sysdate - day) &lt; 365; </code></pre> <p>(The assumption is that day is a date field), then select this pno from the contact table</p> <pre><code>select peid from contact where pno=&amp;pno; </code></pre> <p>select this peid from person table and if any of those selects come up empty, check example values for the field to confirm the id numbers are actually matching between the tables e.g.</p> <pre><code>select count(*) from deal d where exists (select 1 from contact c where c.pno=d.pno) select count(*) from contact c where exists (select 1 from person p where p.peid=c.peid) </code></pre> <p>You are joining to the STAFF table but both STAFF and CONTACT have the same peid field and you arent selecting any columns from the STAFF table but you get firstname, lastname and staff number from the person table. </p> <p>Are there other rows in the person table for people who might be contacts on deals but are not staff (meaning the join to staff is to validate the type) ? if not then maybe the staff table is redundant here. </p> <p>When you have a complicated query (and they can get really complicated, like a couple of pages of A4 printed out), you have to have a method to breakdown the query to simpler queries that you can validate each step and this leads you to where the error occurs.</p> <p>Hope that helps.</p> <hr> <p>probably you should use the analytic functions so a query something like </p> <pre><code> select staff_number, staff_name from ( SELECT P.peid staff_number, P.firstname || ' ' || P.lastname staff_name , D.price PRICE RANK() OVER (ORDER BY d.price) AS position FROM Contact C, Deal D, Person P, Staff S WHERE S.peid = C.peid AND C.pno = D.pno AND P.peid = C.peid AND (SYSDATE - D.day &lt; 365) ) where position &lt; x </code></pre> <p>Use rank() if you want to skip positions where there are several people with a tied position or dense_rank() for having several records with the same position.</p> <hr> <p>just saw that you need it to sum deals for the same person so might be more like this (untested)</p> <pre><code> select staff_number, staff_name from ( SELECT P.peid staff_number, P.firstname || ' ' || P.lastname staff_name , sum(D.price) PRICE RANK() OVER (ORDER BY sum(d.price)) AS position FROM Contact C, Deal D, Person P, Staff S WHERE S.peid = C.peid AND C.pno = D.pno AND P.peid = C.peid AND (SYSDATE - D.day &lt; 365) group by p.peid, p.first_name|' '||p.lastname ) where position &lt; x </code></pre> <p>Definitely have a look at the analytic functions as rownum just returns the first rows in the query, it doesnt return the full result set and then give you the top N records. You may need to use a partition clause but give it a try and see here for examples of rank.</p> <p><a href="http://psoug.org/reference/analytic_functions.html" rel="nofollow">http://psoug.org/reference/analytic_functions.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload