简体   繁体   English

存储过程SQL Server 2008 NOT IN

[英]Stored procedure sql server 2008 NOT IN

I have created a query which allows me to view all of the items that have been ordered by customers where the company name = Best Baths using the following code. 我创建了一个查询,该查询使我可以使用以下代码查看公司名称= Best Baths的客户所订购的所有项目。

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DateReceived,

CASE WHEN la.Status = 'Blocked' THEN 'Blocked' ELSE 'Active' END AS Status

from Customers cu
-- inner join the following to find customers orders and cost
inner join CustomerOrder co on co.CustomerID = cu.CustomerID
inner join ItemOrder io on co.ID = io.ItemOrderID
inner join materialItem mi on io.MaterialID = mi.MaterialItemID
inner join Material ma on io.MaterialID = ma.MaterialItemID
left join ItemForInvoice ifi on io.ItemOrderID = ifi.ItemOrderID
left join Invoice iv on ifi.InvoiceItemID = iv.InvoiceItemID
left join PaymentMethod pm on iv.InvoiceID = pm.InvoiceID
left join LockedAccount la on pm.PaymentID = la.PaymentID
inner join Suppliers su on mi.SupplierID = su.SuppliersID
inner join SupplierDetails sd on su.SuppliersID = sd.SuppliersID
Where su.SuppliersName = 'Best Baths'

I now want to create a query thats shows items that have not yet been ordered, can anybody point me in the right direction? 现在,我想创建一个查询,该查询显示尚未订购的物品,有人可以向我指出正确的方向吗?

It's not obvious from your query as to what tables what, but the basic query would be something like 从查询中不知道什么表是什么,但是基本查询将是

SELECT items.id, COUNT(orders.itemid) AS cnt
FROM items
LEFT JOIN orders ON items.id = orders.itemid
GROUP BY items.id
HAVING (cnt = 0)

Annoscia, look this example: Annoscia,看这个例子:

(SELECT coll FROM TableA)
EXCEPT
(SELECT coll FROM TableB)

it's mean: 意思是:

SELECT DISTINCT coll
  FROM TableA 
 WHERE NOT EXISTS (SELECT * 
                     FROM TableB 
                    WHERE TableA.col1 = TableB.col1)

for your case it will be so: 对于您的情况将是这样:

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DateReceived,    
CASE WHEN la.Status = 'Blocked' THEN 'Blocked' ELSE 'Active' END AS Status

from Customers cu
-- inner join the following to find customers orders and cost
inner join CustomerOrder co on co.CustomerID = cu.CustomerID
inner join ItemOrder io on co.ID = io.ItemOrderID
inner join materialItem mi on io.MaterialID = mi.MaterialItemID
inner join Material ma on io.MaterialID = ma.MaterialItemID
left join ItemForInvoice ifi on io.ItemOrderID = ifi.ItemOrderID
left join Invoice iv on ifi.InvoiceItemID = iv.InvoiceItemID
left join PaymentMethod pm on iv.InvoiceID = pm.InvoiceID
left join LockedAccount la on pm.PaymentID = la.PaymentID
inner join Suppliers su on mi.SupplierID = su.SuppliersID
inner join SupplierDetails sd on su.SuppliersID = sd.SuppliersID

EXCEPT

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DateReceived,

CASE WHEN la.Status = 'Blocked' THEN 'Blocked' ELSE 'Active' END AS Status

from Customers cu
-- inner join the following to find customers orders and cost
inner join CustomerOrder co on co.CustomerID = cu.CustomerID
inner join ItemOrder io on co.ID = io.ItemOrderID
inner join materialItem mi on io.MaterialID = mi.MaterialItemID
inner join Material ma on io.MaterialID = ma.MaterialItemID
left join ItemForInvoice ifi on io.ItemOrderID = ifi.ItemOrderID
left join Invoice iv on ifi.InvoiceItemID = iv.InvoiceItemID
left join PaymentMethod pm on iv.InvoiceID = pm.InvoiceID
left join LockedAccount la on pm.PaymentID = la.PaymentID
inner join Suppliers su on mi.SupplierID = su.SuppliersID
inner join SupplierDetails sd on su.SuppliersID = sd.SuppliersID
Where su.SuppliersName = 'Best Baths'

If you're after the materials (ie item) that have not yet been ordered, then an NOT EXISTS clause will work well. 如果您要处理尚未订购的物料(即物料),则NOT EXISTS子句会很好地工作。 You only need to check that it has not been involved in an order via ItemOrder table. 您只需要通过ItemOrder表检查它是否未包含在订单中。

Select ma.MaterialName as [Item],
       mi.Price as [Item Price]
  from Material ma
  join materialItem mi on io.MaterialID = mi.MaterialItemID
 where not exists (select *
                     from ItemOrder io
                    where io.MaterialID = ma.MaterialItemID)

PLEASE do not quote column aliases using ( ' ) single quotes in future, use the square brackets. 以后请不要使用( ' )单引号来引用列别名,请使用方括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM