SELECT function(列) FROM 表
AVG 函数返回数值列的平均值。NULL 值不包括在计算中
SELECT AVG(column_name) FROM table_name
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT AVG(OrderPrice) AS OrderAverage FROM Orders
结果集:
| orderAverage |
|---|
| 950 |
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
结果集:
| Customer |
|---|
| Bush |
| Carter |
| Adams |
COUNT() 函数返回匹配指定条件的行数
SELECT COUNT(column_name) FROM table_name
SELECT COUNT(*) FROM table_name
SELECT COUNT(DISTINCT column_name) FROM table_name
注释:COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server,但是无法用于 Microsoft Access
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Carter'
| CustomerNilsen |
|---|
| 2 |
SELECT COUNT(*) AS NumberOfOrders FROM Orders
| NumberofOrders |
|---|
| 6 |
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
| NumberOfCustomers |
|---|
| 3 |
FIRST() 函数返回指定的字段中第一个记录的值,且可使用 ORDER BY 语句对记录进行排序
SELECT FIRST(column_name) FROM table_name
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders
结果集:
| FirstOrderPrice |
|---|
| 1000 |
LAST() 函数返回指定的字段中最后一个记录的值,且可使用 ORDER BY 语句对记录进行排序。
SELECT LAST(column_name) FROM table_name
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders
结果集:
| LastOrderPrice |
|---|
| 100 |
MAX 函数返回一列中的最大值。NULL 值不包括在计算中
SELECT MAX(column_name) FROM table_name
注释:MIN 和 MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
结果集:
| LargestOrderPrice |
|---|
| 2000 |
MIN 函数返回一列中的最小值。NULL 值不包括在计算中
SELECT MIN(column_name) FROM table_name
注释:MIN 和 MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
结果集:
| SmallestOrderPrice |
|---|
| 100 |
SUM 函数返回数值列的总数(总额)
SELECT SUM(column_name) FROM table_name
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
结果集:
| OrderTotal |
|---|
| 5700 |
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
结果集:
| Customer | SUM(OrderPrice) |
|---|---|
| Bush | 2000 |
| Carter | 1700 |
| Adams | 2000 |
SELECT Customer,SUM(OrderPrice) FROM Orders
结果集会是:
| Customer | SUM(OrderPrice) |
|---|---|
| Bush | 5700 |
| Carter | 5700 |
| Bush | 5700 |
| Bush | 5700 |
| Adams | 5700 |
| Carter | 5700 |
上面的 SELECT 语句指定了两列(Customer 和 SUM(OrderPrice))。"SUM(OrderPrice)" 返回一个单独的值("OrderPrice" 列的总计),而 "Customer" 返回 6 个值(每个值对应 "Orders" 表中的每一行)。因此,我们得不到正确的结果。不过,您已经看到了,GROUP BY 语句解决了这个问题
SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 1 | 2008/12/29 | 1000 | Bush |
| 2 | 2008/11/23 | 1600 | Carter |
| 3 | 2008/10/05 | 700 | Bush |
| 4 | 2008/09/28 | 300 | Bush |
| 5 | 2008/08/06 | 2000 | Adams |
| 6 | 2008/07/21 | 100 | Carter |
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
结果集:
| Customer | SUM(orderPrice) |
|---|---|
| Carter | 1700 |
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
结果集:
| Customer | SUM(OrderPrice) |
|---|---|
| Bush | 2000 |
| Adams | 2000 |
UCASE 函数把字段的值转换为大写
SELECT UCASE(column_name) FROM table_name
| Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Adams | John | oxford Street | London |
| 2 | Bush | George | Fifth Avenue | New York |
| 3 | Carter | Thomas | Changan Street | Beijing |
SELECT UCASE(LastName) as LastName,FirstName FROM Persons
结果集:
| LastName | FirstName |
|---|---|
| ADAMS | John |
| BUSH | George |
| CARTER | Thomas |
LCASE 函数把字段的值转换为小写
SELECT LCASE(column_name) FROM table_name
| Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Adams | John | oxford Street | London |
| 2 | Bush | George | Fifth Avenue | New York |
| 3 | Carter | Thomas | Changan Street | Beijing |
SELECT LCASE(LastName) as LastName,FirstName FROM Persons
结果集:
| LastName | FirstName |
|---|---|
| adams | John |
| bush | George |
| carter | Thomas |
MID 函数用于从文本字段中提取字符
SELECT MID(column_name,start[,length]) FROM table_name
| 参数 | 描述 |
|---|---|
| column_name | 必须,要提取字符的字段 |
| start | 必须,规定开始位置(起始值是1) |
| length | 可选,要返回的字符数,如果省略,则MID()函数返回剩余文本 |
| Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Adams | John | oxford Street | London |
| 2 | Bush | George | Fifth Avenue | New York |
| 3 | Carter | Thomas | Changan Street | Beijing |
SELECT MID(City,1,3) as SmallCity FROM Persons
结果集:
| SmallCity |
|---|
| Lon |
| New |
| Bei |
LEN 函数返回文本字段中值的长度
SELECT LEN(column_name) FROM table_name
| Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Adams | John | oxford Street | London |
| 2 | Bush | George | Fifth Avenue | New York |
| 3 | Carter | Thomas | Changan Street | Beijing |
SELECT LEN(City) as LengthOfCity FROM Persons
结果集:
| lengthOfCity |
|---|
| 6 |
| 8 |
| 7 |
ROUND 函数用于把数值字段舍入为指定的小数位数
SELECT ROUND(column_name,decimals) FROM table_name
| 参数 | 描述 |
|---|---|
| column_name | 必须,要舍入的字段 |
| decimals | 必须,规定要返回的小数位数 |
| Prod_Id | ProductName | Unit | UnitPrice |
|---|---|---|---|
| 1 | gold | 1000 g | 32.35 |
| 2 | silver | 1000 g | 11.56 |
| 3 | copper | 1000 g | 6.85 |
SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products
结果集:
| ProductName | UnitPrice |
|---|---|
| gold | 32 |
| silver | 12 |
| copper | 7 |
NOW 函数返回当前的日期和时间。提示:如果您在使用 Sql Server 数据库,请使用 getdate() 函数来获得当前的日期时间
SELECT NOW() FROM table_name
| Prod_Id | ProductName | Unit | UnitPrice |
|---|---|---|---|
| 1 | gold | 1000 g | 32.35 |
| 2 | silver | 1000 g | 11.56 |
| 3 | copper | 1000 g | 6.85 |
SELECT ProductName, UnitPrice, Now() as PerDate FROM Products
结果集:
| ProductName | UnitPrice | PerDate |
|---|---|---|
| gold | 32.35 | 12/29/2008 11:36:00 AM |
| silver | 11.56 | 12/29/2008 11:36:00 AM |
| copper | 6.85 | 12/29/2008 11:36:00 AM |
FORMAT 函数用于对字段的显示进行格式化
SELECT FORMAT(column_name,format) FROM table_name
| 参数 | 描述 |
|---|---|
| column_name | 必须,要格式化的字段 |
| format | 必须,规定格式 |
| Prod_Id | ProductName | Unit | UnitPrice |
|---|---|---|---|
| 1 | gold | 1000 g | 32.35 |
| 2 | silver | 1000 g | 11.56 |
| 3 | copper | 1000 g | 6.85 |
SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate
FROM Products
结果集:
| ProductName | UnitPrice | PerDate |
|---|---|---|
| gold | 32.35 | 12/29/2008 |
| silver | 11.56 | 12/29/2008 |
| copper | 6.85 | 12/29/2008 |
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
目录第1题连续问题分析:解法:第2题分组问题分析:解法:第3题间隔连续问题分析:解法:第4题打折日期交叉问题分析:解法:第5题同时在线问题分析:解法:第1题连续问题如下数据为蚂蚁森林中用户领取的减少碳排放量iddtlowcarbon10012021-12-1212310022021-12-124510012021-12-134310012021-12-134510012021-12-132310022021-12-144510012021-12-1423010022021-12-154510012021-12-1523.......找出连续3天及以上减少碳排放量在100以上的用户分析:遇到这类
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我需要一个通过输入字符串进行计算的方法,像这样function="(a/b)*100"a=25b=50function.something>>50有什么方法吗? 最佳答案 您可以使用instance_eval:function="(a/b)*100"a=25.0b=50instance_evalfunction#=>50.0请注意,使用eval本质上是不安全的,尤其是当您使用外部输入时,因为它可能包含注入(inject)的恶意代码。另请注意,a设置为25.0而不是25,因为如果它是整数a/b将导致0(整数)。
我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法
在这段Ruby代码中:ModuleMClassC当我尝试运行时出现“'M:Module'的未定义方法'helper'”错误c=M::C.new("world")c.work但直接从另一个类调用M::helper("world")工作正常。类不能调用在定义它们的同一模块中定义的模块函数吗?除了将类移出模块外,还有其他解决方法吗? 最佳答案 为了调用M::helper,你需要将它定义为defself.helper;结束为了进行比较,请查看以下修改后的代码段中的helper和helper2moduleMclassC