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


Change Excel formula to Case Statement


Change Excel formula to Case Statement

Author
Message
J39L4753
J39L4753
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 382
Koen Verbeeck (10/3/2013)
You'll probably need a cursor to update your values, because to calculate the OneStopOrNot column for one row, you need the calculated result of OneStopOrNot column of the previous row.

With a set-based solution all the rows would be updated at once, which means you cannot use the result of the previous row.

To get you started on cursors:
SQL Server Cursor Example

Just to be perfectly clear: cursors are bad and slow for performance. However, in your specific business case you might need them. (unless someone else proves me wrong of course and uses set-based TSQL to solve this issue)


Cool, thanks for this, I'll have a look and give it a go
parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 136
Hi,

This can be achieved using CTE



declare @person table (PersonID int, ApptDate datetime, ApptStatus varchar(10), NoOfAppts int, OneStopOrNot VARCHAR(10))

insert into @person (PersonID, ApptDate, ApptStatus, NoOfAppts) values
(49820,'07-09-2013','Seen',1),
(49827,'12-sep-2013','Seen',1),
(49831,'07-sep-2013','Seen',1),
(49834,'07-sep-2013','Seen',1),
(50084,'07-sep-2013','Seen',1),
(50097,'05-sep-2013','Seen',1),
(50172,'05-sep-2013','Seen',1),
(50172,'27-sep-2013','Seen',2),
(50175,'05-sep-2013','Seen',1)


;WITH persons AS (
SELECT
rownum = ROW_NUMBER() OVER (ORDER BY PersonID, ApptStatus),
*
FROM @person
)
UPDATE cur SET OneStopOrNot =
( case WHEN cur.PersonID != prev.PersonID THEN 'OneStop'
WHEN prev.PersonID IS NULL THEN 'OneStop'
WHEN cur.PersonID = prev.PersonID AND prev.OneStopOrNot = 'MoreThan1' THEN 'MoreThan1'
WHEN cur.PersonID = prev.PersonID AND cur.ApptDate = prev.ApptDate THEN 'OneStop'
ELSE 'MoreThan1' END)
FROM persons cur
LEFT JOIN persons prev on prev.rownum = cur.rownum - 1

select * from @person


parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 136
This can be achieved using CTE



declare @person table (PersonID int, ApptDate datetime, ApptStatus varchar(10), NoOfAppts int, OneStopOrNot VARCHAR(10))

insert into @person (PersonID, ApptDate, ApptStatus, NoOfAppts) values
(49820,'07-09-2013','Seen',1),
(49827,'12-sep-2013','Seen',1),
(49831,'07-sep-2013','Seen',1),
(49834,'07-sep-2013','Seen',1),
(50084,'07-sep-2013','Seen',1),
(50097,'05-sep-2013','Seen',1),
(50172,'05-sep-2013','Seen',1),
(50172,'27-sep-2013','Seen',2),
(50175,'05-sep-2013','Seen',1)


;WITH persons AS (
SELECT
rownum = ROW_NUMBER() OVER (ORDER BY PersonID, ApptStatus),
*
FROM @person
)
UPDATE cur SET OneStopOrNot =
( case WHEN cur.PersonID != prev.PersonID THEN 'OneStop'
WHEN prev.PersonID IS NULL THEN 'OneStop'
WHEN cur.PersonID = prev.PersonID AND prev.OneStopOrNot = 'MoreThan1' THEN 'MoreThan1'
WHEN cur.PersonID = prev.PersonID AND cur.ApptDate = prev.ApptDate THEN 'OneStop'
ELSE 'MoreThan1' END)
FROM persons cur
LEFT JOIN persons prev on prev.rownum = cur.rownum - 1

select * from @person


parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 136
By mistaken two times posted the same.
J39L4753
J39L4753
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 382
parulprabu (10/3/2013)
By mistaken two times posted the same.


Thanks for the CTE, I'll give that a go Smile
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