Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql Query Lookup based on range

Posted on 2016-09-29
2
Medium Priority
?
94 Views
Last Modified: 2016-10-27
Here is some sample data from 2 sql 2008 tables

CampaignSeg Table

PlayerID      Seg     LowPoints  HighPoints
1                   56


Campaign_Seg_LookUp  Table

Campaign      SegFloor         SegCeiling          LowPoints          Highpoints
280                35                   49.99999      5000                  6000
280                      50                      74.99999      7000                  8000
280                      75                   1499.99999      9000                   10000





I am trying to write an update query that will populate the Low and high points on CampaignSeg Table from the Campaign_Seg_LookUp  Table based on player segment

Using the above sample data,  table entry in would be as follows (since 56 between the segfloor of 50 and segceiling of 74.999


Playerid        Seg         Lowpoints     Highpoints
 1                   56           7000               8000

Please Note:  The number of entries in Campaign_Seg_LookUp  Table can vary from campaign to campaign along with the ranges so can't really do hard coded case statements
0
Comment
Question by:johnnyg123
[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
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 41822032
DECLARE @CampaignSeg TABLE
(
	PlayerID	INT,
	Seg			INT,
	LowPoints	INT,
	HighPoints	INT
);

INSERT @CampaignSeg (PlayerID, Seg, LowPoints, HighPoints)
VALUES (1, 56, NULL, NULL);

DECLARE @Campaign_Seg_Lookup TABLE
(
	Campaign	INT,
	SegFloor	DECIMAL(9,4),
	SegCeiling	DECIMAL(9,4),
	LowPoints	INT,
	HighPoints	INT
);

INSERT @Campaign_Seg_Lookup (Campaign, SegFloor, SegCeiling, LowPoints, HighPoints)
VALUES (280, 35, 49.99999, 5000, 6000),
	(280, 50, 74.99999, 7000, 8000),
	(280, 75, 1499.99999, 9000, 10000);

WITH cteLookup AS
(
	SELECT C.PlayerID,
		C.Seg,
		MIN(CLookup.LowPoints) AS LowPoints,
		MAX(CLookup.HighPoints) AS HighPoints
	FROM @CampaignSeg AS C
	INNER JOIN @Campaign_Seg_Lookup AS CLookup
		ON C.Seg >= CLookup.SegFloor
		AND C.Seg <= CLookup.SegCeiling
	GROUP BY C.PlayerID, C.Seg
)
UPDATE C
SET LowPoints = L.LowPoints,
	HighPoints = L.HighPoints
FROM @CampaignSeg AS C
INNER JOIN cteLookup AS L
	ON C.PlayerID = L.PlayerID;

SELECT *
FROM @CampaignSeg;

Open in new window

0
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41824665
@Author - have you checked the query given the expert above or you need more help on this?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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