总和没有产生正确的结果T-SQL

时间:2014-05-05 12:46:16

标签: sql sql-server tsql

编辑:我已经删除了DISTINCT的完整过程(在这种情况下我们不知道这是禁止的)。基本上,我需要按日期和RC求和。

    WITH EMPLOYEE_CTE
AS
(

SELECT *

FROM DBO.[TBL_EMP]

),

T_TKT_TOTALS_CTE
as
(

SELECT 

v485.EXT_KEY1,
v485.[COMPLTG_T_ID] AS [T_UID], 
CASE WHEN LEFT(EMP.[T_Title], 1) = 'F' THEN LEFT(EMP.[T_Title], 1)
     WHEN LEFT(EMP.[T_Title], 1) = 'W' THEN LEFT(EMP.[T_Title], 1)
     WHEN LEFT(EMP.[T_Title], 1) = 'S' THEN LEFT(EMP.[T_Title], 1) END AS [TITLE],
EMP.[T_RC] AS [RC],
v485.[COMP_CAN_DT] AS [COMP_DT],

COUNT (v485.EXT_KEY1) OVER(PARTITION BY v485.[COMPLTG_T_ID], v485.[COMP_CAN_DT]) AS [T_DAILY_TOTAL]


FROM [tbl] v485,

EMPLOYEE_CTE EMP

WHERE 1=1
AND (LEN(v485.[EXT_KEY1]) < 12 )
and year(v485.[comp_can_dt]) = year(getdate())
AND LEN(v485.[COMPLTG_T_ID]) = 6
AND EMP.[T_UID] = v485.[COMPLTG_T_ID]
AND v485.[WR_STS] LIKE '%COMPLETE%'
AND v485.COMP_CAN_DT = (select MAX(COMP_CAN_DT) from [tbl] as f where F.EXT_KEY1 = v485.EXT_KEY1)

),

T_DWA_TOTALS_CTE
as
(

SELECT 

DWA.[T Id],
DWA.Jobid,
EMP.[T_RC],
DWA.[Report Date]


FROM dbo.tbl_T_DWA_Load dwa,
EMPLOYEE_CTE EMP

WHERE 1=1
AND (LEN(DWA.[Jobid]) < 12 )
AND DWA.[T Id] = EMP.[T_UID]
AND DWA.Jobid IS NOT NULL

),

T_TOTALS_CTE
AS
(

SELECT 

TKTTOT.[T_UID],
TKTTOT.[RC],
TKTTOT.[COMP_DT],
TKTTOT.[TITLE],
TKTTOT.[T_DAILY_TOTAL],

(SELECT COUNT(F.[Jobid]) OVER(PARTITION BY F.[T Id], F.[Jobid]) FROM T_DWA_TOTALS_CTE F 
WHERE (F.[T Id] = TKTTOT.[T_UID]) AND (F.[Jobid] = TKTTOT.[EXT_KEY1]) AND (F.[Report Date] = TKTTOT.[COMP_DT])) AS [T_DWA_TOTAL]


FROM T_DWA_TOTALS_CTE DWA,
T_TKT_TOTALS_CTE TKTTOT


WHERE 1=1
AND (TKTTOT.[TITLE] = 'F' OR TKTTOT.[TITLE] = 'W' OR TKTTOT.[TITLE] = 'S')

AND TKTTOT.[COMP_DT] >= (GETDATE() - 30)

),

NM_TOTALS_CTE
AS
(

//Here is where it is messing up

SELECT 
MR.MR_UID,
TOT.[RC],
TOT.[COMP_DT],
TOT.[TITLE],

SUM(CAST(TOT.[T_DAILY_TOTAL] AS DECIMAL(10,5))) OVER (PARTITION BY TOT.[RC], TOT.[COMP_DT]) AS [NM_DAILY_TOTAL],
SUM(CAST(TOT.[T_DWA_TOTAL] AS DECIMAL(10,5))) OVER (PARTITION BY TOT.[RC], TOT.[COMP_DT]) AS [NM_DWA_TOTAL]


FROM T_TOTALS_CTE TOT,
EMPLOYEE_TABLE_CTE MR

WHERE TOT.RC = MR.MR_RC

)


INSERT INTO DBO.TBL_TOTALS


SELECT  

NM.[RC],
NM.MR_UID,
NM.[COMP_DT],

T.T_UID,

NM.[TITLE],
NM.[NM_DAILY_TOTAL],
NM.[NM_DWA_TOTAL],

