You are here: Re: Locate rows in table where column value missing (I think!) « MsSQL Server « IT news, forums, messages
Re: Locate rows in table where column value missing (I think!)

Posted by Plamen Ratchev on 02/08/07 14:24

Here are two ways to accomplish what you need in SQL Server 2000:

SELECT A.fldYear, A.fldCode1, A.fldCode2
FROM (SELECT fldYear, fldCode1, fldCode2
FROM (SELECT DISTINCT fldYear FROM MyTable) AS Y
CROSS JOIN (SELECT DISTINCT fldCode1, fldCode2 FROM
MyTable) AS C) AS A
LEFT OUTER JOIN MyTable M
ON M.fldYear = A.fldYear AND M.fldCode1 = A.fldCode1 AND M.fldCode2
= A.fldCode2
WHERE M.fldYear IS NULL

SELECT fldYear, fldCode1, fldCode2
FROM (SELECT 'A' AS flag, fldYear, fldCode1, fldCode2
FROM (SELECT DISTINCT fldYear FROM MyTable) AS Y
CROSS JOIN (SELECT DISTINCT fldCode1, fldCode2 FROM
MyTable) AS C
UNION ALL
SELECT 'M', fldYear, fldCode1, fldCode2 FROM MyTable) AS UA
GROUP BY fldYear, fldCode1, fldCode2
HAVING COUNT(*) = 1 AND MAX(flag) = 'A'

On SQL Server 2005 this is much easier:

SELECT fldYear, fldCode1, fldCode2
FROM (SELECT DISTINCT fldYear FROM MyTable) AS Y
CROSS JOIN (SELECT DISTINCT fldCode1, fldCode2 FROM MyTable) AS C
EXCEPT
SELECT fldYear, fldCode1, fldCode2 FROM MyTable

HTH,

Plamen Ratchev
http://www.SQLStudio.com

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация