Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

update records based on condition Expand / Collapse
Author
Message
Posted Thursday, August 1, 2013 2:57 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:09 PM
Points: 12,905, Visits: 32,166
I think you will need to update the table against a version of itself featuring row_number().



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1480157
Posted Thursday, August 1, 2013 3:20 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 4, 2013 8:13 AM
Points: 190, 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'
Post #1480166
Posted Thursday, August 1, 2013 3:24 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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)
Post #1480169
Posted Friday, August 2, 2013 11:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 4, 2013 8:13 AM
Points: 190, 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
Post #1480533
Posted Friday, August 2, 2013 11:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:09 PM
Points: 12,905, Visits: 32,166
UPDATE SomeTable
SET Column =
CASE
WHEN condition = 'HF'
THEN SomeCalculationOrValueOrColumn
WHEN condition2 = 'AST'
THEN ADifferentCalculationOrValueOrColumn
END


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1480535
Posted Friday, August 2, 2013 11:28 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 4, 2013 8:13 AM
Points: 190, 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

Post #1480539
Posted Friday, August 2, 2013 12:08 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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)
Post #1480555
Posted Friday, August 2, 2013 12:35 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 4, 2013 8:13 AM
Points: 190, 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

Post #1480566
Posted Friday, August 2, 2013 1:00 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
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)
Post #1480578
Posted Friday, August 2, 2013 1:16 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 4, 2013 8:13 AM
Points: 190, Visits: 367
but i dont need select ,i just need update statement, the store proc should not have select
Post #1480583
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse