Solved

Sql Query Lookup based on range

Posted on 2016-09-29
2
49 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 28

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

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.

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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