SQL QUERY FOR DATABASE ON Microsoft SQL Server

时间:2017-05-29 17:06:04

标签: sql sql-server

我的查询根本不起作用,一直在努力查看错误但找不到它。说有两个相同的值,但说实话,我正在尽力证明我的代码:

SELECT
"HQCO"."HQCo",
"HQCO"."Name",
"JCJP"."Contract",
"JCJP"."Item",
"JCJP"."Phase",
"JCCP"."CostType",
"JCCI"."Item",
"JCCI"."Description",
"JCCP"."Phase",
"JCJP"."Description",
"JCCT"."Description",
"JCCH"."UM",
"JCCP"."CurrEstUnits",
"JCCP"."CurrEstCost",
"JCCP"."ActualUnits",
"JCCP"."ActualCost",
"JCCM"."Description",
"JCJP"."JCCo",
"JCCP"."ProjCost",
"JCCP"."RemainCmtdCost",
"JCCP"."ProjPlug",
"JCCP"."Mth",
"JCCI"."BilledAmt",
"JCCM"."BilledAmt",
"JCCM"."udGEACrev",
"JCCP"."Job",
"JCCM"."Department",
"JCJM"."ProjectMgr",
"JCMP"."Name"

FROM
 (
(((((("Viewpoint"."dbo"."JCCP" "JCCP" 
      INNER JOIN
         "Viewpoint"."dbo"."HQCO" "HQCO" 
         ON "JCCP"."JCCo" = "HQCO"."HQCo")
      INNER JOIN
         "Viewpoint"."dbo"."JCCT" "JCCT" 
         ON ("JCCP"."PhaseGroup" = "JCCT"."PhaseGroup") 
         AND 
         (
             "JCCP"."CostType" = "JCCT"."CostType"
         )
) 
      LEFT OUTER JOIN
         "Viewpoint"."dbo"."JCCH" "JCCH" 
         ON (((("JCCP"."JCCo" = "JCCH"."JCCo") 
         AND 
         (
            "JCCP"."Job" = "JCCH"."Job"
         )
) 
         AND 
         (
            "JCCP"."PhaseGroup" = "JCCH"."PhaseGroup"
         )
) 
         AND 
         (
            "JCCP"."Phase" = "JCCH"."Phase"
         )
) 
         AND 
         (
            "JCCP"."CostType" = "JCCH"."CostType"
         )
 ) 
           LEFT OUTER JOIN
             "Viewpoint"."dbo"."JCJP" "JCJP" 
          ON ((("JCCP"."JCCo" = "JCJP"."JCCo") 
          AND 
         (
            "JCCP"."Job" = "JCJP"."Job"
         )
) 
          AND 
         (
            "JCCP"."PhaseGroup" = "JCJP"."PhaseGroup"
         )
) 
          AND 
         (
            "JCCP"."Phase" = "JCJP"."Phase"
         )
)
       LEFT OUTER JOIN
          "Viewpoint"."dbo"."JCCI" "JCCI" 
         ON (("JCJP"."JCCo" = "JCCI"."JCCo") 
         AND 
         (
            "JCJP"."Contract" = "JCCI"."Contract"
         )
) 
         AND 
         (
             "JCJP"."Item" = "JCCI"."Item"
         )
)
      INNER JOIN
         "Viewpoint"."dbo"."JCCM" "JCCM" 
         ON ("JCCI"."JCCo" = "JCCM"."JCCo") 
         AND 
         (
            "JCCI"."Contract" = "JCCM"."Contract"
         )
) 
       INNER JOIN
          "Viewpoint"."dbo"."JCJM" "JCJM" 
           ON ("JCCM"."JCCo" = "JCJM"."JCCo") 
           AND 
         (
           "JCCM"."Contract" = "JCJM"."Job"
          )
   )
      LEFT OUTER JOIN
      "Viewpoint"."dbo"."JCMP" "JCMP" 
       ON ("JCJM"."JCCo" = "JCMP"."JCCo") 
       AND 
       (
          "JCJM"."ProjectMgr" = "JCMP"."ProjectMgr"
       )




