| | |
Select only items that don't exist in another table
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 30
Reputation:
Solved Threads: 0
I want to select from a table only when a field repeats more than once. For this I have:
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?
MS SQL Syntax (Toggle Plain Text)
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?
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.
as for only showing fields that don't exist in another table you can use exists condition.
sql Syntax (Toggle Plain Text)
SELECT title, count(*) As Cnt FROM poss_titles GROUP BY title HAVING count(*) > 1 ORDER BY count(*) desc
as for only showing fields that don't exist in another table you can use exists condition.
sql Syntax (Toggle Plain Text)
SELECT title, count(*) As Cnt FROM poss_titles pt WHERE NOT EXISTS (SELECT title FROM other_table ot WHERE ot.title = pt.title) GROUP BY title HAVING count(*) > 1 ORDER BY count(*) desc
Last edited by cgyrob; Oct 19th, 2009 at 3:04 pm.
![]() |
Similar Threads
- [req] how to check whether given oracle table exist or not + C# (C#)
- Select a Set of rows from a table (MS SQL)
- Shopping Cart Not Working (ASP.NET)
- how to select the isbn number in table and get all books of isbn selected (JSP)
- Select all the items in a listbox right before submit. (JavaScript / DHTML / AJAX)
- Syntax error,plz help (PHP)
- php table help (PHP)
- Pagination - not displaying results properly, please help! (PHP)
- Send all items in a listBox to SQL server database table (ASP.NET)
Other Threads in the MS SQL Forum
- Previous Thread: SELECT statement
- Next Thread: Best way to backup a database from one server to another server in SQL Server 2005
Views: 570 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday





