Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

How i implement a SQL instruction that makes this.

Hi folks,

I need a SQL instruction that shows the AVERAGE sell price of the last 4 purchase of some client.

I am using this:

=======================================================
SELECT FIRST 4 * FROM books WHERE customer='JOEL' ORDER BY DATE_INC DESC
=======================================================

But with this, i just select the last 4 purchases of my client JOEL. How i show the AVERAGE sell price of these 4 last purchases?

P.S.: DATE_INC saves the date when the customer buy something. My price field called PRICE.

I deeply need it.

Best Regards.
0
joelsilva
Asked:
joelsilva
  • 10
  • 6
  • 4
1 Solution
 
Ivanov_GCommented:
SELECT ... AVG(PRICE) ...
0
 
joelsilvaAuthor Commented:
Thx. for your comments.

I´ve tried that, but i didnt get it. I dont know how to add AVG(PRICE) in my code above. Do you know how to do it?
0
 
Ivanov_GCommented:
AVG(PRICE) should be in the place of column

if you write
SELECT PRICE FROM YOUR_TABLE
it will output only columns PRICE from YOUR_TABLE.

SELECT AVG(PRICE) FROM YOUR_TABLE
this will should you the average PRICE
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
joelsilvaAuthor Commented:
I know it friend,  but how to insert this AVG(PRICE) in my SQL instruction (SELECT FIRST 4 * FROM books WHERE customer='JOEL' ORDER BY DATE_INC DESC), to implement what a want?



0
 
Ivanov_GCommented:
try with

SELECT FIRST 4 AVG(PRICE)
FROM books
WHERE customer='JOEL'
ORDER BY DATE_INC DESC
0
 
joelsilvaAuthor Commented:
Hi and thx. very much...

That didnt work... The problem i in AVG(PRICE), because, if i replace AVG(PRICE) by *, it works but the result is not what i need.
0
 
Ivanov_GCommented:
If your DB implementation support nested selects, you can try this:

SELECT AVG(SUBSELECT.SUBSELECT_PRICE)
FROM
  ( SELECT FIRST 4 PRICE AS SUBSELECT_PRICE
    FROM BOOKS
    WHERE CUSTOMER='JOEL'
    ORDER BY DATE_INC DESC ) SUBSELECT
0
 
joelsilvaAuthor Commented:
No way man. I am using Firebird 1.5.
I deeply thank you by your insistance.
0
 
_Katka_Commented:
Hi,

joelsilva: I need a SQL instruction that shows the AVERAGE sell price of the last 4 purchase of some client.
Ivanov G: SELECT FIRST 4 AVG(PRICE) FROM books WHERE customer='JOEL' ORDER BY DATE_INC DESC
joelsilve: That didnt work... The problem i in AVG(PRICE), because, if i replace AVG(PRICE) by *, it works but the result is not what i need.
Kate: So why are you replacing that AVG by *, you wanted AVG price, that's done by exacly what Ivanov_G suggested..

I feel a bit of misunderstanding here.. joelsilva please specify what exactly is your need ;)

regards,
Kate
0
 
joelsilvaAuthor Commented:
That didnt worked... I just replace avg by * to confirm that the error was that...

What i´d like to do is what IVANOV wrote:
=================================
SELECT AVG(SUBSELECT.SUBSELECT_PRICE)
FROM
  ( SELECT FIRST 4 PRICE AS SUBSELECT_PRICE
    FROM BOOKS
    WHERE CUSTOMER='JOEL'
    ORDER BY DATE_INC DESC ) SUBSELECT
=================================

But it didnt worked.
0
 
_Katka_Commented:
Well..now it's clear :) so:

1) FireBird cannot make subselects (unfortunatelly)
2) The only solution is make a VIEW like this:

MY_VIEW:

select first 4
  PRICE
 from BOOKS
 where
  CUSTOMER='JOEL'
 order by
  DATE_INC desc

3) Then use this VIEW in select like this:

select AVG(PRICE)
 from MY_VIEW

That should do the work..you can combine usage of views to achieve different results
(for example returning average PRICE from VIEW directly and so)

regards,
Kate
0
 
joelsilvaAuthor Commented:
Ok... Thx again and i think that we will get it.

I put:
=============================================================
MY_VIEW: select first 4 PRICE from BOOKS where CUSTOMER='JOEL' order by DATE_INC desc
=============================================================

But i got the following error message: "Incorrect toke followed by ':'"

