views:

27

answers:

1

I am trying to count the no of buses based on no of conditions. Problem is in a subquery it is returning 2 rows. I am expecting the count to be = 1 if all conditions are true. but it is giving it according to returned rows. Can u plz help how can i just compare rows instead of returning rows.

A: 

In your comment you state that this is the subquery that returns two rows.

SELECT 'x' FROM DP WHERE dp_id = DPT.dp_id having DPT.p_datetime = min(p_datetime) or DPT.a_datetime = max(a_datetime) 

What would you like to compare? If you want rows in your outer query where the subquery returns anything you can use the EXISTS keyword:

SELECT * FROM DTP WHERE EXISTS (SELECT 'x' FROM DP WHERE dp_id = DPT.dp_id having DPT.p_datetime = min(p_datetime) or DPT.a_datetime = max(a_datetime));

Is that what you mean? If not, please clarify what you mean by "compare rows" as it is rather ambiguous.

Tomas