SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to show lowest price in DISTINCT query

    I have a price list that contains about 14000 items. My problem is that there are multiple items with the same name, but different prices. I want to run a query that will show distinct names and only show the lowest price. I'm just not quite sure how to tackle this problem.

    Thanks in advance

  2. #2
    SitePoint Guru asterix's Avatar
    Join Date
    Jun 2003
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post some relevant DDL and say what the database type is.
    I can already inform you that its is certainly not "DISTINCT" what you need, but a correlated subquery using (MIN(pricefield))....

  3. #3
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using Access right now. I'm just cleaning up a spreadsheet that I will be importing into SQL Server.

    Basically i have name, description and price

    example

    Product 1 Name Product 1 Description $20.00
    Product 1 Name Product 1 Description $30.00
    Product 1 Name Product 1 Description $33.00
    Product 2 Name Product 2 Description $55.00
    Product 2 Name Product 2 Description $65.00


    And the result i want is:

    Product 1 Name Product 1 Description $20.00
    Product 2 Name Product 2 Description $55.00

  4. #4
    SEOChat has too many ads Protoss's Avatar
    Join Date
    Aug 2004
    Location
    Long Island, NY
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know how efficiently this will run (in terms of query execution speed while under load), but try something like this (MS-SQL):

    Code:
    SELECT Product, Description, Price
    FROM Products
    WHERE (Price = (SELECT MIN(P.Price) FROM Products P WHERE (P.Product = Products.Product)))

  5. #5
    Xbox why have you forsaken me? moospot's Avatar
    Join Date
    Feb 2001
    Location
    Clearwater, FL
    Posts
    3,615
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will give that a shot and let you know if it worked for me.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    SELECT Product, Description, MIN(P.Price) as Price
    FROM Products
    GROUP BY Product, Description
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •