2014-06-13 68 views
0

我想解決爲什麼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) - 字符串 '非流程'

謝謝你的幫助!

+0

你可以嘗試和減少你的查詢的大小 - 這將有助於隔離和解決問題 –

+0

將嘗試,謝謝 – user2678787

回答

0

要找出差異,請運行SQL Profiler並運行您的vb.net代碼,以檢查最終要進入sql server的查詢。

+0

我的應用程序已經運行多用戶(已經實施,我只做報道的東西模塊),但是,我根據你的建議做了這個練習,並且這個查詢通過了sql分析器。 (RPC:已完成) – user2678787

+0

您是否在TextData列中檢查了查詢?本專欄將告訴你vb.net發送到sql server的執行情況。 –

+0

是, EXEC sp_executesql的N'SELECT DISTINCT DATENAME(毫米,DATEADD(毫米,lgdatm,-1))...(太長了) 這是我的每個CASE整個查詢,SUM等 – user2678787