Solved

SQL server 2005 sql query problem

Posted on 2007-03-30
2
180 Views
Last Modified: 2010-03-19
I am having problems writing this sql query, help would be much appreciated:

I have two tables
ageBracketPercent and ageBracketCount
ageBracketPercent has the following fields
mediaId
lowAge
highAge
percentage

ageBracketCount has these fields
mediaId
lowAge
highAge
count

I am trying to retrieve all records in ageBracketCount that are not
represented in agePercentCount so something like this

select * from ageBracketCount
where [the combined key (mediaId, lowAge, highAge) does not exist in ageBracketPercent)]
(I wonder why this SQL query is not working!;-)

so in other words, if I have this data in the tables
ageBracketPercent:
mediaId      lowAge      highAge percentage
1          1              10            50
1          25           100        30

and this data in ageBracketCount
mediaId      lowAge      highAge count
1          1              10            10000
1          10               25            50000
1          25           100            1000

I want to get to the middle record with this data
mediaId      lowAge      highAge count
1          10               25          50000
0
Comment
Question by:HugoForte
2 Comments
 
LVL 10

Accepted Solution

by:
ksaul earned 125 total points
ID: 18826817
SELECT a.*
FROM ageBracketCount a
LEFT OUTER JOIN ageBracketPercent p ON a.mediaId = p.mediaId AND a.lowAge = p.lowAge
WHERE p.mediaId IS NULL
0
 

Author Comment

by:HugoForte
ID: 18826899
LoL;-) thanks a lot, I dont' know why I was complicating it!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

911 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

23 Experts available now in Live!

Get 1:1 Help Now