You are here: Re: Merging two tables with selection « MsSQL Server « IT news, forums, messages
Re: Merging two tables with selection

Posted by Bruce Stradling on 08/26/06 22:07

Thanks ... that was exactly what I needed. Here is the final code:

SELECT U.PropertyType, U.Description, U.Status
FROM UserPropertyTypeTable U
UNION ALL SELECT S.PropertyType, S.Description, S.Status
FROM SystemPropertyTypeTable S
WHERE S.Status = 'ACTIVE'
AND NOT EXISTS (SELECT *
FROM UserPropertyTypeTable U
WHERE S.PropertyType = U.PropertyType)
ORDER BY PropertyType;

And then another that removed all inactive records:

SELECT U.PropertyType, U.Description, U.Status
FROM UserPropertyTypeTable U
WHERE U.Status = 'ACTIVE'
UNION ALL SELECT S.PropertyType, S.Description, S.Status
FROM SystemPropertyTypeTable S
WHERE S.Status = 'ACTIVE'
AND NOT EXISTS (SELECT *
FROM UserPropertyTypeTable U
WHERE S.PropertyType = U.PropertyType)
ORDER BY PropertyType;

"Erland Sommarskog" <esquel@sommarskog.se> wrote in message
news:Xns982BE98115752Yazorman@127.0.0.1...
> Bruce Stradling (bstradling@cox.net) writes:
> > I would like to Merge these two tables using the following logic.
> > The SystemPropertyTypeTable any records that have "ACTIVE" for the
status.
> > The UserPropertyTypeTable all records.
> >
> > Group by Name and remove any duplicates. if the UserPropertyTypeTable
> > has INACTIVE then Throw away the Active Record from the
> > SystemPropertyTypeTable and keep the INACTIVE record.
>
> If I understand this correctly, you want:
>
> SELECT U.PropertyType, U.Description, U.Status
> FROM UserPropertyTable U
> UNION ALL
> SELECT S.PropertyType, S.Description, S.Status
> FROM SystemPropertyType S
> WHERE S.Status = 'ACTIVE'
> AND NOT EXISTS (SELECT *
> FROM UserPropertyTable U
> WHERE S.Property = U.Property)
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 

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

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