You are here: Re: Query help: Item below reorder level-find all items for same vendor « MsSQL Server « IT news, forums, messages
Re: Query help: Item below reorder level-find all items for same vendor

Posted by Erland Sommarskog on 05/07/07 21:45

rdraider (rdraider@sbcglobal.net) writes:
> Thanks for the help. Can I ask a follow up?
> What if you wanted to add the CategoryID to this so that the results
> showed UnitsInStock < ReorderLevel and included items where the supplier
> AND CategoryID were the same?
> Examples: SupplierID 7 has 5 items returned but only 1 is below reorder
> and all are different categories
> SupplierID 23 has 3 items returned but only 2 share the same
> CategoryID.

This is precisely why I prefer EXISTS over IN. IN can only handle when
the condition is on a single column. EXISTS is extensible:

Select a.ProductID, a.ProductName, a.SupplierID, a.CategoryID,
a.UnitsInStock, a.ReorderLevel
from Products a
where exists (
SELECT *
FROM Products b
WHERE a.SupplierID = b.SupplierID
AND a.CategoryID = b.CategoryID
AND b.UnitsInStock < b.ReorderLevel
)
ORDER BY a.SupplierID, a.CategoryID, a.ProductID


--
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

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