Solved

How i implement a SQL instruction that makes this.

Posted on 2004-08-15
20
150 Views
Last Modified: 2010-04-05
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
Comment
Question by:joelsilva
  • 10
  • 6
  • 4
20 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
SELECT ... AVG(PRICE) ...
0
 

Author Comment

by:joelsilva
Comment Utility
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
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
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
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
try with

SELECT FIRST 4 AVG(PRICE)
FROM books
WHERE customer='JOEL'
ORDER BY DATE_INC DESC
0
 

Author Comment

by:joelsilva
Comment Utility
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
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
No way man. I am using Firebird 1.5.
I deeply thank you by your insistance.
0
 
LVL 10

Expert Comment

by:_Katka_
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:_Katka_
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
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
 
LVL 10

Expert Comment

by:_Katka_
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
Katka, the only problem is: VIEWs doesnt allows the use of "ORDER BY".... :(
0
 
LVL 10

Expert Comment

by:_Katka_
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
Oh!!! Thx. very much for your insistance!!!

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

Thx. again.

0
 
LVL 10

Expert Comment

by:_Katka_
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
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
 
LVL 10

Accepted Solution

by:
_Katka_ earned 30 total points
Comment Utility
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
 

Author Comment

by:joelsilva
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now