Solved

Find record with minimum variable value; correlated subquery produces empty result set

Posted on 2015-02-22
3
81 Views
Last Modified: 2015-02-22
I have a stored proc like this
ALTER PROCEDURE [dbo].[uspGetTopCrossSells] 
	@customerId int, @ProductCategory nvarchar (50),
	@Score float Output, @ClientKey nvarchar (50) Output, @Ranking int Output
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	SELECT Score, ClientKey, Ranking
	FROM CrossSellScore css, ItemCategory ic
	WHERE customerid = @customerid
       and score > 6.0
       and css.ItemCategoryId = ic.Id
       and description like @ProductCategory+'|%'
       and EndDate=(Select max(EndDate) from DatePeriod)
    Order By Score Desc
END

Open in new window


This produces a result set like this:
Score         ClientKey                 Ranking
9.17      Play Things|Other Plush      13
8.463      Play Things|Plush      54
8.308      Play Things|Bungee      69
8.301      Play Things|Other      70
8.19      Play Things|Unstuffies      86
8.011      Play Things|Cloth            100

I only want the first record, with the minimum Ranking. My efforts to modify the query have failed, along lines like adding a subquery

and Ranking = (Select MIN(Ranking) from CrossSellScore
             as C where C.CustomerId = css.CustomerId)

How do I get only the record w/ the lowest Ranking?
0
Comment
Question by:Mark Klein
3 Comments
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
Comment Utility
Try this:
SELECT TOP 1
              M.Score,
              M.ClientKey,
              M.Ranking
FROM
(
      SELECT Score, ClientKey, Ranking
      FROM CrossSellScore css, ItemCategory ic
      WHERE customerid = @customerid
       and score > 6.0
       and css.ItemCategoryId = ic.Id
       and description like @ProductCategory+'|%'
       and EndDate=(Select max(EndDate) from DatePeriod)
) M
ORDER BY M.Ranking ASC
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
E.g.

WITH Ordered AS (
	SELECT	Score, 
		ClientKey, 
		Ranking
		ROW_NUMBER() OVER ( Ranking ASC ) AS RN
	FROM	CrossSellScore css, 
		ItemCategory ic
	WHERE	customerid = @customerid
		AND score > 6.0
		AND css.ItemCategoryId = ic.Id
		AND description like @ProductCategory+'|%'
		AND EndDate = ( SELECT MAX(EndDate) FROM DatePeriod )
	)
	SELECT	Score, 
		ClientKey, 
		Ranking	
	FROM	Ordered O
	WHERE	O.RN = 1;

Open in new window

 

btw, please use for every column the appropriate table alias name. Also check whether you can use the JOIN syntax.
0
 

Author Closing Comment

by:Mark Klein
Comment Utility
Worked perfectly, except returned a second row w/ nulls.  Is that b/c the select uses 'score' instead of '@score=css.Score' etc.?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now