?
Solved

SQL Query

Posted on 2014-12-18
5
Medium Priority
?
233 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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

752 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