Click here to Skip to main content
15,914,066 members
Home / Discussions / Database
   

Database

 
AnswerRe: Where to store my files? Pin
Eddy Vluggen1-Jun-09 5:05
professionalEddy Vluggen1-Jun-09 5:05 
GeneralRe: Where to store my files? Pin
musefan1-Jun-09 5:13
musefan1-Jun-09 5:13 
GeneralRe: Where to store my files? Pin
Eddy Vluggen1-Jun-09 7:57
professionalEddy Vluggen1-Jun-09 7:57 
QuestionVFP OLE DB Provider error: Variable X is not found Pin
dasha_pl1-Jun-09 3:35
dasha_pl1-Jun-09 3:35 
QuestionSQL 2008 Workgroup - how do CALs work? Pin
aastudent1-Jun-09 0:38
aastudent1-Jun-09 0:38 
AnswerRe: SQL 2008 Workgroup - how do CALs work? Pin
Mycroft Holmes1-Jun-09 1:14
professionalMycroft Holmes1-Jun-09 1:14 
GeneralRe: SQL 2008 Workgroup - how do CALs work? Pin
aastudent1-Jun-09 1:26
aastudent1-Jun-09 1:26 
Questionproblem in executing stored procedure Pin
souravghosh1831-May-09 20:12
souravghosh1831-May-09 20:12 
AnswerRe: problem in executing stored procedure Pin
Aman Bhullar6-Jun-09 1:50
Aman Bhullar6-Jun-09 1:50 
AnswerRe: problem in executing stored procedure Pin
Niladri_Biswas25-Jun-09 5:57
Niladri_Biswas25-Jun-09 5:57 
QuestionCONTAINS not working in my full text search Pin
Brendan Vogt31-May-09 5:13
Brendan Vogt31-May-09 5:13 
AnswerRe: CONTAINS not working in my full text search Pin
Satish Pai31-May-09 19:49
Satish Pai31-May-09 19:49 
Questionline break not working in crystal reports ? Pin
kindman_nb30-May-09 21:58
kindman_nb30-May-09 21:58 
AnswerRe: line break not working in crystal reports ? Pin
Rajesh Anuhya1-Jun-09 0:03
professionalRajesh Anuhya1-Jun-09 0:03 
QuestionHelp with SQL query Pin
Joe_P30-May-09 10:30
Joe_P30-May-09 10:30 
AnswerRe: Help with SQL query Pin
Mycroft Holmes30-May-09 23:06
professionalMycroft Holmes30-May-09 23:06 
GeneralRe: Help with SQL query Pin
Joe_P31-May-09 4:11
Joe_P31-May-09 4:11 
GeneralRe: Help with SQL query Pin
Mycroft Holmes31-May-09 11:10
professionalMycroft Holmes31-May-09 11:10 
GeneralRe: Help with SQL query Pin
Joe_P31-May-09 11:23
Joe_P31-May-09 11:23 
GeneralRe: Help with SQL query Pin
Mycroft Holmes31-May-09 11:36
professionalMycroft Holmes31-May-09 11:36 
GeneralRe: Help with SQL query Pin
Joe_P31-May-09 14:08
Joe_P31-May-09 14:08 
GeneralRe: Help with SQL query Pin
Mycroft Holmes31-May-09 17:24
professionalMycroft Holmes31-May-09 17:24 
GeneralRe: Help with SQL query Pin
smcnulty20004-Jun-09 1:24
smcnulty20004-Jun-09 1:24 
The way I read this problem all of the children must have the status of 10. If the parent has any children without the status ten then it doesn't come back.

This makes it a bear because you have to do something to match all children with all children who have a status of 10 and only return the parent if the children all come back as status 10.

So I'd use a counting mechanism.

select distinct x.parentid 
from (
select 
bhr.* , 
(select max(bh.status) from @BatchHeader bh where bh.bhid=bhr.child) status, 
(select count(*) from @BatchHeaderReference bhr2 where bhr2.parentid=bhr.parentid) cnt, 
(select count(*) from @BatchHeaderReference bhr3 inner join @BatchHeader bh3 on bh3.bhid=bhr3.child where bh3.status=10 and bhr3.parentid=bhr.parentid) truecount 
from @BatchHeaderReference bhr
) x
 where x.cnt=x.truecount 


I'm more of an oracle guy but the above will work.

Going from the inside out, which is usually the best way to deal with these problems, you create a count of all children. Then a count of all 'true' children- those with a status of 10. Then you put all of that inside of a wrapper which we call X.
From there we treat X as another table.

If you need to also check to see if the parent has a status of 10 then you just change it to this.

select distinct x.parentid  
from (
select 
bhr.* , 
(select max(bh.status) from @BatchHeader bh where bh.bhid=bhr.child) status, 
(select count(*) from @BatchHeaderReference bhr2 where bhr2.parentid=bhr.parentid) cnt, 
(select count(*) from @BatchHeaderReference bhr3 inner join @BatchHeader bh3 on bh3.bhid=bhr3.child where bh3.status=10 and bhr3.parentid=bhr.parentid) truecount, 
 (select max(bh.status) from @BatchHeader bh where bh.bhid=bhr.parentid) pstatus
from @BatchHeaderReference bhr  
  
) x
 where x.cnt=x.truecount 
 and x.pstatus=10 


Here you get the parent status, calling it pstatus, and then just check it at the last stage.

edit: the reason MAX is used is to stop certain errors from creeping in if you have the same value show up but multiple times. If you expect different values for status against a given id then you'd have to take another approach because this would be incorrect.

_____________________________
Those who study history are doomed to watch others repeat it. -Scott M.


AnswerRe: Help with SQL query Pin
Niladri_Biswas2-Jul-09 0:25
Niladri_Biswas2-Jul-09 0:25 
Questionget the inserted GUID Pin
Mohammad Al Hoss30-May-09 2:18
Mohammad Al Hoss30-May-09 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.