排除sql中的某些方面

时间:2015-01-06 19:44:45

标签: sql-server

我希望有人可以帮助我。

我有以下5个表:

tblCustomer

CustomerID   CustomerName   
 -------     ------------
   1         ABC Bank     
   2         Chase Bank      

tblOrderType

OrderTypeID    OrderTypeName   
 ----------    ------------
   1           Assignment     
   2           LienRelease  

tblActivity

ActivityID        ActivityName   
 ----------       ------------
   1              Received     
   2              Keyed
   3              Printed
   4              Delivered To Cusotmer

tblOrder

OrderID        CustomerID      OrderTypeID   LoanNumber
 ----------    ------------    -----------   ----------
   1           1                1            45584565
   2           1                1            45566856
   3           1                1            45565584
   4           1                1            45588545

tblOrderActivity

OrderID        ActivityID    ActivityDate
 ----------    -----------   ----------
   1           1             2007-04-16 8:34:00 AM
   1           2             2007-04-16 9:22:00 AM  
   1           3             2007-04-16 9:51:00 AM 
   1           4             2007-04-16 4:14:00 PM  
   2           1             2007-04-16 8:34:00 AM
   3           1             2007-04-16 8:34:00 AM
   3           2             2007-04-16 9:22:00 AM
   3           3             2007-04-16 9:51:00 AM
   3           4             2007-04-16 4:14:00 PM
   4           1             2007-04-16 8:34:00 AM
   4           2             2007-04-16 9:22:00 AM
   4           3             2007-04-16 9:51:00 AM 

为保护目的,信息已被更改。所以说我需要能够列出所有具有“收到”活动而不是ABC银行“已交付”活动的分配订单,我需要返回客户名称,客户ID,贷款编号和“收到日期”(收到的活动日期) )。我已经能够完成所有工作,但是,我无法删除任何没有“已交付”活动的文件。以下是我到目前为止的情况:

SELECT tblCustomer.CustomerName, tblCustomer.CustomerID, tblOrder.LoanNumber, (tblOrderActivity.ActivityDate) AS [received date]
FROM tblOrderActivity (NOLOCK)

INNER JOIN tblOrder (NOLOCK)
ON tblOrderActivity.OrderID = tblOrder.OrderID

INNER JOIN tblCustomer (NOLOCK)
ON tblOrder.CustomerID = tblCustomer.CustomerID

INNER JOIN tblOrderType (NOLOCK)
ON tblOrder.OrderTypeID = tblOrderType.OrderTypeID

INNER JOIN tblActivity (NOLOCK)
ON tblActivity.ActivityID = tblOrderActivity.ActivityID

WHERE tblOrderType.OrderTypeName = 'Assignment' 
AND EXISTS (SELECT DISTINCT [OrderID] FROM [tblOrderActivity] WHERE tblActivity.ActivityName = 'Received')
AND NOT EXISTS (SELECT DISTINCT [OrderID] FROM [tblOrderActivity] WHERE tblActivity.ActivityName = 'Delivered To Customer')

GROUP BY tblCustomer.CustomerName, tblCustomer.CustomerID, tblOrder.LoanNumber, tblOrderActivity.ActivityDate;

我的结果低于

CustomerName   CustomerID      LoanNumber    received date
 ----------    ------------    -----------   ----------
 ABC Bank      1               45565584      2007-04-16 08:34:00.000
 ABC Bank      1               45566856      2007-04-16 08:34:00.000
 ABC Bank      1               45584565      2007-04-16 08:34:00.000
 ABC Bank      1               45588545      2007-04-16 08:34:00.000

2 个答案:

答案 0 :(得分:0)

试试这个。不是世界上最有效的t-sql,但架构没有多大帮助。可能需要对主查询中的表进行别名以使其更具可读性,并确保子查询正常工作(至少这是我通常所做的)。

这应该返回所有没有交付的已接收记录

SELECT tblCustomer.CustomerName, tblCustomer.CustomerID, tblOrder.LoanNumber, (tblOrderActivity.ActivityDate) AS [received date]
FROM tblOrderActivity (NOLOCK)

INNER JOIN tblOrder (NOLOCK)
ON tblOrderActivity.OrderID = tblOrder.OrderID

INNER JOIN tblCustomer (NOLOCK)
ON tblOrder.CustomerID = tblCustomer.CustomerID

INNER JOIN tblOrderType (NOLOCK)
ON tblOrder.OrderTypeID = tblOrderType.OrderTypeID

INNER JOIN tblActivity (NOLOCK)
ON tblActivity.ActivityID = tblOrderActivity.ActivityID

WHERE tblOrderType.OrderTypeName = 'Assignment' 

AND NOT EXISTS (SELECT 1 FROM [tblOrderActivity] WHERE OA.ActivityID = 4 AND OA.OrderID = tblOrder.OrderID)  --Delivered Excluded
AND tblOrderActivity.ActivityID = 1  --Received Only

答案 1 :(得分:0)

以下是我将如何进行查询

SELECT * 
FROM tblOrder
LEFT JOIN (
    SELECT 'r' AS r, tblOrderActivity.ActivityDate, tblActivity.ActivityName, tblOrderActivity.OrderID
    FROM tblOrderActivity
    JOIN tblActivity ON tblOrderActivity.ActivityID = tblActivity.ActivityID 
        AND tblActivity.ActivityName = 'Received'
) AS Received ON tblOrder.OrderID = Received.OrderID
LEFT JOIN (
    SELECT 'd' AS d, tblOrderActivity.ActivityDate, tblActivity.ActivityName, tblOrderActivity.OrderID
    FROM tblOrderActivity
    JOIN tblActivity ON tblOrderActivity.ActivityID = tblActivity.ActivityID 
        AND tblActivity.ActivityName = 'Delivered To Cusotmer'
) AS Delivered ON tblOrder.OrderID = Delivered.OrderID
WHERE Delivered.ActivityDate IS NULL

或者像这样

SELECT * 
FROM tblOrder
LEFT JOIN (
    SELECT 'r' AS r, tblOrderActivity.ActivityDate, tblActivity.ActivityName, tblOrderActivity.OrderID
    FROM tblOrderActivity
    JOIN tblActivity ON tblOrderActivity.ActivityID = tblActivity.ActivityID 
        AND tblActivity.ActivityID IN (SELECT ActivityID FROM tblActivity WHERE tblActivity.ActivityName = 'Received')
) AS Received ON tblOrder.OrderID = Received.OrderID
LEFT JOIN (
    SELECT 'd' AS d, tblOrderActivity.ActivityDate, tblActivity.ActivityName, tblOrderActivity.OrderID
    FROM tblOrderActivity
    JOIN tblActivity ON tblOrderActivity.ActivityID = tblActivity.ActivityID 
        AND tblActivity.ActivityID IN (SELECT ActivityID FROM tblActivity WHERE tblActivity.ActivityName = 'Delivered To Cusotmer')
) AS Delivered ON tblOrder.OrderID = Delivered.OrderID
WHERE Delivered.ActivityDate IS NULL

将其修剪为您想要的列