Results 1 to 3 of 3

Thread: Sql + Distinct + Inner Join + Eek

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Sql + Distinct + Inner Join + Eek

    I'm having trouble trying to figure out how to run a query. I want to only select distinct order_ids, but list all columns. I know I have to use a GROUP BY s.order_id, but I can't figure out where to put it. I get an error everywhere I go. Any ideas?

    Code:
    SELECT DISTINCT( s.order_id ), * FROM schedule AS s 
       INNER JOIN schedule_detail AS sd ON s.s_id = sd.s_id 
       WHERE sd.invoice_id = 0 AND s.shipped_quantity = s.quantity
       AND s.quantity > 0
    FYI, using Postgres.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Sql + Distinct + Inner Join + Eek

    This doesn;'t make any sense to me - you want a single ORDER ID but all the columns associated with that ORDER ID.

    Is there more then one row with the same ORDER ID in the details table? Which row's data would you want to show?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Sql + Distinct + Inner Join + Eek

    I ended up taking a different route with this. In the schedule table (shipment schedule), the order id may be present more than once, as an order may have multiple parcels, so yes. What I wanted to do was to grab only unique order ids, but at the same time return all fields for the row, but only have DISTINCT applied to the order id.

    I just ended up returning all order ids then pulling associated information in a seperate query (from a completely different table, even). This was mostly because the scope of the projet changed.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width