Complex SQL Query...HELP! 
Author Message
 Complex SQL Query...HELP!

Hello,
        I am having tons of problems formulating this query:  Say I am a librarian,
I want to find the cheapest supplier of each book I have:

Sample Table Data:      TableName: Supplier

        Name    ISBN    Price
        ======= ======= =======
        ABC     100     $5.99
        XYZ     100     $7.99
        ABC     200     $1.99
        XYZ     200     $1.50
        ABC     300     $2.95
        XYZ     300     $3.75

Running the SQL Query should return the following:

Sample Correct Output:  (This is what I am trying to achieve)

        Name    ISBN    Price
        ======= ======= =======
        ABC     100     $5.99
        XYZ     200     $1.50
        ABC     300     $2.95

What I have so far is not working 100% :

SELECT Name, ISBN, Min(Price) AS MinOfPrice FROM Supplier GROUP BY Name, ISBN
ORDER BY Min(Price)

The output of the above query is:

        Name    ISBN    Price
        ======= ======= =======
        ABC     100     $5.99   (GOOD)
        XYZ     200     $1.50   (GOOD)
        ABC     300     $2.95   (GOOD)
        XYZ     100     $7.99   (GO AWAY!)
        ABC     200     $1.99   (GO AWAY!)
        XYZ     300     $3.75   (GO AWAY!)

How do I change the above query (or simply make a new one) which gives me
my desired output?


I'm using VB 4.0 Enterprise if that matters (shouldn't).

Thanks again
Deepak



Mon, 07 Jun 1999 03:00:00 GMT  
 Complex SQL Query...HELP!


Quote:

>Hello,
>    I am having tons of problems formulating this query:  Say I am a librarian,
>I want to find the cheapest supplier of each book I have:

>Sample Table Data:  TableName: Supplier

>    Name    ISBN    Price
>    ======= ======= =======
>    ABC     100     $5.99
>    XYZ     100     $7.99
>    ABC     200     $1.99
>    XYZ     200     $1.50
>    ABC     300     $2.95
>    XYZ     300     $3.75

>Running the SQL Query should return the following:

>Sample Correct Output:      (This is what I am trying to achieve)

>    Name    ISBN    Price
>    ======= ======= =======
>    ABC     100     $5.99
>    XYZ     200     $1.50
>    ABC     300     $2.95

>What I have so far is not working 100% :

>SELECT Name, ISBN, Min(Price) AS MinOfPrice FROM Supplier GROUP BY Name, ISBN
>ORDER BY Min(Price)

I'm not sure if the DISTINCT predicate can go in the middle of the
SQL, but you could use your ISBN as the first parameter like this..

"SELECT DISTINCT ISBN, Name, Min(Price) AS MinOfPrice FROM Supplier
GROUP BY Name, ISBN"

The result will be that the query only selects an ISBN one time, and
it should be the one with the lowest(Min), price.

HTH,
Ed Phillippe

- Show quoted text -

Quote:
>ORDER BY Min(Price)

>The output of the above query is:

>    Name    ISBN    Price
>    ======= ======= =======
>    ABC     100     $5.99   (GOOD)
>    XYZ     200     $1.50   (GOOD)
>    ABC     300     $2.95   (GOOD)
>    XYZ     100     $7.99   (GO AWAY!)
>    ABC     200     $1.99   (GO AWAY!)
>    XYZ     300     $3.75   (GO AWAY!)

>How do I change the above query (or simply make a new one) which gives me
>my desired output?


>I'm using VB 4.0 Enterprise if that matters (shouldn't).

>Thanks again
>Deepak




Tue, 08 Jun 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Complex SQL Query: HELP!

2. Need help with complex SQL query

3. SQL's with complex queries

4. complex SQL query

5. VB 4.0, ANSI SQL '89, complex queries?

6. complex SQL query

7. Complex SQL query

8. Complex SQL Query

9. Complex Query Needs Help

10. Complex WebSite/Win CGI/VB/MS Access Query Help Needed

11. Complex WebSite/Win CGI/VB/MS Access Query Help Needed

12. Help with complex SQL statement

 

 
Powered by phpBB® Forum Software