sql
  • vb.net
  • 2017-07-06 188 views 0 likes 
    0

    當前嘗試按日期進行分組,但忽略提示它的時間,因爲我的dgv按時間對其進行了分組,但我環顧四周,正在努力適應工作。這是可以工作的原始代碼,但是按日期/時間分組。VB.net GROUP By日期忽略時間

    Dim sql2 
        Try 
    
         connect() 
         sql2 = "SELECT r_datetime, SUM(r_quantity) FROM [r_repair] WHERE r_location = 'CNV' AND r_line =('" & computer_line & "') AND r_datetime >= @startdata2 AND r_datetime < @enddata2 AND r_area = 'DPMO(T)' GROUP BY r_datetime" 
         Dim adapter2 As New SqlDataAdapter(sql2, con) 
    
         With adapter2.SelectCommand.Parameters 
          .Clear() 
    
          .Add("@startdata2", SqlDbType.DateTime).Value = monthly_from.Value 
          .Add("@enddata2", SqlDbType.DateTime).Value = monthly_to.Value.AddDays(1) 
         End With 
    
         Dim table_stencil As New DataTable() 
         table_stencil.Locale = System.Globalization.CultureInfo.InvariantCulture 
         adapter2.Fill(table_stencil) 
         Me.bs_m_t.DataSource = table_stencil 
    
         dgv_m_t.DataSource = bs_m_t 
    
         disconnect() 
    
         dgv_m_t.RowHeadersWidth = "28" 
    
         dgv_m_t.Columns(0).HeaderText = "Date:" 
         dgv_m_t.Columns(1).HeaderText = "Quantity:" 
    
         dgv_m_t.EnableHeadersVisualStyles = False 
    
        Catch ex As System.Exception 
        End Try 
    

    任何想法的?

    乾杯, 皮特

    回答

    0

    首先,您應該始終參數化所有變量。

    第二,如果你想按日期組,你不能在選擇列表中的日期時間,你必須做這樣的事情:

    SELECT cast(r_datetime as date) as r_date, 
         SUM(r_quantity) as r_sum_quantity 
    FROM [r_repair] 
    WHERE r_location = 'CNV' 
    AND r_line = @r_line 
    AND r_datetime >= @startdata2 
    AND r_datetime < @enddata2 
    AND r_area = 'DPMO(T)' 
    GROUP BY CAST(r_datetime as date) 
    
    +0

    就像一個治療 - 只是一個簡單的一,我的數據是02/07/2017和04/07/2017 - 它很容易讓它顯示一個日期,沒有數據給它的數量爲0,所以在dgv它將展示01/07/2017,02/07/2017,03/07/2017 .....? –

    +0

    首先,我[很高興幫助](http://meta.stackoverflow.com/questions/291325/how-to-show-appreciation-to-a-user-on-stackoverflow/291327#291327) 。其次,你可能想看看[這個鏈接](https://stackoverflow.com/questions/38218157/get-all-day-of-month-then-mapping-data-to-specific-date/38219556#38219556 )。 –

    0

    使用CONVERT(DATE, r_datetime)date數據類型沒有時間組件。

    相關問題