Solved

Sql Query Lookup based on range

Posted on 2016-09-29
2
40 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
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 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 23

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

943 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

15 Experts available now in Live!

Get 1:1 Help Now