Solved

Sql Query Lookup based on range

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

Independent Software Vendors: 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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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