计算错误未返回正确的值

时间:2014-09-05 18:19:13

标签: sql sql-server tsql

我有以下脚本,并且开始计算返回的是它看起来不在分组之后的相同值。我为每所学校回来27。所有4个计算都在做同样的事情。当我手动查询数据以检查值时,它返回正确的数据。这一定是我进行计算的方式。按年级计算学生人数和每个熟练程度。有什么建议。你这家伙很棒。

  SELECT DISTINCT 
 t.descript,
 t.schoolc AS School,
-- s.ident AS StdID,
--ze.descript,
  st.grdlvl,
 -- st.takendt, 
 --CASE td.subtestc WHEN 'LI' THEN 'CELDT Listening' WHEN 'RD' THEN 'CELDT Reading' WHEN 'SP' THEN 'CELDT Speaking' WHEN 'WR' THEN 'CELDT Writing' WHEN
 --               'TO' THEN 'CELDT Overall' ELSE '' END AS ELDassessment, 
 CASE st.testscore WHEN '1' THEN 'BE' WHEN '2' THEN ' EI' WHEN '3' THEN 'IN' WHEN '4' THEN 'EA' WHEN '5' THEN 'AD' ELSE '' END AS ProfLevel,

 sum (CASE WHEN (st.testscore) IN ('1') THEN 1 ELSE 0 END) AS [Beginning],
 sum((CASE WHEN (st.testscore) IN ('2') THEN 1 ELSE 0 END)) AS [Early Intermediate],
 SUM(CASE WHEN (st.testscore) IN ('3') THEN 1 ELSE 0 END) AS [Intermediate],
 SUM(CASE WHEN (st.testscore) IN ('4') THEN 1 ELSE 0 END) AS [Early Advanced],
 SUM(CASE WHEN (st.testscore) IN ('5') THEN 1 ELSE 0 END) AS [Advanced]


FROM            dbo.stustat AS s INNER JOIN
                         dbo.track AS t ON s.trkuniq = t.trkuniq  INNER JOIN
                         dbo.stutests AS st ON s.suniq = st.suniq INNER JOIN
                         dbo.xstutests AS xst INNER JOIN
                         dbo.codeassoc AS ca3 ON xst.scrsource = ca3.zcode ON st.ststuniq = xst.ststuniq INNER JOIN
                         dbo.testval AS tv INNER JOIN
                         dbo.testdef AS td INNER JOIN
                         dbo.ztest AS zt INNER JOIN
                         dbo.codeassoc AS ca1 ON zt.testc = ca1.zcode ON td.testc = zt.testc INNER JOIN
                         dbo.zsubtest AS zst ON td.subtestc = zst.subtestc AND td.testc = zst.testc ON td.testuniq = tv.testuniq INNER JOIN
                         dbo.ztscrtyp AS ztscr INNER JOIN
                         dbo.codeassoc AS ca2 ON ztscr.tscrtypc = ca2.zcode ON tv.tscrtypc = ztscr.tscrtypc ON st.testuniq = tv.testuniq AND st.tscrtypc = tv.tscrtypc 
                        INNER JOIN studemo sd ON s.ident=sd.ident 
                        INNER JOIN dbo.zethnic ze ON sd.ethnicc=ze.ethnicc
WHERE        
--(st.takendt >= '2013-07-01') 
 (tv.tscrtypc = 'A') 
AND (td.subtestc IN ( 'TO'))
--AND (t.schoolc IN ('32'))
   AND  t.trkuniq NOT  BETWEEN '1'AND '1000066'
  AND t.trkuniq NOT BETWEEN  '1000134' AND '1000156'
 -- AND grdlvl ='0'
  --AND (st.testscore) IN ('1')

    GROUP BY  
     t.descript,
     td.subtestc,
     st.testscore,
 t.schoolc ,
 s.ident ,
--ze.descript,
  st.grdlvl
 -- st.takendt

  ORDER BY  t.descript, t.schoolc,st.grdlvl

1 个答案:

答案 0 :(得分:0)

您的联接中的ON条款到处都是我在下面猜到的地方,并试图解决其中大多数问题,请参阅下文

如果在JOIN的ON子句中有多个条件,则在连接中使用了两个ON子句,您应该使用AND来提及第二,第三个条件但不使用{{1}多次在ON子句中指定多个条件。

对于像这样的大量查询,正确的缩进可以更容易地编写/调试//修改代码。

ON