SQL语句中的多个计算 - 结果不同

时间:2014-06-13 07:06:43

标签: sql sql-server vb.net case

我试图解决为什么vb.net代码中的sql语句给我的结果与sql server不同。 我的代码:

                            strsql = "SELECT DISTINCT DateName(mm,DATEADD(mm,lgdatm,-1)) as lgMon, Avi,  SUM((ProcElaps / 60) / Emplo) as Util, Effi, SUM(((ProcElaps / 60) * Avi) / Emplo) as DayProd, ProcElaps, ProcCount, NonProcCount FROM " & _
        "(SELECT DISTINCT lgdatm, Emplo, SUM((Emplo - (NonProcElaps / 60)) / Emplo) as Avi, SUM((ProcElaps / 60) / (Emplo- (NonProcElaps / 60))) as Effi, ProcElaps, ProcCount, NonProcCount FROM " & _
    "(SELECT DISTINCT lgdatm, SUM(460 * lgdat * EmpCount) as Emplo, NonProcElaps, ProcElaps, ProcCount, NonProcCount FROM " & _
    "(SELECT DISTINCT MONTH(LogDate) AS lgdatm, " & _
                    "Count(DISTINCT " & _
                    "CASE WHEN (ActivityCategory = @ActivAc) AND (MONTH([LogDate]) = @LGDat) THEN Enumber ELSE '0' END) AS EmpCount, " & _
                    "Count(DISTINCT " & _
                    "CASE WHEN (ActivityCategory = @ActivAc) AND (MONTH([LogDate]) = @LGDat) THEN LogDate ELSE LogDate END) lgdat, " & _
                    "Count(" & _
                    "CASE WHEN (ActivityCategory = @ActivAc) AND (MONTH([LogDate]) = @LGDat) THEN FirstActivity ELSE '0' END) AS ProcCount, " & _
                    "Count(" & _
                    "CASE WHEN (ActivityCategory = @ActivAcnon) AND (MONTH([LogDate]) = @LGDat) THEN FirstActivity ELSE '0' END) AS NonProcCount, " & _
                    "SUM(" & _
                    "CASE WHEN  (ActivityCategory = @ActivAc) AND (MONTH([LogDate]) = @LGDat) THEN Elapsed ELSE '0' END) ProcElaps, " & _
                    "SUM(" & _
                    "CASE WHEN  (ActivityCategory = @ActivAcnon) AND (MONTH([LogDate]) = @LGDat) THEN Elapsed ELSE '0' END) NonProcElaps " & _
    "FROM tblMain WHERE MONTH([LogDate]) = @LGDat " & _
    "GROUP BY MONTH(LogDate)) AS T1 " & _
    "Group By lgdatm, EmpCount, NonProcElaps, ProcElaps, ProcCount, NonProcCount) AS T2 " & _
    "Group BY lgdatm, Emplo, ProcElaps, ProcCount, NonProcCount) As T3 " & _
    "Group BY lgdatm, Avi, Effi, ProcElaps, ProcCount, NonProcCount Order By DayProd, Util, Avi, Effi"
                        Dim da As New SqlDataAdapter(strsql, cnn)
                        Dim ds As New DataSet()
                        da.SelectCommand.Parameters.AddWithValue("@ActivAc", proc)
                        da.SelectCommand.Parameters.AddWithValue("@ActivAcnon", nonproc)
                        da.SelectCommand.Parameters.AddWithValue("@LGDat", Mon)
                        da.Fill(ds, "Productivity")
                        txtDayProductivity.Text = Format(ds.Tables(0).Rows(0)(4).ToString(), "Percent")
                        txtAvailabilityTeam.Text = Format(ds.Tables(0).Rows(0)(1).ToString(), "Percent")
                        txtEfficiencyTeam.Text = Format(ds.Tables(0).Rows(0)(3).ToString(), "Percent")
                        txtUtilizationTeam.Text = Format(ds.Tables(0).Rows(0)(2).ToString(), "Percent")
                        txtTotLogg.Text = (ds.Tables(0).Rows(0)(5).ToString()) / 60
                        txtTotalTimeLogged.Text = (CDec(txtTotLogg.Text).ToString("N2")) & " mins"
                        txtAllProcess.Text = ds.Tables(0).Rows(0)(6).ToString()
                        txtAllNonProcess.Text = ds.Tables(0).Rows(0)(7).ToString()

当我将每个线程添加到单独的查询时,结果与我在执行此代码期间获得的结果完全不同。你认为这是因为CASE,还是我错误地使用了CASE。

(" @ LGDat",周一) - 周一是当月(数字) (" @ActivAc",proc) - 字符串'处理' (" @ActivAcnon",nonproc) - 字符串'非处理'

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

要找出差异,请运行SQL Profiler并运行您的vb.net代码以检查最终将进入sql server的查询。