Select only items that don't exist in another table

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 29
Reputation: benkyma is an unknown quantity at this point 
Solved Threads: 0
benkyma benkyma is offline Offline
Light Poster

Select only items that don't exist in another table

 
0
  #1
Oct 19th, 2009
I want to select from a table only when a field repeats more than once. For this I have:

  1. SELECT title, Count(*) AS Cnt FROM poss_titles WHERE Cnt > 1 GROUP BY Title ORDER BY Cnt DESC

But it says invalid column name cnt. How should I refer to this dynamic field?
Also, I only really want titles which dont exist in another table. I believe a left join is required for this but I'm not too sure how to do it.
Can anyone offer me some pointers?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster
 
3
  #2
Oct 19th, 2009
You can't use the alias in the boolean test plus you should use a having clause to test the count(*) and not the where clause.

  1. SELECT title, count(*) As Cnt
  2. FROM poss_titles
  3. GROUP BY title
  4. HAVING count(*) > 1
  5. ORDER BY count(*) desc

as for only showing fields that don't exist in another table you can use exists condition.

  1. SELECT title, count(*) As Cnt
  2. FROM poss_titles pt
  3. WHERE NOT EXISTS (SELECT title FROM other_table ot WHERE ot.title = pt.title)
  4. GROUP BY title
  5. HAVING count(*) > 1
  6. ORDER BY count(*) desc
Last edited by cgyrob; Oct 19th, 2009 at 3:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 29
Reputation: benkyma is an unknown quantity at this point 
Solved Threads: 0
benkyma benkyma is offline Offline
Light Poster
 
0
  #3
Oct 20th, 2009
That's fantastic. Thanks a lot : )
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC