Solved

SELECT DISTINCT on only one column from SELECT list

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

708 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

11 Experts available now in Live!

Get 1:1 Help Now