Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
925
Using Pivot with SQL server
posted

I am using SQL server with my application.  I need to create Pivot tables in various formats.  I am not sure if a binding between a SQL database and the Pivot control is possible, but if it is can you please help me with sample code to achieve something like I did with the code below

Public Sub FIllGrid()

Dim cmd As New SqlClient.SqlCommand

cmd.Connection = con

Dim Table As New DataTable

If con.State = ConnectionState.Closed Then

con.Open()

End If

cmd.CommandText = " SELECT distinct QUALIFICATION.[SETA] as [SETA] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Gender_Code] ='M' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [MALE] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Gender_Code] ='F' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [FEMALE] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='BA' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [BA] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='BC' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [BC] " & _

" " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='BI' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [BI] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='WH' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [WH] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='U' and " & _

"QUALIFICATION.[DateRegisRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [U] " & _

", " & _

" " & _

" " & _

"SUM(CASE " & _

"WHEN Student.[Gender_Code] ='M' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [MALE 2] " & _

" , " & _

"SUM(CASE " & _

"WHEN STUDENT.[Gender_Code] ='F' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [FEMALE 2] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='BA' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [BA 2] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='BC' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [BC 2] " & _

" " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='BI' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [BI 2] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='WH' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [WH 2] " & _

", " & _

"SUM(CASE " & _

"WHEN STUDENT.[Equity_Code] ='U' and " & _

"QUALIFICATION.[DatecompRes] " & _

"BETWEEN @StartDate and @EndDate THEN 1 ELSE 0 END) as [U 2] " & _

" " & _

"FROM (STUDENT JOIN QUALIFICATION ON " & _

"QUALIFICATION.[ID] = STUDENT.[ID] ) where QUALIFICATION.[SETA] <> '' " & _

" and QUALIFICATION.[SETA] <> 'STUDENT REGISTRATIONS' " & _

"GROUP by QUALIFICATION.[SETA] order by QUALIFICATION.[SETA] "

cmd.Parameters.AddWithValue("@StartDate", StartingDate)

cmd.Parameters.AddWithValue("@EndDate", EndingDate)

cmd.CommandType = CommandType.Text

Table.Load(cmd.ExecuteReader())

UltraGrid1.DataSource = Table

If Table.Rows.Count > 0 Then

btnExcelExport.Enabled = True

End If

If con.State = ConnectionState.Open Then

con.Close()

End If

End Sub

Parents Reply Children