SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Case When Help


Case When Help

Author
Message
helal.mobasher 13209
helal.mobasher 13209
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 219
I am trying to assign Active or inactive based on member termdate. A member may have multiple termdates. The logic is if a member MAX termdate is greater than getdate() then regardless of his/her previous termdate, he/she is assigned Active. If a member Max termdate is less than getdate(), then he/she is assinged inactive. Here are DLLs:
CREATE TABLE table1
(
ID Nvarchar (10),
Date smalldat
)

INSERT INTO table1
(ID,Date)
SELECT '1', '12/31/2078'
UNION ALL
SELECT '1', '12/31/2011'
UNION ALL
SELECT '2', '12/31/2011'
UNION ALL
SELECT '2', '11/30/2011'
UNION ALL
SELECT '2', '10/31/2011'

--Results
CREATE TABLE Results
(
ID Nvarchar (10),
Date smalldat,
Status varchar(15)
)

INSERT INTO Results
(ID,Date,Status)
SELECT '1', '12/31/2078' , 'Active'
UNION ALL
SELECT '1', '12/31/2011' , 'Active'
UNION ALL
SELECT '2', '12/31/2011' , 'Inactive'
UNION ALL
SELECT '2', '11/30/2011' , 'Inactive'
UNION ALL
SELECT '2', '10/31/2011' , 'Inactive'

Thank for the help

Helal
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18471 Visits: 6431
You can try this:


SELECT ID, Date
,Status=CASE WHEN GETDATE() < MAX(Date) OVER (PARTITION BY ID) THEN 'Active' ELSE 'Inactive' END
FROM Table1





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
subbu1
subbu1
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1339 Visits: 695
Pls try below code.

CREATE TABLE #table1
(
ID Nvarchar (10),
Date DATE
)

INSERT INTO #table1
(ID,Date)
SELECT '1', '12/31/2078'
UNION ALL
SELECT '1', '12/31/2011'
UNION ALL
SELECT '2', '12/31/2011'
UNION ALL
SELECT '2', '11/30/2011'
UNION ALL
SELECT '2', '10/31/2011'
SELECT *,(SELECT CASE WHEN MAX(T2.Date)>GETDATE() THEN 'ACTIVE' ELSE 'INACTIVE' END FROM #table1 T2 WHERE T1.ID=T2.ID) STATUS FROM #table1 T1
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search