WHERE
   "JCCP"."Mth" < {ts '2017-04-02 00:00:00'} 
   AND "JCJP"."JCCo" = 1 
   AND "JCJM"."ProjectMgr" = 8 
   AND "JCCM"."Department" = '10' 
    ORDER BY
       "JCJP"."Contract", "JCJP"."Item", "JCJP"."Phase", "JCCP"."CostType"  

正如您所看到的,所有表和值都是相关的,并与内部和外部联接相结合以补充表。这是一份报告

1 个答案:

答案 0 :(得分:0)

您的查询有多个问题。

1) Problem with parenthese (too '(')
2) You use left outer join on table and you test a zone of this table -> null will be excluded like it
3) Multiples Zones in your select with same name
4) Quotes for zones and alias for tables are not necessary

请尝试保留此查询:

SELECT
HQCO.HQCo,
HQCO.Name,
JCJP.Contract,
JCJP.Item,
JCJP.Phase,
JCCP.CostType,
JCCI.Item,
JCCI.Description Desc1,
JCCP.Phase,
JCJP.Description Desc2,
JCCT.Description Desc3,
JCCH.UM,
JCCP.CurrEstUnits,
JCCP.CurrEstCost,
JCCP.ActualUnits,
JCCP.ActualCost,
JCCM.Description Desc4,
JCJP.JCCo,
JCCP.ProjCost,
JCCP.RemainCmtdCost,
JCCP.ProjPlug,
JCCP.Mth,
JCCI.BilledAmt BilledAmt1,
JCCM.BilledAmt BilledAmt2,
JCCM.udGEACrev,
JCCP.Job,
JCCM.Department,
JCJM.ProjectMgr,
JCMP.Name Name2

FROM
      Viewpoint.dbo.JCCP  
      INNER JOIN Viewpoint.dbo.HQCO ON JCCP.JCCo = HQCO.HQCo
      INNER JOIN Viewpoint.dbo.JCCT ON JCCP.PhaseGroup = JCCT.PhaseGroup AND JCCP.CostType = JCCT.CostType
      LEFT OUTER JOIN Viewpoint.dbo.JCCH ON JCCP.JCCo = JCCH.JCCo AND JCCP.Job = JCCH.Job 
      AND JCCP.PhaseGroup = JCCH.PhaseGroup AND JCCP.Phase = JCCH.Phase AND JCCP.CostType = JCCH.CostType
      LEFT OUTER JOIN Viewpoint.dbo.JCJP ON JCCP.JCCo = JCJP.JCCo AND JCCP.Job = JCJP.Job 
      AND JCCP.PhaseGroup = JCJP.PhaseGroup AND JCCP.Phase = JCJP.Phase AND JCJP.JCCo = 1 
      LEFT OUTER JOIN Viewpoint.dbo.JCCI ON JCJP.JCCo = JCCI.JCCo AND JCJP.Contract = JCCI.Contract AND JCJP.Item = JCCI.Item
      INNER JOIN Viewpoint.dbo.JCCM ON JCCI.JCCo = JCCM.JCCo AND JCCI.Contract = JCCM.Contract
      INNER JOIN Viewpoint.dbo.JCJM ON JCCM.JCCo = JCJM.JCCo AND JCCM.Contract = JCJM.Job
      LEFT OUTER JOIN Viewpoint.dbo.JCMP ON JCJM.JCCo = JCMP.JCCo AND JCJM.ProjectMgr = JCMP.ProjectMgr

WHERE
   JCCP.Mth < {ts '2017-04-02 00:00:00'} 
   AND JCJM.ProjectMgr = 8 
   AND JCCM.Department = '10' 
ORDER BY JCJP.Contract, JCJP.Item, JCJP.Phase, JCCP.CostType