Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SELECT DISTINCT on only one column from SELECT list

Posted on 2013-07-02
3
Medium Priority
?
871 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

688 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