1

I am to write a stored function to retrieve the number of items of a given product that have been delivered in the last year, and write a SQL Select statement that uses the function to display product no and name + no of items delivered in the last year for all products.

Please help me out

Thanks

8
  • 3
    SQL Server or Oracle?? Or both?? Also: please show us your table structure so we know what we're dealing with! Commented Aug 25, 2011 at 8:22
  • If possible oracle , Thanks for your quick reply Commented Aug 25, 2011 at 8:23
  • Can you please let me knoe how can i add table structure here, sorry i am quite new here Commented Aug 25, 2011 at 8:25
  • 1
    Do you want a stored function AND a SQL Select to use the function of just a single SQL statement to retrieve the correct data? Commented Aug 25, 2011 at 8:27
  • 2
    Is this a class assignment? Only it would in reality be much more efficient not to build and use a function to do this, but instead just to write the SQL query you need. Commented Aug 25, 2011 at 9:00

2 Answers 2

3

Presumably your question is prompted by a school or college assignment. At least I hope it is, because using a function for this purpose is bad practice. It performs badly, because of the constant switching between SQL and PL/SQL engines, and because it queries the deliveries on a row-by-row basis instead of using a set operation.

A simple pure SQL statement performs better. I have chosen to use an outer join because you probably want to include products which haven't had any activity in the last year.

select p.product_id
       , p.product_name
       , count(d.delivery_id) as sales_this_year 
from products p
    left outer join deliveries d
         on (p.product_id = d.product_id)
where d.dispatch_date >= add_months(sysdate, -12)
group by p.product_id, p.product_name
/

I have interpreted "in the last year" to mean the last twelve months, hence the use of the ADD_MONTHS() function. A different interpretation would have a WHERE clause like this:

where d.dispatch_date between to_date('01-JAN-2010') and to_date('31-DEC-2010')

that is, last calendar year; or perhaps:

where d.dispatch_date >= trunc(sysdate, 'YYYY')

that is, this calendar year.

Sign up to request clarification or add additional context in comments.

Comments

2

I suppose you have a delivery table and a product table.

create or replace function prod_delivered_cnt (p_prod_id in number) as
  v_res number;
begin
  select count(*) into v_res 
  from delivery_table d 
  where d.prod_id = p_prod_id 
  and d.date < to_date('2011', 'yyyy');
  return v_res;
end prod_delivered_cnt;

select p.prod_num, p.prod_name, prod_delivered_cnt(p.id) as cnt 
from product_table p;

4 Comments

Can you help me bit more please,create a function to retrieve the name of the manager for a employee table. Write a SQL select statement that uses the function to produce a list of employee numbers and names together with the name of their manager
Sorry sir, that would be a new question, which I may also answer once this one is closed/accepted correctly. Also, please note all those comments suggesting to provide more details with such questions. I mean you should provide the table structure and the DB vendor if you want to get an answer that contains working code.
Thanks, I will put this question as new one. Also I am quite new here and don't have much idea how to put the ERD image over here, Can you please help me , It would be highly appriciated
Your WHERE clause will select all deliveries made except those executed this year.

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.