0
 
_Katka_Commented:
Well it was meant like this - create a view MY_VIEW with content:

select first 4 PRICE from BOOKS where CUSTOMER='JOEL' order by DATE_INC desc

;) Kate
0
 
joelsilvaAuthor Commented:
Katka, the only problem is: VIEWs doesnt allows the use of "ORDER BY".... :(
0
 
_Katka_Commented:
Sure in this certain case FireBird is a bit limited.. You'll have to use stored procedure (input parameter is name of customer, output is average value):

CREATE PROCEDURE MY_PROC (
    iNAME VARCHAR (32))
RETURNS (
    oRESULT FLOAT)
AS
  DECLARE VARIABLE vPRICE integer;
  DECLARE VARIABLE vCOUNT integer;
BEGIN
  oRESULT = 0; vCOUNT = 0;
  FOR
    select first 4 PRICE
     from BOOKS
     where CUSTOMER=iNAME
     order by DATE_INC desc
     into :vPRICE
   DO
    BEGIN
      oRESULT = oRESULT + vPRICE;
      vCOUNT = vCOUNT + 1;
    END
  oRESULT = oRESULT / vCOUNT;
  SUSPEND;
END

than execute it like: EXECUTE PROCEDURE MY_PROC ('JOEL') RETURNING_VALUES (your_variable_here)

regards,
Kate
0
 
joelsilvaAuthor Commented:
Oh!!! Thx. very much for your insistance!!!

I dont have Delphy right now, but i will test tonight.

Thx. again.

0
 
_Katka_Commented:
You're welcomed.. if I may, I'll offer you two more advices:

1) make a table CUSTOMERS with fields:

ID - unique identificator [integer]
NAME - name of customer [varchar]
..

and then use an ID field in 'where' condition instead of comparison of varchars, it's much more faster and purer ;)

2) ask questions about Firebird in DB/Interbase forum, you'll get answers much faster and also there's better chance to get some..

regards,
Kate :)
0
 
joelsilvaAuthor Commented:
Hi Katka,

Our code seems to work but with these two alterations:

=====================================
CREATE PROCEDURE MY_PROC (
    iNAME VARCHAR (32))
RETURNS (
    oRESULT FLOAT)
AS
  DECLARE VARIABLE vPRICE integer;
  DECLARE VARIABLE vCOUNT integer;
BEGIN
  oRESULT = 0; vCOUNT = 0;
  FOR
  select first 4 PRICE
     from BOOKS
     where CUSTOMER='Joel' // If i put "CUSTOMER=iNAME", i got a error message
                                          // I dropped the line "ORDER ..." because it gave a error message
                                          // I dropped the line "into :vPRICE" -> error message                
   DO
    BEGIN
      oRESULT = oRESULT + vPRICE;
      vCOUNT = vCOUNT + 1;
    END
  oRESULT = oRESULT / vCOUNT;
  SUSPEND;
END
=====================================

I see that you made your best, but it seems that Firebird has some particular details that dont allow us to implement that...

Thx. very much for now, and for your last advices too.
0
 
_Katka_Commented:
Hi,

 that stored procedure was running fine on FireBird 1.5.1, maybe the problem with iNAME was that it's to be :iNAME ;) otherwise it functions well in form:

CREATE PROCEDURE MY_PROC (
    INAME VARCHAR (32))
RETURNS (
    ORESULT FLOAT)
AS
  DECLARE VARIABLE vPRICE integer;
  DECLARE VARIABLE vCOUNT integer;
BEGIN
  oRESULT = 0; vCOUNT = 0;
  FOR
    select first 4 PRICE
     from CUSTOMER
     where CUSTOMER=:INAME
     order by DATE_INC desc
     into :vPRICE
   DO
    BEGIN
      oRESULT = oRESULT + vPRICE;
      vCOUNT = vCOUNT + 1;
    END
  IF (vCOUNT=0) THEN
  BEGIN
    oRESULT = 0;
  END ELSE
  BEGIN
    oRESULT = oRESULT / vCOUNT;
  END
  SUSPEND;
END

I also added 'ZERO DIVISION' check..for completeness..

regards,
Kate
0
 
joelsilvaAuthor Commented:
Hey KATE,

I deeply thank you for your special attention for my question.
Now your recomendation works fine and my problem is gone.

Don´t have words to tell you how i thank you, because that problem seemed doesnt have a solution...

Best regards for you!!!


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now