Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help with query

Author  Topic 

gualm
Starting Member

11 Posts

Posted - 2007-11-27 : 09:34:10
Hello everyone, I am new to Sql Server 2005 and need help with a query.

where off_rate ='Basic' then offer_prono = 'Family' then off_rate = 'Reg' and and acct_status in ('A', 'H', 'N') and chg_date = 1

I need help with a query that request output based on prior mulitiple criteria.

Example: Say we have Plans Basic, Family or no plan at all (Reg). The Family plan is a spec program.

In my query, I need to select only customers that were on the Basic plan at one time, then changed to the Family plan and are now on no plan (Reg). I am having trouble trying to use effective on and off program dates in my query to give me the information I need.

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-11-27 : 09:35:57
Please paste some code or give the outlook of your database.
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2007-11-27 : 10:55:03
Maybe something like the following. If not, post some test data and expected results:

-- *** Test Data ***
DECLARE @CustomerPlans TABLE
(
CustomerID int NOT NULL
,PlanType varchar(20) NOT NULL
,DateFrom datetime NOT NULL
,DateTo datetime NULL
)
INSERT INTO @CustomerPlans
SELECT 1, 'Basic', '20000101','20030601' UNION ALL
SELECT 1, 'Family', '20030601', NULL UNION ALL
SELECT 2, 'Family', '20020701', '20060314' UNION ALL
SELECT 3, 'Basic', '20000123','20040601' UNION ALL
SELECT 3, 'Family', '20040601','20070506' UNION ALL
SELECT 4, 'Basic', '20010823', NULL
-- *** End Test Data ***

SELECT C1.CustomerID
FROM @CustomerPlans C1
JOIN @CustomerPlans C2
ON C1.CustomerID = C2.CustomerID
WHERE C1.PlanType = 'Family'
AND C2.PlanType = 'Basic'
AND C2.DateTO <= C1.DateFrom
AND C1.DateTo IS NOT NULL
AND C2.DateTo IS NOT NULL
Go to Top of Page
   

- Advertisement -