如果不应该

时间:2016-03-10 09:25:07

标签: sql sql-server tsql

我提出了一个查询,以了解我们为每个采购订单的客户解决的所有“门票”。我注册了3张不同的门票,1小时工作时间,2小时工作时间和4小时工作时间。在我的概述中,我应该很好地获得三张票,并且有时间处理它,所以3行。出于某种原因,我得到了6行,每张票3张,所以每张票2张。第一个是1个工作小时和2个工作小时的总和,因此3个小时,第二个工作时间是4小时工作小时。我这些时间只在单独的门票上注册,但是它们被添加到所有门票中,我不知道它是如何可能的。我认为它可能与purchaseorderID有关?

Result

    SELECT
    po.PurchaseOrderId, 
    po.Reference AS PO,
    po.CustomerPurchaseOrderId AS CustomerPurchaseOrderId,
    po.BudgetPrice,
    ti.subject,
    ti.ticketid,
    SUM(reg.DurationHours) AS DurationHours,
    reg.IsOutOfHours,
    CASE DATEPART(DW, reg.RegistrationDate) WHEN 7 THEN 1 ELSE 0 END AS IsSunday,

    CASE
        WHEN reg.IsOutOfHours = 1 AND CASE DATEPART(DW, reg.RegistrationDate) WHEN 7 THEN 1 ELSE 0 END = 1 THEN po2e.HourlyRate * 2
        WHEN reg.IsOutOfHours = 1 AND CASE DATEPART(DW, reg.RegistrationDate) WHEN 7 THEN 1 ELSE 0 END = 0 THEN po2e.HourlyRate * 1.5
        ELSE po2e.HourlyRate
    END AS HourlyRate
    FROM Registration reg
        LEFT OUTER JOIN Employee emp ON reg.EmployeeId = emp.EmployeeId
        LEFT OUTER JOIN PurchaseOrder po ON reg.PurchaseOrderId = po.PurchaseOrderId
        LEFT 

OUTER JOIN Ticket ti on ti.PurchaseOrderId=po.PurchaseOrderId
        LEFT OUTER JOIN PurchaseOrder2Employee po2e ON po2e.EmployeeId = emp.EmployeeId AND po2e.PurchaseOrderId = po.PurchaseOrderId
    WHERE po.PurchaseOrderId=1042
    GROUP BY ti.subject, ti.ticketid, po2e.HourlyRate, po.PurchaseOrderId, po.Reference, po.BudgetPrice ,po.CustomerPurchaseOrderId,reg.IsOutOfHours, CASE DATEPART(DW, reg.RegistrationDate) WHEN 7 THEN 1 ELSE 0 END
        order by po;

Keys

Registrations

1 个答案:

答案 0 :(得分:1)

我认为这是导致问题的reg.isOutOfHours,尝试从select和GroupBy中取出,看看是否会产生影响。