Solved

Sql Query Lookup based on range

Posted on 2016-09-29
2
28 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
Comment Utility
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 17

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now