pardeshirahul
asked on
How to write the query
I have a table
it has 3 coulumns
Tax_id, tax_number, Tax_status
now
suppose the data is
tax_id 1
tax_number 123
Tax_status Open
Tax_id 1tax_number 1234
tax_status - closed
tax_id 1
Tax_number 12345
tax_status - Confirmed
now i want the query
for whicg
find a Tax_id
under which all
the tax_number ( 123, 12354,12345 )
has status'COnfirmed'
it has 3 coulumns
Tax_id, tax_number, Tax_status
now
suppose the data is
tax_id 1
tax_number 123
Tax_status Open
Tax_id 1tax_number 1234
tax_status - closed
tax_id 1
Tax_number 12345
tax_status - Confirmed
now i want the query
for whicg
find a Tax_id
under which all
the tax_number ( 123, 12354,12345 )
has status'COnfirmed'
select * from table where Status ='Confirmed"
Sorry
Select * FROM table WHERE status ="Confirmed"
Select * FROM table WHERE status ="Confirmed"
Sorry, I missed part of the exists in my original post and cannot edit it now. Here is the correct version:
select tax_id from tbl a
where tax_number = 123 and status = 'Confirmed' and
exists (select 1 from tbl b where tax_number = 12354 and status = 'Confirmed' and a.tax_id = b.tax_id) and
exists (select 1 from tbl c where tax_number = 12345 and status = 'Confirmed' and a.tax_id = c.tax_id);
select tax_id from tbl a
where tax_number = 123 and status = 'Confirmed' and
exists (select 1 from tbl b where tax_number = 12354 and status = 'Confirmed' and a.tax_id = b.tax_id) and
exists (select 1 from tbl c where tax_number = 12345 and status = 'Confirmed' and a.tax_id = c.tax_id);
ASKER
nope
i want just the Tax_id under which if there are 10 Tax_number
all of which with status='CONFIRMED'
not just one tax_id under which any tax_number with tax_status='Confirmed'
i want just the Tax_id under which if there are 10 Tax_number
all of which with status='CONFIRMED'
not just one tax_id under which any tax_number with tax_status='Confirmed'
ASKER
what if i dont want to hard code the tax_number
check this
select Tax_id
from table_name
group by Tax_id
having SUM(case when Tax_status = 'Confirmed' then 1 else 0 end) = COUNT(*)
we are guessing at what you need exactly ...
if this is for learning sql, explain in more detail your problem
if this is for learning sql, explain in more detail your problem
So in your example, tax_id 1 would not be returned because all of the statuses are not 'Confirmed'? But, if there were a tax_id of 2 that had tax_numbers of 456 and 4567 both with statuses of 'Confirmed', it would be returned?
ASKER
yes that is my requirement
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@pardeshirahul - Did you run the query I provided?
select tax_id from tbl
where tax_number = 123 and status = 'Confirmed' and
exists (select 1 from tbl where tax_number = 12354 and status = 'Confirmed') and
exists (select 1 from tbl where tax_number = 12345 and status = 'Confirmed');