Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


update records based on condition


update records based on condition

Author
Message
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14971 Visits: 38985
I think you will need to update the table against a version of itself featuring row_number().

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

riya_dave
riya_dave
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 367
but i need loop or condtion hwo to do it

example if primarycondition = 'HF'
then update table a
set secondary condtion = null

if there is no primary condition = 'HF'
then
if primarycondition = 'CAD'
update table a
set secondarycondition = primarycondition
except for primarycondition ='CAD'
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
riya_dave (8/1/2013)
but i need loop or condtion hwo to do it

example if primarycondition = 'HF'
then update table a
set secondary condtion = null

if there is no primary condition = 'HF'
then
if primarycondition = 'CAD'
update table a
set secondarycondition = primarycondition
except for primarycondition ='CAD'


Use ROW_NUMBER.

If you need specific help with syntax you MUST post ddl and sample data in a consumable format. If you can do that you will get tested and accurate code back very quickly. If you continue to refuse to help us to help you, you will continue to get answers with no real details.

You should read this article and think about how this relates to what you have posted in this thread.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
riya_dave
riya_dave
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 367
i used row_number ,but how to get multiple condition in update

for example if condition = 'HF'
then update
else if condition ='AST'
then update
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14971 Visits: 38985
UPDATE SomeTable
SET Column =
CASE
WHEN condition = 'HF'
THEN SomeCalculationOrValueOrColumn
WHEN condition2 = 'AST'
THEN ADifferentCalculationOrValueOrColumn
END

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

riya_dave
riya_dave
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 367
i am trying to explain it here

example:

ID pcondition scondition

1 HF NULL
1 CA NULL
1 CF NULL
2 CA NULL
2 AST NULL

the order for updating data is

1 HF,
2 CA
3 CF
4 AST

if for particular ID ( ID 1)
WE HAVE 3 pcondition, HF,CA,CF

based on order ,here HF will be pcondition and CA and CF WILL MOVE TO scondition

for ID(2), WE have CA,AST
Based on order among 4 condition,whiever comes first will be primary
so for ID(2) CA will be pcondition and AST will move to scondition

result

ID pcondition scondition
1 PF NULL
1 NULL CA
1 null CF
2 CA NULL
2 NULL AST

thanks
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
riya_dave (8/2/2013)
i am trying to explain it here


Why do you find it so difficult to read the article about best practices? Why is it so difficult to take a few minutes of your time to put together something that we can actually use to write some code with to help you with your problem? This is really NOT a difficult query to write. It would take Lowell or I probably less than 5 minutes to put together a fully functional query that does exactly what you want. The problem is that writing a query requires 2 things: a table and data. You have so far provided neither of those things in anything resembling a format that is easy to use.

Maybe the issue here is a language barrier. That is not really a problem. We help people that speak many different languages on a regular basis. How is that we help all those other people so easily? They post ddl and sample in the language that ALL of us on this site have in common. SQL.

Post your two tables in a format that we can use and also what you expect as the output based on your sample data. If you had done that yesterday this issue would be far behind you and you could get on with your next issue.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
riya_dave
riya_dave
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 367
HERE YOU GO



CREATE TABLE CustomCode
(ID int,
pcondition varchar(50),
Pinten int,
scondition varchar(50),
sinten int)

INSERT into CustomCode
SELECT 1,'HF',1,NULL,NULL
UNION
SELECT 1,'CA',1,NULL,NULL
UNION
SELECT 1,'AF',1,NULL,NULL
UNION
SELECT 2,'CAD',2,null,NULL
UNION
SELECT 2,'AST',2,null,null
UNION
SELECT 3,'CA',1,NULL,NULL
UNION
SELECT 4,NULL,NULL,'HF',1


Order to Update :

If ID has more than one pcondition
one of them will be pconditrion and pinten
other will be scondition and sinten

Based on followind order
1 - AF
2- CA
3 - CAD
4 - AST
5- hf

Output :
ID pcondition Pinten scondition sinten
1 AF 1 NULL NULL
1 NULL NULL CA 1
1 NULL NULL HF 1
2 NULL NULL AST 2
2 CAD 2 NULL NULL
3 CA 1 NULL NULL
4 NULL NULL HF 1
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
Thanks! That makes it so that those of us helping have enough information to work with. I took the liberty of turning your sort logic into a table too so you can actually do something with it.


create table #SortSomething
(
pcondition varchar(3),
SortOrder int
)

insert #SortSomething(SortOrder, pcondition)
select 1, 'AF' union all
select 2, 'CA' union all
select 3, 'CAD' union all
select 4, 'AST' union all
select 5, 'hf'

;with SortedData as
(
select cc.*, s.SortOrder, ROW_NUMBER() over(partition by ID order by s.SortOrder) as RowNum
from CustomCode cc
left join #SortSomething s on cc.pcondition = s.pcondition
)

select ID,
case when RowNum = 1 AND SortOrder is not null then pcondition else null end as pcondition,
case when RowNum = 1 AND SortOrder is not null then Pinten else null end as Pinten,
case when RowNum > 1 then pcondition
when RowNum = 1 AND SortOrder is null then scondition
else null end as scondition,
case when RowNum > 1 then Pinten
when RowNum = 1 AND SortOrder is null then sinten
else null end as sinten
from SortedData sd
order by sd.ID, RowNum



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
riya_dave
riya_dave
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 367
but i dont need select ,i just need update statement, the store proc should not have select
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