Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-22
3
Medium Priority
?
107 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 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 total points
ID: 40624148
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 36

Expert Comment

by:ste5an
ID: 40624176
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
ID: 40624193
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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…

926 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