排除 sql 中的某些方面
Excluding certain aspects in sql
我希望有人能帮助我。
我有以下 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
出于保护目的,信息已更改。所以说我需要能够列出所有具有“已接收” activity 但不是“已交付” activity 的 ABC 银行的分配订单,我需要 return CustomerName,CustomerID, LoanNumber 和“接收日期”(Activity 接收日期)。我已经能够完成所有事情,但是,我无法删除任何没有 Activity 或 "Delivered" 的文件。这是我目前所拥有的:
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
试试这个。不是世界上最有效的 t-sql ,但模式并没有多大帮助。可能需要在主查询中为表添加别名以使其更具可读性并确保子查询正常工作(至少我通常是这样做的)。
这应该return 所有收到但没有交付的记录
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
下面是我将如何进行查询
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
trim 当然是你想要的列
我希望有人能帮助我。
我有以下 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
出于保护目的,信息已更改。所以说我需要能够列出所有具有“已接收” activity 但不是“已交付” activity 的 ABC 银行的分配订单,我需要 return CustomerName,CustomerID, LoanNumber 和“接收日期”(Activity 接收日期)。我已经能够完成所有事情,但是,我无法删除任何没有 Activity 或 "Delivered" 的文件。这是我目前所拥有的:
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
试试这个。不是世界上最有效的 t-sql ,但模式并没有多大帮助。可能需要在主查询中为表添加别名以使其更具可读性并确保子查询正常工作(至少我通常是这样做的)。
这应该return 所有收到但没有交付的记录
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
下面是我将如何进行查询
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
trim 当然是你想要的列