SQL SUM子查询

时间:2016-03-17 05:43:56

标签: sql sql-server-2008

无论如何都要查询这样的内容吗?

SELECT
    SUM(
        SELECT Tbl1.Cost FROM Tbl1 WHERE Tbl1.Type = 0 +
        SELECT Tbl2.Cost FROM Tbl2 WHERE Tbl2.Type = 0 +
        SELECT Tbl3.Cost FROM Tbl3 WHERE Tbl3.Type = 0
    ) AS EstimatedCost
FROM Tbl1 <JOIN> Tbl2 <JOIN> Tbl3
GROUP BY Tbl1.id,Tbl2.id,Tbl3.id,Tbl1.num,Tbl2.num,Tbl3.num

修改

当前的SQL查询我有

SELECT 
    COALESCE (vJoboutsEst.ddTask_id, vMaterialsEst.ddTask_id, vLaborsEst.ddTask_id) AS ddTask_id,
    COALESCE (vJoboutsEst.dd_id, vMaterialsEst.dd_id, vLaborsEst.dd_id) AS dd_id,
    SUM(
      CASE WHEN vJoboutsEst.jo_cost IS NULL THEN 0 ELSE vJoboutsEst.jo_cost END
      + 
      CASE WHEN vMaterialsEst.item_amt IS NULL THEN 0 ELSE vMaterialsEst.item_amt END
      + 
      CASE WHEN vLaborsEst.labor_amt IS NULL THEN 0 ELSE vLaborsEst.labor_amt END
    ) AS EstimateCost
FROM 
vDryDock,
(
    SELECT vJoboutsEst.ddTask_id, vJoboutsEst.dd_id,SUM(vJoboutsEst.jo_cost) as jo_cost
    FROM vJoboutsEst WHERE vJoboutsEst.jo_type = 0 GROUP BY vJoboutsEst.ddTask_id, vJoboutsEst.dd_id
) vJoboutsEst 
FULL OUTER JOIN (
    SELECT vMaterialsEst.ddTask_id, vMaterialsEst.dd_id,SUM(vMaterialsEst.item_amt) as item_amt
    FROM vMaterialsEst WHERE vMaterialsEst.material_type = 0 GROUP BY vMaterialsEst.ddTask_id, vMaterialsEst.dd_id
) vMaterialsEst ON vJoboutsEst.ddTask_id = vMaterialsEst.ddTask_id
FULL OUTER JOIN (
    SELECT vLaborsEst.ddTask_id, vLaborsEst.dd_id,SUM(vLaborsEst.labor_amt) as labor_amt
    FROM vLaborsEst WHERE vLaborsEst.labor_type = 0 GROUP BY vLaborsEst.ddTask_id, vLaborsEst.dd_id
) vLaborsEst ON ISNULL(vJoboutsEst.ddTask_id, vMaterialsEst.ddTask_id) = vLaborsEst.ddTask_id
GROUP BY
    vJoboutsEst.ddTask_id, vMaterialsEst.ddTask_id, vLaborsEst.ddTask_id,
    vJoboutsEst.dd_id, vMaterialsEst.dd_id, vLaborsEst.dd_id

这是我在数据库上的示例数据:

See Here

我还附加了查询输出, 但我想要的输出是这样的

ddTask_id    dd_id    EstimateCost
31           15       40
32           16       40

2 个答案:

答案 0 :(得分:0)

尝试使用此查询:

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + tasklist /s <RemoteMachineName>/u <username>/p <password>);
        Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
       procStartInfo.UseShellExecute = false;
       procStartInfo.CreateNoWindow = true;
 System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.StartInfo = procStartInfo;
      proc.Start();

答案 1 :(得分:0)

查看您的示例数据(没有任何dd_id列!),您可以使用union all语句而不是完全外连接来完成此操作。请尝试以下查询:

class is_compatible
    : public boost::static_visitor<bool>
{
public:

    template <typename T, typename U>
    bool operator()( const T &, const U & ) const
    {
        return false; // cannot compare different types
    }

    bool operator()( const A& lhs, const A& rhs ) const
    {
        // whatever A-specific comparison
    }

    bool operator()( const B& lhs, const B& rhs ) const
    {
        // whatever B-specific comparison
    }    
};