使用方式如下:
- select
- case when grouping(Dept)=1 then N'合計' else isnull(StaffNumber,'') end '員工編號',
- case when grouping(StaffNumber)=1 and grouping(Dept)=0 then N'小計' else isnull(Dept,'') end '部門',
- sum(pay) as '年薪'
- from GirdTest group by Dept,StaffNumber with rollup
- private DataTable GetData()
- {
- DataTable dt = new DataTable();
- string Sql = @"
- select
- case when grouping(Dept)=1 then N'合計' else isnull(StaffNumber,'') end '員工編號',
- case when grouping(StaffNumber)=1 and grouping(Dept)=0 then N'小計' else isnull(Dept,'') end '部門',
- sum(pay) as '年薪'
- from GirdTest group by Dept,StaffNumber with rollup";
- using (SqlConnection connection =
- new SqlConnection(connectionString))
- {
- SqlCommand command = new SqlCommand(Sql, connection);
- try
- {
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- dt.Load(reader);
- reader.Close();
- return dt;
- }
- catch (Exception ex)
- {
- //Error logging...
- }
- }
- return dt;
- }
[SQL]以某個欄位群組N筆數做分頁加總
,然後直接使用 CEILING(ROW_NUMBER() OVER (PARTITION BY 群組欄位 ORDER BY 群組欄位) /2.0) + SubQuery來處理就可順利解決。如下,
01 | WITH NOXT1 |
02 | AS |
03 | ( |
04 | SELECT CEILING(row_number() OVER (PARTITION BY NOX ORDER BY NOX) /2.0) AS 'Grouping' , |
05 | * |
06 | FROM dbo.XX AS X |
07 | ) |
08 | SELECT A.NOX, A.NO_ITEM, A.QTY |
09 | , ( SELECT SUM (B.QTY) FROM NOXT1 B WHERE A.NOX = B.NOX AND A. Grouping = B. Grouping ) AS '分頁小計' |
10 | , ( SELECT SUM (B.QTY) FROM NOXT1 B WHERE A.NOX = B.NOX AND A. Grouping >= B. Grouping ) AS '群組累計' |
11 | FROM NOXT1 A; |
[ASP.NET]Gridview 各欄位的加總計算
GridViewRow.RowType 屬性
如何將GridView內的金額欄位,以動態方式加總?
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sumTotal As Integer = 0
For Each rows As GridViewRow In GridView1.Rows
Dim priceTotal As Decimal = 0
priceTotal += Convert.ToDecimal(CType(rows.FindControl("lab_dircharge"), Label).Text.Trim)
sumTotal = sumTotal + CInt(priceTotal.ToString("c0"))
Next
lab_sum.Text = sumTotal
End Sub
Dim sumTotal As Integer = 0
For Each rows As GridViewRow In GridView1.Rows
Dim priceTotal As Decimal = 0
priceTotal += Convert.ToDecimal(CType(rows.FindControl("lab_dircharge"), Label).Text.Trim)
sumTotal = sumTotal + CInt(priceTotal.ToString("c0"))
Next
lab_sum.Text = sumTotal
End Sub
沒有留言:
張貼留言