下面您将看到有些行具有相同的ContractID和Part Desc,但有两种不同类型的布尔值(一行为true,另一行为false)。我只想返回ContractID和Part Dec只有false值的行。如果是,则不返回,如果存在行为true且行为false的情况,请执行不回来这是我到目前为止所拥有的。
SELECT TOP (100) PERCENT dbo.ContractServices.ContractServiceID, dbo.ContractServices.ContractID, dbo.ContractServices.ServiceType, dbo.ContractServices.QuoteID, dbo.ContractServices.PartsCharge,
dbo.ContractParts.PartDescription, dbo.ServiceCallCharges.ChargeDescription, dbo.ServiceCalls.ServiceCallID, dbo.ServiceCallCharges.ShowOnFieldTicket, dbo.ContractServices.Renewed
FROM dbo.ContractServices INNER JOIN
dbo.ContractParts ON dbo.ContractServices.ContractServiceID = dbo.ContractParts.ContractServiceID INNER JOIN
dbo.ServiceCallCharges ON dbo.ServiceCallCharges.ChargeDescription LIKE '%' + dbo.ContractParts.PartDescription + '%' INNER JOIN
dbo.ServiceCalls ON dbo.ServiceCallCharges.ServiceCallID = dbo.ServiceCalls.ServiceCallID AND dbo.ContractServices.ContractID = dbo.ServiceCalls.ContractID AND
dbo.ContractParts.ContractID = dbo.ServiceCalls.ContractID INNER JOIN
dbo.ServiceCallServiceTypes ON dbo.ServiceCalls.ServiceCallID = dbo.ServiceCallServiceTypes.ServiceCallID
WHERE (dbo.ContractServices.PartsCharge > 0) AND (dbo.ServiceCallCharges.ChargeDescription LIKE ' -%') AND (dbo.ContractServices.Renewed = 'True') AND (dbo.ServiceCallCharges.ShowOnInvoice = 'False')
GROUP BY dbo.ContractServices.ContractServiceID, dbo.ContractServices.ContractID, dbo.ContractServices.ServiceType, dbo.ContractServices.QuoteID, dbo.ContractServices.PartsCharge, dbo.ContractParts.PartDescription,
dbo.ServiceCallCharges.ChargeDescription, dbo.ServiceCalls.ServiceCallID, dbo.ServiceCallCharges.ShowOnFieldTicket, dbo.ContractServices.Renewed
ORDER BY dbo.ContractServices.ContractID
我希望摆脱这样的一些输出:
177633 S00304 LEVEL2 3 126.6700 SAMPLE, OIL - SAMPLE, OIL True True
177633 S00304 LEVEL2 3 126.6700 SAMPLE, OIL - SAMPLE, OIL False True
198559 S00369 SERVICE 6 240.6800 FILTER, FUEL - FILTER, FUEL True True
198559 S00369 SERVICE 6 240.6800 FILTER, FUEL - FILTER, FUEL False True
你可以看到除了一个字段外,一切都是一样的。如果发生这种情况,我想从视图中删除它。我只想返回在该特定字段中只有false的值。为实现这一目标,需要进入此查询的逻辑是什么?
答案 0 :(得分:0)
您可以尝试以下查询:
SELECT
ContractServiceID,
ContractID,
ServiceType,
QuoteID,
PartsCharge,
PartDescription,
ChargeDescription,
ServiceCallID,
'False' as ShowOnFieldTicket,
Renewed
FROM
(
SELECT DISTINCT
dbo.ContractServices.ContractServiceID,
dbo.ContractServices.ContractID,
dbo.ContractServices.ServiceType,
dbo.ContractServices.QuoteID,
dbo.ContractServices.PartsCharge,
dbo.ContractParts.PartDescription,
dbo.ServiceCallCharges.ChargeDescription,
dbo.ServiceCalls.ServiceCallID,
SUM(CASE WHEN dbo.ServiceCallCharges.ShowOnFieldTicket = 'True' THEN 1 ELSE NULL)
OVER (PARTITION BY dbo.ContractServices.ContractServiceID, dbo.ContractServices.ContractID ORDER BY dbo.ContractServices.ContractID) as ShowOnFieldTicket
dbo.ContractServices.Renewed
FROM
dbo.ContractServices
INNER JOIN dbo.ContractParts
ON dbo.ContractServices.ContractServiceID = dbo.ContractParts.ContractServiceID
INNER JOIN dbo.ServiceCallCharges
ON dbo.ServiceCallCharges.ChargeDescription LIKE '%' + dbo.ContractParts.PartDescription + '%'
INNER JOIN dbo.ServiceCalls
ON dbo.ServiceCallCharges.ServiceCallID = dbo.ServiceCalls.ServiceCallID
AND dbo.ContractServices.ContractID = dbo.ServiceCalls.ContractID
AND dbo.ContractParts.ContractID = dbo.ServiceCalls.ContractID
INNER JOIN dbo.ServiceCallServiceTypes
ON dbo.ServiceCalls.ServiceCallID = dbo.ServiceCallServiceTypes.ServiceCallID
WHERE
(dbo.ContractServices.PartsCharge > 0) AND
(dbo.ServiceCallCharges.ChargeDescription LIKE ' -%') AND
(dbo.ContractServices.Renewed = 'True') AND
(dbo.ServiceCallCharges.ShowOnInvoice = 'False')
) T
WHERE ShowOnFieldTicket is NULL
ORDER BY ContractID
答案 1 :(得分:0)
WITH cte2 AS (SELECT TOP (100) PERCENT ContractServices_1.ContractServiceID, ContractServices_1.ContractID, ContractServices_1.ServiceType, ContractServices_1.QuoteID, ContractParts_1.PartDescription,
ServiceCallCharges_1.ChargeDescription, ServiceCalls_1.ServiceCallID, ServiceCallCharges_1.ShowOnFieldTicket, ContractServices_1.Renewed
FROM dbo.ContractServices AS ContractServices_1 INNER JOIN
dbo.ContractParts AS ContractParts_1 ON ContractServices_1.ContractServiceID = ContractParts_1.ContractServiceID INNER JOIN
dbo.ServiceCallCharges AS ServiceCallCharges_1 ON ServiceCallCharges_1.ChargeDescription LIKE '%' + ContractParts_1.PartDescription + '%' INNER JOIN
dbo.ServiceCalls AS ServiceCalls_1 ON ServiceCallCharges_1.ServiceCallID = ServiceCalls_1.ServiceCallID AND ContractServices_1.ContractID = ServiceCalls_1.ContractID AND
ContractParts_1.ContractID = ServiceCalls_1.ContractID
WHERE (ServiceCallCharges_1.ChargeDescription LIKE ' -%' OR
ServiceCallCharges_1.ChargeDescription LIKE '%') AND (ServiceCallCharges_1.ShowOnFieldTicket = 'True') AND (ContractServices_1.Renewed = 'True')
GROUP BY ContractServices_1.ContractServiceID, ContractServices_1.ContractID, ContractServices_1.ServiceType, ContractServices_1.QuoteID, ContractParts_1.PartDescription,
ServiceCallCharges_1.ChargeDescription, ServiceCalls_1.ServiceCallID, ContractServices_1.Renewed, ServiceCallCharges_1.ShowOnFieldTicket
ORDER BY ContractServices_1.ContractID), cte1 AS
(SELECT TOP (100) PERCENT dbo.ContractServices.ContractServiceID, dbo.ContractServices.ContractID, dbo.ContractServices.ServiceType, dbo.ContractServices.QuoteID, dbo.ContractParts.PartDescription,
dbo.ServiceCallCharges.ChargeDescription, dbo.ServiceCalls.ServiceCallID, dbo.ServiceCallCharges.ShowOnFieldTicket, dbo.ContractServices.Renewed
FROM dbo.ContractServices INNER JOIN
dbo.ContractParts ON dbo.ContractServices.ContractServiceID = dbo.ContractParts.ContractServiceID INNER JOIN
dbo.ServiceCallCharges ON dbo.ServiceCallCharges.ChargeDescription LIKE '%' + dbo.ContractParts.PartDescription + '%' INNER JOIN
dbo.ServiceCalls ON dbo.ServiceCallCharges.ServiceCallID = dbo.ServiceCalls.ServiceCallID AND dbo.ContractServices.ContractID = dbo.ServiceCalls.ContractID AND
dbo.ContractParts.ContractID = dbo.ServiceCalls.ContractID
WHERE (dbo.ServiceCallCharges.ChargeDescription LIKE ' -%' OR
dbo.ServiceCallCharges.ChargeDescription LIKE '%') AND (dbo.ServiceCallCharges.ShowOnFieldTicket = 'False') AND (dbo.ContractServices.Renewed = 'True')
GROUP BY dbo.ContractServices.ContractServiceID, dbo.ContractServices.ContractID, dbo.ContractServices.ServiceType, dbo.ContractServices.QuoteID, dbo.ContractParts.PartDescription,
dbo.ServiceCallCharges.ChargeDescription, dbo.ServiceCalls.ServiceCallID, dbo.ContractServices.Renewed, dbo.ServiceCallCharges.ShowOnFieldTicket
ORDER BY dbo.ContractServices.ContractID)
SELECT TOP (100) PERCENT cte1_1.ContractServiceID, cte1_1.ContractID, cte1_1.ServiceType, cte1_1.QuoteID, cte1_1.PartDescription, cte1_1.ChargeDescription, cte1_1.ServiceCallID, cte1_1.ShowOnFieldTicket,
cte1_1.Renewed
FROM cte1 AS cte1_1 LEFT OUTER JOIN
cte2 AS cte2_1 ON cte1_1.PartDescription = cte2_1.PartDescription AND cte1_1.ContractID = cte2_1.ContractID
WHERE (cte2_1.ShowOnFieldTicket IS NULL)
GROUP BY cte1_1.ContractServiceID, cte1_1.ContractID, cte1_1.ServiceType, cte1_1.QuoteID, cte1_1.PartDescription, cte1_1.ChargeDescription, cte1_1.ServiceCallID, cte1_1.ShowOnFieldTicket, cte1_1.Renewed,
cte2_1.ShowOnFieldTicket
ORDER BY cte1_1.ContractID