0

I want to make a query to list cats that took longer than average cats to sell? I have five tables:

Animal, Sale, AnimalOrderItem, AnimalOrder, and SaleAnimal

Animal table: AnimalID, Name, Category (cat, dog, fish)

SaleAnimal table: SaleID, AnimalID, SalePrice

Sale table: SaleID, date, employeeID, CustomerID

AnimalOrderItem table: OrderID, AnimalID, cost

AnimalOrder: OrderID, OrderDate, ReceivingDate, SupplierID, ShippingCost, EmployeeID

There is other tables I don’t think they have an effect on the query.

I thought of the following ... make a query to calculate days to sell for all ex.:

[SaleDate]-[ReceiveDate] AS DaysToSell
Have the INNER JOIN built:
Sale INNER JOIN ((AnimalOrder INNER JOIN (Animal INNER JOIN AnimalOrderItem
ON Animal.AnimalID = AnimalOrderItem.AnimalID) ON AnimalOrder.
OrderID = AnimalOrderItem.OrderID) INNER JOIN SaleAnimal ON Animal.
AnimalID = SaleAnimal.AnimalID) ON Sale.SaleID = SaleAnimal.SaleID

Create another query based on the above query

SELECT AnimalID, Name, Category, DaysToSell
WHERE Category="Cat" AND DaysToSell>
(SELECT Avg(DaysToSell)
FROM the earlier query
WHERE Category="Cat"
ORDER BY DaysToSell DESC;

After running the query it I got error saying

ORA-00921: unexpected end of SQL command

Any suggestions! please

4
  • If you want some help you should try to format your question. Not just copy and paste. Commented May 15, 2009 at 0:35
  • WOW, this question needs some formatting. Commented May 15, 2009 at 0:36
  • What flavour of SQL you need spooks. sql server 2005?? Commented May 15, 2009 at 1:18
  • any would help ! I'm working on oracle .. thanks a lot guys Commented May 15, 2009 at 1:25

3 Answers 3

1

Queries can be combined with a subquery. For example,

select *
from (
    select * 
    from mytable
) subquery

Applying this pattern to your problem seems fairly straightforward.

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

1 Comment

you mean making it like Nebakanezer suggestion, can you explain with an example please
0

I don't see the closed bracket that matches with the select avg

1 Comment

thanks for that ... but I really need the help of connecting the two queries together and make it work
0

Ok, I've come up with this:

    SELECT AnimalID, Name, Category,
           [SaleDate]-[ReceiveDate] AS DaysToSell
    FROM   Sale INNER JOIN ((AnimalOrder INNER JOIN (Animal INNER JOIN AnimalOrderItem ON Animal.AnimalID = AnimalOrderItem.AnimalID) ON AnimalOrder.OrderID = AnimalOrderItem.OrderID)
           INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID) ON Sale.SaleID = SaleAnimal.SaleID
    WHERE  Category = "Cat"
    AND    ([SaleDate]-[ReceiveDate]) > (SELECT AVG([SaleDate]-[ReceiveDate])
                                         FROM   Sale INNER JOIN ((AnimalOrder INNER JOIN (Animal INNER JOIN AnimalOrderItem ON Animal.AnimalID = AnimalOrderItem.AnimalID) ON AnimalOrder.OrderID = AnimalOrderItem.OrderID)
                                         INNER JOIN SaleAnimal ON Animal.AnimalID =SaleAnimal.AnimalID) ON Sale.SaleID = SaleAnimal.SaleID
                                         WHERE Category = "Cat")
    ORDER BY ([SaleDate]-[ReceiveDate]) DESC;

1 Comment

I got missing expression error .. I really appreciate your help guy's

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.