博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习 SQL 语句 - Select(7): 分组统计之 Avg()、Sum()、Max()、Min()、Count()
阅读量:5793 次
发布时间:2019-06-18

本文共 4336 字,大约阅读时间需要 14 分钟。

  hot3.png

Group By
本来 Group By 也可以根据多个字段分组("," 隔开), 但这个例子只有 Continent 字段适合分组,
也只有 Area、Population 字段适合统计.

本例效果图:
26153408_cNb8.png

代码文件:

unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB;type  TForm1 = class(TForm)    DBGrid1: TDBGrid;    DataSource1: TDataSource;    ADODataSet1: TADODataSet;    Panel1: TPanel;    Button1: TButton;    Button2: TButton;    Button3: TButton;    Button4: TButton;    Button5: TButton;    procedure FormCreate(Sender: TObject);    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button4Click(Sender: TObject);    procedure Button5Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin  with ADODataSet1 do begin    Close;    CommandText := 'SELECT Continent, ' +                   'AVG(Area) AS 平均面积, AVG(Population) AS 平均人口 ' +                   'FROM country GROUP BY Continent';    Open;  end;end;procedure TForm1.Button2Click(Sender: TObject);begin  with ADODataSet1 do begin    Close;    CommandText := 'SELECT Continent, ' +                   'SUM(Area) AS 面积总数, SUM(Population) AS 人口总数 ' +                   'FROM country GROUP BY Continent';    Open;  end;end;procedure TForm1.Button3Click(Sender: TObject);begin  with ADODataSet1 do begin    Close;    CommandText := 'SELECT Continent, ' +                   'MAX(Area) AS 面积最大, MIN(Population) AS 人口最少 ' +                   'FROM country GROUP BY Continent';    Open;  end;end;procedure TForm1.Button4Click(Sender: TObject);begin  with ADODataSet1 do begin    Close;    CommandText := 'SELECT Continent, ' +                   'MAX(Area + Population) AS 面积与人口总和最多, ' +                   'MIN(Area + Population) AS 面积与人口总和最少 ' +                   'FROM country GROUP BY Continent';    Open;  end;end;procedure TForm1.Button5Click(Sender: TObject);begin  with ADODataSet1 do begin    Close;    CommandText := 'SELECT COUNT(*) AS 记录总数 FROM country';    Open;  end;end;procedure TForm1.FormCreate(Sender: TObject);var  mdbFile: string;begin  mdbFile := GetEnvironmentVariable('COMMONPROGRAMFILES');  mdbFile := mdbFile + '\CodeGear Shared\Data\dbdemos.mdb';  ADODataSet1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +    mdbFile + ';Persist Security Info=False';  DBGrid1.DataSource := DataSource1;  DataSource1.DataSet := ADODataSet1;end;end.
窗体文件:

object Form1: TForm1  Left = 0  Top = 0  Caption = 'Form1'  ClientHeight = 407  ClientWidth = 626  Color = clBtnFace  Font.Charset = DEFAULT_CHARSET  Font.Color = clWindowText  Font.Height = -11  Font.Name = 'Tahoma'  Font.Style = []  OldCreateOrder = False  OnCreate = FormCreate  PixelsPerInch = 96  TextHeight = 13  object DBGrid1: TDBGrid    Left = 0    Top = 33    Width = 626    Height = 374    Align = alClient    DataSource = DataSource1    TabOrder = 0    TitleFont.Charset = DEFAULT_CHARSET    TitleFont.Color = clWindowText    TitleFont.Height = -11    TitleFont.Name = 'Tahoma'    TitleFont.Style = []  end  object Panel1: TPanel    Left = 0    Top = 0    Width = 626    Height = 33    Align = alTop    Caption = 'Panel1'    TabOrder = 1    object Button1: TButton      Left = 6      Top = 5      Width = 75      Height = 25      Caption = 'Button1'      TabOrder = 0      OnClick = Button1Click    end    object Button2: TButton      Left = 87      Top = 5      Width = 75      Height = 25      Caption = 'Button2'      TabOrder = 1      OnClick = Button2Click    end    object Button3: TButton      Left = 168      Top = 5      Width = 75      Height = 25      Caption = 'Button3'      TabOrder = 2      OnClick = Button3Click    end    object Button4: TButton      Left = 249      Top = 5      Width = 75      Height = 25      Caption = 'Button4'      TabOrder = 3      OnClick = Button4Click    end    object Button5: TButton      Left = 330      Top = 5      Width = 75      Height = 25      Caption = 'Button5'      TabOrder = 4      OnClick = Button5Click    end  end  object DataSource1: TDataSource    DataSet = ADODataSet1    Left = 184    Top = 112  end  object ADODataSet1: TADODataSet    CursorType = ctStatic    Parameters = <>    Left = 232    Top = 184  endend

转载于:https://my.oschina.net/hermer/blog/319883

你可能感兴趣的文章
xss
查看>>
iOS:百度长语音识别具体的封装:识别、播放、进度刷新
查看>>
JS获取服务器时间并且计算距离当前指定时间差的函数
查看>>
华为硬件工程师笔试题
查看>>
jquery居中窗口-页面加载直接居中
查看>>
cd及目录快速切换
查看>>
Unity Shaders and Effects Cookbook (3-5) 金属软高光
查看>>
31-hadoop-hbase-mapreduce操作hbase
查看>>
C++ 代码风格准则:POD
查看>>
linux-友好显示文件大小
查看>>
【转】【WPF】WPF中MeasureOverride ArrangeOverride 的理解
查看>>
【转】二叉树的非递归遍历
查看>>
NYOJ283对称排序
查看>>
接连遇到大牛
查看>>
[Cocos2d-x For WP8]矩形碰撞检测
查看>>
自己写spring boot starter
查看>>
花钱删不完负面消息
查看>>
JBPM之JPdl小叙
查看>>
Membership三步曲之进阶篇 - 深入剖析Provider Model
查看>>
前端优化及相关要点总结
查看>>