Solved

SQL Query

Posted on 2014-12-18
5
230 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

732 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