Solved

SELECT DISTINCT on only one column from SELECT list

Posted on 2013-07-02
3
858 Views
Last Modified: 2013-07-02
Hi Experts,

I have  a stored proc that runs just fine but we have recently introduced a new column to the table which means that a column that was unique, (the barcode held in A.products_model in the code below), is no longer unique. The new column, (A.products_conditionID), effectively creates a composite key.  This just leaves the unique SQL identity column A.products_id.

So, my question is, how can I return a set of results that are unique based only on the A.products_model column but ALSO have two additional, runtime columns of 'lowest_price' and 'highest-price' based on the A.products_sale_price column.

So, for example, let's say we have a book which we have three copies of, all 3 in different condition for sale.  This would create three rows, all with the same products_model value but all with different products_conditionID values.  I effectively want 1 row that includes products_model and 'lowest_price' and 'highest-price' based on the A.products_sale_price column from these 3 rows.

Obviously the stored proc will be returning multiple rows, TOP 96 to be exact.

The stored proc is curently:

      SELECT DISTINCT TOP 96 A.products_id, A.products_name,
            A.products_model, A.products_promopoints, A.products_sale_price, E.authorName, E.authorID
      FROM dbo.tblWS_products A, dbo.tblWS_products_to_categories C, dbo.tblWS_shopCategories D, dbo.tblWS_authors E
      WHERE A.products_status = 1
      AND A.products_id = C.products_id
      AND C.categories_id = D.catID
      AND D.shopID = @shopID
      AND A.products_author_id = E.authorID
      ORDER BY A.products_id DESC

Help much appreciated...
0
Comment
Question by:jammy-d0dger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39293278
SELECT TOP 96 
           A.products_name
         , A.products_model
	 , E.authorName
	 , min(A.products_sale_price) as min_products_sale_price
	 , max(A.products_sale_price) as max_products_sale_price
  FROM dbo.tblWS_products A, dbo.tblWS_products_to_categories C, dbo.tblWS_shopCategories D, dbo.tblWS_authors E
 WHERE A.products_status = 1
   AND A.products_id = C.products_id
   AND C.categories_id = D.catID
   AND D.shopID = @shopID
   AND A.products_author_id = E.authorID
 GROUP BY 
           A.products_name
         , A.products_model
         , E.authorName
 ORDER BY A.products_id DESC

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39293358
Ah, one of my favorite topics :)

"select distinct" is utterly and totally devoid of options. It is "unconditional".

So, if you ever hear yourself say: "I need distinct, but with .....", then you cannot use "select distinct" at all because of the "but with".

Your ally and friend here is "group by", it does know about conditions - as Louis01 has so neatly displayed in comma first layout.

btw: MIN() and MAX() are examples of how "the conditions" are applied.

NO points please.
0
 

Author Comment

by:jammy-d0dger
ID: 39293672
Awesome, perfect answer, thank you very much Louis01.  After a few additional lines and tweaks for my specific requirements I now have:

	SELECT TOP 96 
           A.products_name
         , A.products_model
         , E.authorID
		 , E.authorName
		 , MIN(A.products_sale_price) as min_products_sale_price
		 , MAX(A.products_sale_price) as max_products_sale_price
		 , MIN(A.products_id) AS products_id
		 , MAX(A.products_promopoints) AS products_promopoints
  FROM dbo.tblWS_products A, dbo.tblWS_products_to_categories C, dbo.tblWS_shopCategories D, dbo.tblWS_authors E
 WHERE A.products_status = 1
   AND A.products_id = C.products_id
   AND C.categories_id = D.catID
   AND D.shopID = @shopID
   AND A.products_author_id = E.authorID
 GROUP BY 
           A.products_name
         , A.products_model
         , E.authorID
         , E.authorName
 ORDER BY MIN(A.products_id) DESC 

Open in new window


And this is working a treat.  Thanks also to PortletPaul for the explanation and very modest of you to not want any points. :)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

617 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