将datatable linq中数据进行分组,不写SQL

使用SQL进行分组统计查询
使用SQL进行分组统计查询
来源: &&&&excel表格的基本操作教程
视频教程:
视频教程:
视频教程:
视频教程:
视频教程:
视频教程:
在日常工作中,对表格中的数据进行类似于求总和、平均值、最大最小值之类的汇总统计非常普遍。利用Excel分类汇总及査找引用类可以制作这类报表,但操作过程有时很烦琐,在需要从多张数据表提取数据进行汇总统计时,尤其如此.但用SELECT命令非常简单。【例10.27】某学校职工的收入由基本工资、奖金和每月收入等项组成,如图10~38(a)~(c〉所示。现在要统计每个单位的奖金(或基本工资,或月收入总计)总额、平均额、最高额和最低额等数据,如图10-38(d)所示。应用Excel的分类汇总功能,可以快捷方便地制作出如图10-38(d)所示的报表。但是,如果要制作出如图10-28(e)所示的报表,则有一定的难度,过程较为繁杂。采用Vlookup或lookUp、SUM和IF之类的査找引用和求和函数,完成基本工资和奖金统计数据没有太大的问题,但要完成月收入汇总统计的则有一定的难度,因为每个职工有不同的月收入数据,有的有12行,有的有10行,有的有8行,因为每个月一行数据而不同职工的工作时间长短不同。应用Excel的分类汇总功能,可以制作出如图10-38(d)和(e)所示的报表。但采用SELECT命令的分组查询功能更为方便,根据原工作表数据可以快速完成统计,生成需要的报表。当统计的数据来源于多个不同的数据表,效率更高。所谓分组査询,就是指定数据表中的某个字段为分组关键字,分组关键字所在列上的值相同的数据行就是一组,Selecet命令可以对同一组内的数据进行求总和、求平均数、求最大值及最小值等统计运算。例如,在如图10-38U)所示的奖金表中,如果指定单位为分组关键字,则具有相同单位的数据行为一组。因此,外国语学院是一组,教务处是一组,科技处又是另一组,&&SELECT可以对每一组分别统计最高、最低及平均值等数据,使用方法如下:SelectA,统计函数(字段名1&&&统计函数(字段名N)是要进行统计的数据表,可以是一个或多个,如果是多个数据表,则必须通过条件P指定各个数据表连接的关联字段,以及其他数据筛选条件。A是来源于某个数据表的列标题名称,称为分组关键字。在数据表中,列A中值相同的数据则为一组。在SELECT关键字后面,除了分组关键字A之外,出现的所有其他列标題必须使用统计函数进行计算,不允许出现没有使用统计函数的列标题。这里所说的统计函数,在数据库中有更专业的术语,称为聚集函数。可以是sum(总和)、avg(平均数)、max(最大值)、min(最小值)及count(计数)等。【例10.281在例10.27建立的工作表中,利用OLEDBSQL统计査询各部门的奖金总额、职工平均奖金、最高奖金和最低奖金等数据。在例10.27建立的工作簿中,通过OLEDB的&连接属性&对话框,输入下面的select单位,sum(奖金)From[奖金$]Groupby单位总额,_(奖金&as最高,min(奖金&as最低,avg(奖金&执行该査询语句,结果将以单位为分组进行统计计算,结果如图10-39U)所示。语句中的As用于为统计函数计算出的数据列指定一个有意义的别名,如果不指定别名,其含义不一定清楚。例如,avg(奖金)没有指定别名,结果如10-39U)中E列所示。【例10.29】在例10.27建立的工作簿各数据表基础上,统计每个单位的基本工资和奖金的总金额,以及各个部门内各职工这两项收入总计的最高、最低和平均值。在例10.27建立的工作簿中,通过OLEDB的连接属性对话框,输入并执行下面的SELECT语句,将得到如图10-39(b)所示的汇总查询数据表,其中的汇总统计数据是从基本工资和奖金两个数据表统计得到的。select[基本工资$]~单位,sum(奖金+基本工资)as总额,max(奖金+基本工资)as最髙,min(奖金+基本工资)as最低,avg(奖金+基本工资)as平均值Fron[奖金$],[基本工资$]Where[基本工资$].职工编号=[奖金$].职工编号Groupby[基本工资$].单位【例10.30】在例10.27建立的工作簿各数据表基础上,统计出每位职工的月收入汇总数据表,如图10-39(c)所示。实现职工月收入总计的SELECT命令如下:select职工编号,姓名,单位,sum(收入小计)as月收入总计From[每月收入$]Groupby职工编号,姓名,单位Groupby同时使用职工编号、姓名、单位3个列标题,原因是查询报表最后需要显示这些内容,它们需要在SELECT命令的后面出现,但又没有使用聚集函数,因此必须这样写。
相关推荐:
·Excel技巧:
·Excel技巧:
·Excel技巧:
·Excel技巧:
·Excel技巧:
·Excel技巧:
·Excel技巧:
·Excel技巧:
使用SQL进行分组统计查询已有天涯账号?
这里是所提的问题,您需要登录才能参与回答。
"天涯问答"是天涯社区旗下的问题分享平台。在这里您可以提问,回答感兴趣的问题,分享知识和经历,无论您在何时何地上线都可以访问,此平台完全免费,而且注册非常简单。
如何将datatable数据保存到sql seleve去
在C#中创建一个data table表,然后将datatable中的数据要保存到SQL中去,应该如何保存呢??/
08-11-26 &匿名提问 发布
这个是添加的代码 using S using System.Collections.G ponentM using System.D using System.D using System.T using System.Windows.F using System.Data.SqlC namespace 学生成绩管理系统 { public partial class studentluru : Form { // CurrencyManager cmA // SqlConnection sqlConnection1 = new SqlConnection(); public studentluru() { InitializeComponent(); } private void studentluru_Load(object sender, EventArgs e) { } //录入 private void button5_Click(object sender, EventArgs e) { string sno = textBox1.T string sname = textBox2.T string ssex = textBox3.T string sage = textBox4.T string sclass = textBox5.T string sdept = textBox6.T string saddress = textBox7.T string sphone = textBox8.T string sqq = textBox9.T if (textBox1.Text == && || textBox2.Text == && || textBox5.Text == &&) { if (textBox1.Text == &&) { MessageBox.Show(&学号不能为空&, &警告提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox1.Focus();
} if (textBox2.Text == &&) { MessageBox.Show(&姓名不能为空&, &警告提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox2.Focus();
} if (textBox5.Text == &&) { MessageBox.Show(&班级不能为空&, &警告提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox5.Focus();
} } else { string connstr = &server=IT32;uid=pwd='sa';database=&; try { SqlConnection conn = new SqlConnection(connstr); conn.Open(); string sqlinsert = &insert into studentinfo values('& + sno + &','& + sname + &','& + ssex + &','& + sage + &','& + sclass + &','& + sdept + &','& + saddress + &','& + sphone + &','& + sqq + &')&; SqlCommand sc = new SqlCommand(sqlinsert, conn); sc.ExecuteNonQuery(); textBox1.Text = &&; textBox2.Text = &&; textBox3.Text = &&; textBox4.Text = &&; textBox5.Text = &&; textBox6.Text = &&; textBox7.Text = &&; textBox8.Text = &&; textBox9.Text = &&; MessageBox.Show(&数据已经添加成功&, &温馨提示&); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } private void button8_Click(object sender, EventArgs e) { this.Dispose(); } } } 这个是查询界面的代码: using S using System.Collections.G ponentM using System.D using System.D using System.T using System.Windows.F using System.Data.SqlC namespace 学生成绩管理系统 { public partial class studentchaxun : Form { DataTable dt = new DataTable();
CurrencyManager cmA public studentchaxun() { InitializeComponent(); } public studentchaxun(string k, string s) { InitializeComponent(); per = sno = } private void studentchaxun_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。 //this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo); //数据绑定 if (per == &超级用户&) { string sqlsel = &select * from studentinfo &; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContext[dt]; this.dataGridView1.DataSource = boBox1.DataSource = boBox1.DisplayMember = &sno&; boBox2.DataSource = boBox2.DisplayMember = &sname&; } if (per == &普通用户&) { string sqlsel = &select * from studentinfo where sno='& + sno + &' &; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContext[dt]; this.dataGridView1.DataSource = boBox1.DataSource = boBox1.DisplayMember = &sno&; boBox2.Enabled = this.radioButton2.Enabled = //this.toolStripButton1.Enabled = //this.toolStripButton2.Enabled = //this.toolStripButton3.Enabled = //this.toolStripButton4.Enabled = //boBox2.DataSource = //boBox2.DisplayMember = &sname&; } } DataTable bangding(string sqlsel) { string connstr = &server=.;uid=pwd=database=sc&; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); da.Fill(ds, &coust&); DataTable dt = ds.Tables[&coust&]; conn.Close();
} } private void button1_Click(object sender, EventArgs e) { if (this.radioButton1.Checked == true) { string xuehao = comboBox1.T string sqlsel = &select * from studentinfo where sno='& + xuehao + &'&; this.dataGridView1.DataSource = bangding(sqlsel); } else { string xingming = comboBox2.T string sqlsel = &select * from studentinfo where sname='& + xingming + &'&; this.dataGridView1.DataSource = bangding(sqlsel); } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (this.radioButton1.Checked == true) { boBox1.Enabled = boBox2.Enabled = } else { boBox1.Enabled = boBox2.Enabled = } } private void checkstate(int pos) { if (pos == 0) { toolStripButton1.Enabled = toolStripButton2.Enabled = toolStripButton3.Enabled = toolStripButton4.Enabled = } else { toolStripButton1.Enabled = toolStripButton2.Enabled = toolStripButton3.Enabled = toolStripButton4.Enabled = } } //首记录 private void toolStripButton1_Click(object sender, EventArgs e) { cmAmend.Position = 0;//设置为0.回到首记录 this.dataGridView1.Select(); checkstate(cmAmend.Position); } //上一条 private void toolStripButton2_Click(object sender, EventArgs e) { cmAmend.Position--; this.dataGridView1.Select(); checkstate(cmAmend.Position); } //下一条 private void toolStripButton3_Click(object sender, EventArgs e) { cmAmend.Position++; this.dataGridView1.Select(); checkstate(cmAmend.Position); } //末记录 private void toolStripButton4_Click(object sender, EventArgs e) { cmAmend.Position = cmAmend.Count - 1; this.dataGridView1.Select(); checkstate(cmAmend.Position); } } } 删除的代码: using S using System.Collections.G ponentM using System.D using System.D using System.T using System.Windows.F using System.Data.SqlC namespace 学生成绩管理系统 { public partial class studentshanchu : Form { CurrencyManager cmA public studentshanchu() { InitializeComponent(); } private void studentshanchu_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。 //this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo); //数据绑定 string sqlsel = &select * from studentinfo &; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContext[dt]; this.dataGridView1.DataSource = this.textBox1.DataBindings.Add(&text&, dt, &sno&); this.textBox2.DataBindings.Add(&text&, dt, &sname&); this.textBox3.DataBindings.Add(&text&, dt, &ssex&); this.textBox4.DataBindings.Add(&text&, dt, &sage&); this.textBox5.DataBindings.Add(&text&, dt, &sclass&); this.textBox6.DataBindings.Add(&text&, dt, &sdept&); this.textBox7.DataBindings.Add(&text&, dt, &saddress&); this.textBox8.DataBindings.Add(&text&, dt, &sphone&); this.textBox9.DataBindings.Add(&text&, dt, &sqq&); } DataTable bangding(string sqlsel) { string connstr = &server=.;uid=pwd=database=sc&; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); da.Fill(ds, &coust&); DataTable dt = ds.Tables[&coust&]; conn.Close();
} } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) { this.Height = 450; } else { this.Height = 250; } } private void button1_Click(object sender, EventArgs e) { if (MessageBox.Show(&你确定要删除该记录吗&, &询问&, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { int pos = this.dataGridView1.CurrentCell.RowI //获取该行 string connstr = &server=.;uid=pwd=database=sc&; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); DataSet ds = new DataSet(); string sqlset = &select * from studentinfo&; //数据集 SqlDataAdapter da = new SqlDataAdapter(sqlset, conn); da.Fill(ds, &coust&); DataTable dt = ds.Tables[&coust&]; SqlCommandBuilder cb = new SqlCommandBuilder(da); dt.Rows[pos].Delete(); da.Update(ds, &coust&); textBox1.Text = &&; textBox2.Text = &&; textBox3.Text = &&; textBox4.Text = &&; textBox5.Text = &&; textBox6.Text = &&; textBox7.Text = &&; textBox8.Text = &&; textBox9.Text = &&; this.dataGridView1.DataSource = bangding(sqlset); MessageBox.Show(&恭喜你已成功删除&,&温馨提示&); conn.Close(); } } } } } 修改的代码: using S using System.Collections.G ponentM using System.D using System.D using System.T using System.Windows.F using System.Data.SqlC namespace 学生成绩管理系统 { public partial class studentxiugai : Form { string connstr = &server=.;uid=pwd=database=sc&;
CurrencyManager cmA public studentxiugai() { InitializeComponent(); } public studentxiugai(string k,string s) { InitializeComponent(); per = sno = } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) { //this.Height = 450; comboBox1.Enabled = textBox1.Enabled = } else { //this.Height = 250; comboBox1.Enabled = textBox1.Enabled = } } private void studentxiugai_Load(object sender, EventArgs e) { //数据绑定 if (per == &超级用户&) { string sqlsel = &select * from studentinfo order by sno asc &; DataTable dt = bangding(sqlsel); cmAmend = (CurrencyManager)BindingContext[dt]; boBox1.DataSource = boBox1.DisplayMember = &sno&; this.textBox1.DataBindings.Add(&text&, dt, &sname&); this.textBox2.DataBindings.Add(&text&, dt, &ssex&); this.textBox3.DataBindings.Add(&text&, dt, &sage&); this.textBox4.DataBindings.Add(&text&, dt, &sclass&); this.textBox5.DataBindings.Add(&text&, dt, &sdept&); this.textBox6.DataBindings.Add(&text&, dt, &saddress&); this.textBox7.DataBindings.Add(&text&, dt, &sphone&); this.textBox8.DataBindings.Add(&text&, dt, &sqq&); } if (per == &普通用户&) { string sqlsel = &select *from studentinfo where sno='& + sno + &'&; DataTable dt = bangding(sqlsel); boBox1.DataSource = boBox1.DisplayMember = &sno&; this.textBox1.DataBindings.Add(&text&, dt, &sname&); this.textBox2.DataBindings.Add(&text&, dt, &ssex&); this.textBox3.DataBindings.Add(&text&, dt, &sage&); this.textBox4.DataBindings.Add(&text&, dt, &sclass&); this.textBox5.DataBindings.Add(&text&, dt, &sdept&); this.textBox6.DataBindings.Add(&text&, dt, &saddress&); this.textBox7.DataBindings.Add(&text&, dt, &sphone&); this.textBox8.DataBindings.Add(&text&, dt, &sqq&); } } DataTable bangding(string sqlsel) { // string connstr = &server=.;uid=pwd=database=sc&; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn); da.Fill(ds, &coust&); DataTable dt = ds.Tables[&coust&]; conn.Close();
} } private void button1_Click(object sender, EventArgs e) { if (checkBox1.Checked == true) { string sno = comboBox1.T string sname = textBox1.T string sex = textBox2.T string age = textBox3.T string sclass = textBox4.T string sdept = textBox5.T string saddress = textBox6.T string sphone = textBox7.T string sqq = textBox8.T if (textBox2.Text == && || textBox3.Text == && || textBox4.Text == && || textBox5.Text == &&) { if (textBox2.Text == &&) { MessageBox.Show(&性别不能为空&, &温馨提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox2.Focus();
} if (textBox3.Text == &&) { MessageBox.Show(&年龄不能为空&, &温馨提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox3.Focus();
} if (textBox4.Text == &&) { MessageBox.Show(&班级不能为空&, &温馨提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox4.Focus();
} if (textBox5.Text == &&) { MessageBox.Show(&系别不能为空&, &温馨提示&, MessageBoxButtons.OKCancel, MessageBoxIcon.Error); textBox5.Focus();
} } else { string connstr = &server=IT32;uid=pwd='sa';database=&; try { SqlConnection conn = new SqlConnection(connstr); conn.Open(); string sqlupdate = & update studentinfo set ssex='& + sex + &',sage='& + age + &',sclass='& + sclass + &',sdept='& + sdept + &',saddress='& + saddress + &',sphone='& + sphone + &',sqq='& + sqq + &' where sno='& + sno + &'&; SqlCommand sc = new SqlCommand(sqlupdate, conn); sc.ExecuteNonQuery(); MessageBox.Show(&数据已经修改成功&, &温馨提示&); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } else { MessageBox.Show(&对不起,你还没有选中'我要修改',所以您无法修改&,&温馨提示&); } } } } 更新的代码太简单了,只需要重新在数据库里面查询一次并把它取出来就行了,这里就不写了,累死我了,这次应该看懂了吧?
请登录后再发表评论!Datagridview中默认以绑定表字段作为标题,怎么更改列标题_DataTable分组的有关问题,求教各位,怎样将分组後的数据按组取出_sql数据库能直接和程序一块传到服务器上么?该怎么处理__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
Datagridview中默认以绑定表字段作为标题,怎么更改列标题
DataTable分组的有关问题,求教各位,怎样将分组後的数据按组取出
sql数据库能直接和程序一块传到服务器上么?该怎么处理
Datagridview中默认以绑定表字段作为标题,怎么更改列标题
Datagridview中默认以绑定表字段作为标题,如何更改列标题?谢谢!------解决方案--------------------this.Column1.HeaderText =
"重命列名 ";
------解决方案--------------------this.dataView.Columns[0].HeaderText =
------解决方案--------------------怎么都用代码啊?直接在编辑列那里更改列标题不是更方便。
------解决方案--------------------不用代码,就用databindingsource帮定数据集,然后datagridview帮定databindingsource.这样旧能编辑列了,可以增加 删除 修改.
DataTable分组的有关问题,求教各位,怎样将分组後的数据按组取出
DataTable分组的问题,求教各位大虾,怎样将分组後的数据按组取出?如题!------解决方案--------------------up
sql数据库能直接和程序一块传到服务器上么?该怎么处理
sql数据库能直接和程序一块传到服务器上么?如题
没有数据库空间
能不能直接和程序一块上传??------解决方案--------------------sqlService要收费的。
------解决方案--------------------这问题让人纠结.. google 查查资料
------解决方案--------------------数据量不大可以用SQLite,部署比较方便
------解决方案--------------------SQLite,不错效率
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:

我要回帖

更多关于 datatable linq 的文章

 

随机推荐