CASE WHEN (NM.[NM_DWA_TOTAL] > 0 AND NM.[NM_DAILY_TOTAL] > 0) THEN (NM.[NM_DWA_TOTAL] / NM.[NM_DAILY_TOTAL]) * 100  END AS [NM_PERCENTAGE]



FROM NM_TOTALS_CTE NM,
T_TOTALS_CTE T



WHERE NM.[COMP_DT] >= (GETDATE() -30)
AND NM.[RC] = T.[RC]
AND NM.[COMP_DT] = T.[COMP_DT]



END

请原谅丑陋的代码,我不是一个SQL人。只关注总和函数,我将澄清任何不清楚/错误的事情。我确定它的东西很小但看起来它应该有效。

另外,顺便说一下,如果有任何明显的缺陷我会对任何提高性能的事情持开放态度,这种情况确实很慢。

2 个答案:

答案 0 :(得分:0)

您的查询非常混乱,很难在评论部分直截了当。这是您的声明删除所有冗余。它正在做你想要的吗?如果没有,你还想要它做什么?

select distinct
  tkttot.uid,
  tkttot.rc,
  tkttot.comp_dt,
  tkttot.title,
  tkttot.daily_total,
  (
    select count(*)
    from dwa_totals_cte f
    where f. id = tkttot.uid
    and f.jobid = tkttot.ext_key1
    and f.report date = tkttot.comp_dt
  ) as dwa_total,
  sum(cast(tot.daily_total as decimal(10,5))) over (partition by tot.rc, tot.comp_dt) as nm_daily_total,
  sum(cast(tot.dwa_total   as decimal(10,5))) over (partition by tot.rc, tot.comp_dt) as nm_dwa_total
from tkt_totals_cte tkttot
where 1=1
and tkttot.title in ('f','w','s')
and tkttot.comp_dt >= getdate() - 30;
编辑:在您编辑了您的请求后,我仍然认为您错误地加入了联系。这是你的陈述被剥夺了。你真的想加入桌子并建立一个笛卡儿产品吗?是不是t_dwa_totals_cte和t_tkt_totals_cte以某种方式连接,至少是t_rc?我想你只是忘记了旧的连接语法很容易发生的连接标准。

with employee_cte
as
(
 select ...
 from dbo.tbl_emp
),
t_tkt_totals_cte
as
(
 select ...
 from tbl v485
 inner join employee_cte emp on  and emp.t_uid = v485.compltg_t_id
 where ...
),
t_dwa_totals_cte
as
(
 select ...
 from dbo.tbl_t_dwa_load dwa
 inner join employee_cte emp on dwa.t id = emp.t_uid
 where ...
),
t_totals_cte
as
(
 select ...
 from t_dwa_totals_cte dwa,
 cross join t_tkt_totals_cte tkttot -- <- is this intended?
 where ...
),
nm_totals_cte
as
(
    select ...
    from t_totals_cte tot
    inner join employee_table_cte mr on tot.rc = mr.mr_rc
)
insert into dbo.tbl_totals
 select ...
 from nm_totals_cte nm
 t_totals_cte t on and nm.comp_dt = t.comp_dt
where ...

答案 1 :(得分:0)

试试这个

SELECT TOT.[RC], TOT.[COMP_DT]
      ,sum(CAST(TOT.[DAILY_TOTAL] AS DECIMAL(10,5))) AS [NM_DAILY_TOTAL] 
      ,sum(CAST(TOT.[DWA_TOTAL]   AS DECIMAL(10,5))) AS [NM_DWA_TOTAL]  
 FROM TOTALS_CTE TOT, EMPLOYEE_TABLE_CTE     
WHERE TOT.RC = ._RC
GROUP BY TOT.[RC], TOT.[COMP_DT]

我怀疑加入EMPLOYEE_TABLE_CTE会产生重复值 为什么你甚至有加入?
._RC独一无二吗?

select ._RC, count(*) 
  from EMPLOYEE_TABLE_CTE 
 group by ._RC

并且不要使用旧的连接语法

SELECT TOT.[RC], TOT.[COMP_DT]
      ,sum(CAST(TOT.[DAILY_TOTAL] AS DECIMAL(10,5))) AS [NM_DAILY_TOTAL] 
      ,sum(CAST(TOT.[DWA_TOTAL]   AS DECIMAL(10,5))) AS [NM_DWA_TOTAL]  
 FROM TOTALS_CTE TOT, 
 JOIN EMPLOYEE_TABLE_CTE     
   on TOT.RC = ._RC
GROUP BY TOT.[RC], TOT.[COMP_DT]

未使用的联接上的._RC等列名 所有可以做的就是引入重复的行
您需要将其分解并检查数据