我想解決爲什麼sql語句在vb.net代碼給我不同的結果比在sql服務器。 我的代碼:在SQL語句中的多個計算 - 不同的結果
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) - 字符串 '非流程'
謝謝你的幫助!
你可以嘗試和減少你的查詢的大小 - 這將有助於隔離和解決問題 –
將嘗試,謝謝 – user2678787