Solved

SQL Query

Posted on 2014-12-18
5
231 Views
Last Modified: 2014-12-19
Hi,

I have a table item_info which has following records



I need a query which can return only those items for which the commit_ind was never = 1

if you see the table, we can see that the item 2 has commit ind as 0 and the other record has commit ind as 1, i dont need such records, i want a query to return items for which commit_ind was never = 1.

Thanks in advance,
Rajeeva
0
Comment
Question by:rajeeva_nagaraj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 40508643
can you try this :

select a.item_id from your_table a
where not exists ( select 1 from your_table b
where b.item_id = a.item_id
and b.commit_ind = 1 )

if you do not want duplicates item_ids in the output, then you can even use distinct along with it.

select distinct a.item_id from your_table a
where not exists ( select 1 from your_table b
where b.item_id = a.item_id
and b.commit_ind = 1 )

Thanks,
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 40508647
Can be formulated quite the way you say it in SQL:

Select Distinct ii1.item_id from item_info ii1
WHERE NOT EXISTS (SELECT * From item_info ii2 Where ii2.[item id] = ii1.[item id] AND ii2.commit_ind=1);

Bye, Olaf.
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40508674
SUM function will do the trick:
SELECT item_id, SUM(commit_ind) commit_ind 
FROM  item_info 
GROUP BY  item_id
HAVING SUM(commit_ind)=0

Open in new window

0
 

Author Closing Comment

by:rajeeva_nagaraj
ID: 40508714
Thanks for you reply, both the queries were correct. I have chosen one because i wanted the result out of a single query.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 40508722
Well, you choose against a good indicator the EXISTS query is better, because two people recommended it independent of each other. Anyway, the SUM solution is somewhat elegant, but less expandable to other situations.

Let me show you why it could take much longer than a query with a subquery. If you have many records for the same item id, the EXISTS will only need to find one exception to exclude an id, while the SUM will sum all. You think along the lines a partial sum being >0 already exits further summings, but negative summands are possibly making the final sum 0, so it isn't exited early, all records are summed.

You're having a self join being very explicit about what should not exist and it will be faster in more cases.

At least don't make such thumb rule decisions about which query is better. Never turn off thinking just because of an indicator.

Bye, Olaf.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question