Solved

SQL Query

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

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now