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
 Other Forums
 MS Access
 sql to create value based on prior record

Author  Topic 

NTC
Starting Member

21 Posts

Posted - 2008-04-07 : 22:36:01
Is Access/Jet
Table is this:
id, wo,
1, 2345-1
2, 2345-1
3, 2345-1
4, 5432-1
5, 5432-1
6, 5432-1

Need result:

id, wo, parent
1, 2345-1, 1
2, 2345-1, 1
3, 2345-1, 2
4, 5432-1, 4
5, 5432-1, 4
6, 5432-1, 5

the logic is If wo = wo of ID-1 then Parent = Id-1
If wo not= wo of ID-1 then Parent = Id

could use help on the correct sql statement for this...
thanks in advance...

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2008-04-07 : 23:04:37
[code]
SELECT
t.id,
t.wo,
IIF(isnull(t2.id), t.id, IIF(t.wo = t2.wo, t2.id, t.id) ) as parent
FROM t left join t t2 ON t.id-1=t2.id;

[/code]

"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

NTC
Starting Member

21 Posts

Posted - 2008-04-08 : 15:02:34
Much Thanks - works like a champ.. !!
Go to Top of Page
   

- Advertisement -