SQL视图 - 挑战

时间:2014-01-29 14:25:36

标签: sql sql-server view

我在尝试创建“正在进行的工作视图”时面临一个小小的挑战

我不相信我的陈述是最好或正确的,并导致错误“子查询返回的值超过1”

我有三个关键表;

  • 任务
  • PurchaseOrderItem
  • 资源

所有表格中都有一个唯一的参考字段,例如Tasks.TA_SEQ,PurchaseOrderItem.TA_SEQ和Resource.TA_SEQ

我需要从所有这些表中总结不同的总数,关系如下;

  • 1个任务 - 许多PurchaseOrderItem
  • 1任务 - 许多资源

我需要将所有采购订单成本值(订单项可能会有所不同)与任务的有效采购订单相加,并将所有资源成本(3人 - 数量可能会有所不同)与任务相加,任何帮助都会很多赞赏。如果我能使它变得更容易,任何建议都会受到赞赏。

我的查询的一部分;

 SELECT 
    dbo.F_TASKS.TA_SEQ, 
    (
    SELECT 
        SUM(POI_TOTAL) 
    From F_PO_ITEM 
    where POI_FKEY_TA_SEQ = dbo.F_TASKS.TA_SEQ 
        and POI_FKEY_POH_SEQ in 
            (
            select 
                POH_SEQ 
            from F_PO_HEAD 
            where POH_STATUS in ('DORMANT', 'ACTIVE')
            )
    ) AS [Pending PO Cost],
    dbo.F_TASKS.TA_PO_COST AS [PO Cost], 
    dbo.F_TASKS.TA_LABOUR_COST AS [Labour Cost],
    dbo.F_TASKS.TA_LABOUR_COST - SUM(dbo.F_TASK_TIME.TT_OTHER_COSTS) AS [New Labour Cost],

-----------Not Working from
(select 
    SUM(dbo.F_TASK_TIME.TT_OTHER_COSTS) 
from F_TASK_TIME 
where TT_FKEY_TA_SEQ = dbo.F_TASKS.TA_SEQ) + dbo.F_TASKS.TA_PO_COST AS [Subcontractor Costs], 
(SUM(dbo.F_TASK_TIME.TT_OTHER_COSTS + dbo.F_TASKS.TA_PO_COST)) * 0.12 AS [Subcontractor Uplift], 
((SUM(dbo.F_TASK_TIME.TT_OTHER_COSTS + dbo.F_TASKS.TA_PO_COST)) * 0.12) + (SUM(dbo.F_TASK_TIME.TT_OTHER_COSTS + dbo.F_TASKS.TA_PO_COST)) AS [Subcontractor Uplift Total]

-----------Not Working To 

FROM dbo.F_TASKS 
LEFT OUTER JOIN  dbo.F_TASK_TIME
    ON dbo.F_TASKS.TA_SEQ = dbo.F_TASK_TIME.TT_FKEY_TA_SEQ 
LEFT OUTER JOIN dbo.F_PO_ITEM 
    ON dbo.F_TASKS.TA_SEQ = dbo.F_PO_ITEM.POI_FKEY_TA_SEQ 

WHERE (dbo.F_TASKS.TA_TASK_DESC = 'BREAKDOWN') 
    AND (dbo.F_TASKS.TA_PO_COST >= 0) 
    AND (dbo.F_TASKS.TA_STATUS IN ('ACTIVE', 'ASSIGNED', 'COMPLETE'))

GROUP BY dbo.F_TASKS.TA_PO_COST, dbo.F_TASKS.TA_SEQ, dbo.F_TASKS.TA_LABOUR_COST

1 个答案:

答案 0 :(得分:1)

我不打算尝试修复你的SQL,而是提出一个不同的做法。我不能轻易理解select子句中你所选择的所有内容,所以我刚刚完成了前两个。

此方法使用LEFT OUTER JOIN来查询总计ta_seq。这些保证只返回一行/ ta_seq,因为它是如何分组的:

SELECT 
    t.TA_SEQ,
    isnull(po.poi_total, 0) [Pending PO Cost],
    t.TA_PO_COST AS [PO Cost], 
    t.TA_LABOUR_COST AS [Labour Cost],
    t.TA_LABOUR_COST - isnull(tt.other_costs, 0) AS [New Labour Cost],
    -- other cols missed
FROM dbo.F_TASKS t
left outer join
(
    t.ta_seq, SUM(POI_TOTAL) poi_total
    From F_PO_ITEM  poi
    where POI_FKEY_POH_SEQ in 
    (
        select 
        POH_SEQ 
        from F_PO_HEAD 
        where POH_STATUS in ('DORMANT', 'ACTIVE')
    )
    group by t.ta_seq
) po on po.ta_seq = t.ta_seq
left outer join
(
    select tt.TT_FKEY_TA_SEQ ta_seq, sum(tt.tt_other_costs) other_costs
    from F_TASK_TIME tt
    group by tt.TT_FKEY_TA_SEQ
) tt on tt.ta_seq = t.ta_seq
WHERE (t.TA_TASK_DESC = 'BREAKDOWN') 
    AND (t.TA_PO_COST >= 0) 
    AND (t.TA_STATUS IN ('ACTIVE', 'ASSIGNED', 'COMPLETE'))
GROUP BY t.TA_PO_COST, t.TA_SEQ, t.TA_LABOUR_COST

我还使用了表别名,因为我发现schema.tablename格式让我失明(并没有帮我解码丢失的子查询)。 要放入缺失的列,只需将它们转换为如上所述的其他LEFT OUTER JOIN。

干杯 -