Use the Northwind database Products table as an example.
Purchasing dept gets a report showing when inventory items on hand qty are
below the reorder level.
easy enough:
Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel
from Products
where (UnitsInStock < ReorderLevel)
Results:
ProductID ProductName SupplierID UnitsInStock ReorderLevel
2 Chang 1 17
25
3 Aniseed Syrup 1 13
25
It would be nice to know what other products are purchased from this same
vendor in case other items are close to their reorder level.
All products for Supplier ID 1
Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel
from Products
where SupplierID = 1
Results:
ProductID ProductName SupplierID UnitsInStock ReorderLevel
1 Chai 1 39
10
2 Chang 1 17
25
3 Aniseed Syrup 1 13
25
This shows there is 1 more product (Chai) that also comes from Supplier 1.
Is there a way to show all items from a vendor when some of the items are
below the reorder level without needing a separate query for each vendor?
Thanks 4 6070
rdraider wrote:
Use the Northwind database Products table as an example.
Purchasing dept gets a report showing when inventory items on hand qty are
below the reorder level.
easy enough:
Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel
from Products
where (UnitsInStock < ReorderLevel)
Results:
ProductID ProductName SupplierID UnitsInStock ReorderLevel
2 Chang 1 17
25
3 Aniseed Syrup 1 13
25
It would be nice to know what other products are purchased from this same
vendor in case other items are close to their reorder level.
All products for Supplier ID 1
Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel
from Products
where SupplierID = 1
Results:
ProductID ProductName SupplierID UnitsInStock ReorderLevel
1 Chai 1 39
10
2 Chang 1 17
25
3 Aniseed Syrup 1 13
25
This shows there is 1 more product (Chai) that also comes from Supplier 1.
Is there a way to show all items from a vendor when some of the items are
below the reorder level without needing a separate query for each vendor?
Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel
from Products
where SupplierID in (
select SupplierID
from Products
where UnitsInStock < ReorderLevel
)
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.
"Ed Murphy" <em*******@socal.rr.comwrote in message
news:46***********************@roadrunner.com...
rdraider wrote:
>Use the Northwind database Products table as an example. Purchasing dept gets a report showing when inventory items on hand qty are below the reorder level. easy enough: Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel from Products where (UnitsInStock < ReorderLevel)
Results: ProductID ProductName SupplierID UnitsInStock ReorderLevel 2 Chang 1 17 25 3 Aniseed Syrup 1 13 25
It would be nice to know what other products are purchased from this same vendor in case other items are close to their reorder level.
All products for Supplier ID 1 Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel from Products where SupplierID = 1
Results: ProductID ProductName SupplierID UnitsInStock ReorderLevel 1 Chai 1 39 10 2 Chang 1 17 25 3 Aniseed Syrup 1 13 25
This shows there is 1 more product (Chai) that also comes from Supplier 1. Is there a way to show all items from a vendor when some of the items are below the reorder level without needing a separate query for each vendor?
Select ProductID, ProductName, SupplierID, UnitsInStock, ReorderLevel
from Products
where SupplierID in (
select SupplierID
from Products
where UnitsInStock < ReorderLevel
)
rdraider (rd******@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, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
Thanks for your help. A very good lesson my a newbie like me.
"Erland Sommarskog" <es****@sommarskog.sewrote in message
news:Xn**********************@127.0.0.1...
rdraider (rd******@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, es****@sommarskog.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bernie |
last post by:
Greetings,
I have 3 servers all running SQL Server 2000 - 8.00.818. Lets call
them parent, child1, and child 2.
On parent, I create a view called item as follows:
CREATE view Item as...
|
by: Damroo |
last post by:
I have a table as below
---------------------------
name linkcode level
---------------------------
brando 1,3,8 1
damroo 1,5 2
rogers 2,7 1
shane 1,7 ...
|
by: Steve |
last post by:
I have a products table where the PK is ProductID. Also have the standard Orders
table and OrderDetails table. I created a query that joins the Orders table and
OrderDetails table. The query...
|
by: Nathan Plamondon |
last post by:
I am writing a parts inventory database for my workplace. I have it
mostly working, but the reordering report isn't quite right. I want
the report to show me parts that need to be ordered. The...
|
by: kev |
last post by:
Hi folks,
Firstly, Happy New Year to all. I have some problems with query pls
help.I have 4 tables namely Equipment,Safety1,Safety2,Safety3. some of
the fileds in each table is shown below....
|
by: shauna |
last post by:
hi, i am an As level student studying Applied ICT, im having problems with normalisation.
our problem is to computerise a made up business.mine for example is a beauty salon. below are my...
|
by: ismomo |
last post by:
Hi all, I am facing a problem in recursive. I would like to create node strcuture similar with the xml structure shown below. However, I am not able to create the node with the sub I written in perl....
|
by: tizmagik |
last post by:
MS Access Database
I'm working on a 'Reorder' function for my friend's database.
Essentially, it is a 'deep-copy' of several related tables. Here is
the table setup...
tblSalesOrder...
|
by: John Torres |
last post by:
I am working with the Inventory Dbase and I have a “Reorder Level” field which the quantity varies. I also have a query field Qty On Hand
Qty On Hand: Sum(!-!)
I’m trying to make a query to...
|
by: zion4ever |
last post by:
Hello good people,
Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |