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


count record if another record does not exist

Posted on 2006-05-03
Medium Priority
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?

Question by:grizum

Expert Comment

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

Accepted Solution

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')
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)
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

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
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16602198
Did u try the other Query
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')
LVL 17

Expert Comment

ID: 16602217
you're welcome grizum!

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