Solved

SELECT DISTINCT on only one column from SELECT list

Posted on 2013-07-02
3
827 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
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 48

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now