[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

count record if another record does not exist

Posted on 2006-05-03
7
Medium Priority
?
264 Views
Last Modified: 2008-02-01
id | siteid | action      |
---------------------------
1  | 50     | TYPE       |
2  | 56     | TYPE       |
3  | 67     | TYPE       |
4  | 50     | CLOSED  |
5  | 56     | CLOSED  |

i have the above table, i want to select the siteid for every record that does not have a 'closed' action associated with it.  for example, with the above table there should only be 1 result...  siteid 67

how can i write a ms sql statement to accomplish this?

0
Comment
Question by:grizum
7 Comments
 
LVL 5

Expert Comment

by:morisce
ID: 16601973
>> action associated with it
 what is the association betwwen recods ?
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 2000 total points
ID: 16602041
Try this
select siteid,action from yourtable
where id not in(select id from yourtable where action='CLOSED')
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16602164
SELECT SITEID from urTable t2
WHERE NOT EXISTS (SELECT 1 FROM urTable T1 WHERE T1.Action = 'Closed' AND T1.SiteId =t2.SiteID)
0
Technology Partners: 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!

 

Author Comment

by:grizum
ID: 16602194
Thank You HuyBD .. i have struggled with this for a week.  That sql statement is pure poetry, lol.

Worth every point .. thanks again
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16602198
Did u try the other Query
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16602211

I think the query has to be modified to the following

select siteid,action from urTab
where siteid not in(select siteid from urTab where action='CLOSED')
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 16602217
you're welcome grizum!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

831 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