Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

join question

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
I have the following:

Code:
select * 
from  categories c, forums f, topics t
where c.category_id = f.category_id (+)
and t.forum_id (+) = f.forum_id
and (  c.is_removed = 0 and  f.is_removed = 0 and t.is_removed = 0 )

the mgmt decided not to delete the records and just include a flag. if it's deleted is_removed becomes 1.
now the problem is the join gets screwed up when i include the is_removed condition with 0. i have 5 categories with 14 forums and only 2 topics in one forum. all of the is_removed fields are 0.

the query returns 14 results without the is_removed condition and only 1 with is_Removed condition.
what's the correct way of writing this query?

 
nevermind. i have to write this in a different way. if i do :

Code:
and (  c.is_removed = 0 and nvl(f.is_removed, 0) = 0 and nvl(t.is_removed, 0) = 0 )

then i get all 14 records.

 
You could also use inline views or proper views to get round the problem. Companies that I've seen that have used this technique have normally had a view which incorporates the deleted flag and you're supposed to use that rather than the original table.

Personally, I think the idea of having a deleted flag in a table is a recipe for disaster. You can guarantee that not everyone will know about the flag and that someone will forget to incorporate it. What will happen is that a record will be deleted then added back with a different surrogate key. You then get queries returning duplicate rows because someone has forgotten to include the flag. Also, do you really want a whole load a useless deleted rows clogging up your data and making queries take longer than they need to?

Tell your management it's a stupid idea and that if they want to keep the data, they should do it properly using archive or audit tables.



For Oracle-related work, contact me through Linked-In.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top