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
Solved

SELECT DISTINCT on only one column from SELECT list

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

789 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