0
ID     Balance      Account Type      Date
1245    100           HR              01-01-15
1245    500           HC              02-01-15
1325    200           HC              03-01-15
1789    400           HC              04-01-15

In this case, I want only rows With account type as HC and Balance not equal to zero

select * from ABC where Account Type = 'HC'
and Balance<>0; 

is not working.

I want only ID which has account type as HC and Balance > 0 and for that ID it should not have Account type as HR.

1
  • This query is running on MySql or Sql Server? Commented Feb 18, 2015 at 18:06

2 Answers 2

1

Try this:

select * from ABC
where Account_Type = 'HC'
and Balance<>0
and not exists(
    select 'HR'
    FROM abc a2
    where a2.account_type = 'HR'
   AND a2.id = abc.id
)
Sign up to request clarification or add additional context in comments.

Comments

0

This should work:

select * 
from ABC A1 
where 
  Account Type = 'HC' and 
  Balance<>0 and 
  not exists (
    select 1 
    from ABC A2 
    where 
      A1.ID = A2.ID And 
      A2.Type = 'HR'
  )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.