group by的SQL语句
举例1
有一张项目表
CREATE TABLE [ProjectTable] (
[ProjectID] NVARCHAR(16) NOT NULL,
[ProjectName] NVARCHAR(20) NOT NULL ON CONFLICT REPLACE DEFAULT Null,
[DeviceID] INTEGER(16) NOT NULL ON CONFLICT FAIL,
[Description] NVARCHAR(255) NOT NULL ON CONFLICT REPLACE DEFAULT 工程描述);
使用一下的sql语句
select
distinct
a.[ProjectID],
a.[ProjectName]
from
projecttable as a
得出的结果为
其实想要的数据,只是不同的ProjectID所对应的ProjectName
修改sql语句为
select
distinct
a.[ProjectID],
a.[ProjectName]
from
projecttable as a
group by a.[ProjectID]
举例2
https://stackoverflow.com/questions/13998552/why-do-we-need-group-by-with-aggregate-functions
假如现在有一张points detail表,3个字段,user id,company id,points
CREATE TABLE PointsDetail
(
UserId INT NOT NULL ,
CompanyId INT NOT NULL ,
Points INT NOT NULL
);
INSERT INTO dbo.PointsDetail ( UserId ,
CompanyId ,
Points
)
VALUES ( 1 , -- UserId - int
1 , -- CompanyId - int
6 -- Points - int
);
INSERT INTO dbo.PointsDetail ( UserId ,
CompanyId ,
Points
)
VALUES ( 2 , -- UserId - int
1 , -- CompanyId - int
7 -- Points - int
); INSERT INTO dbo.PointsDetail ( UserId ,
CompanyId ,
Points
)
VALUES ( 1 , -- UserId - int
2 , -- CompanyId - int
4 -- Points - int
); INSERT INTO dbo.PointsDetail ( UserId ,
CompanyId ,
Points
)
VALUES ( 2 , -- UserId - int
2 , -- CompanyId - int
3 -- Points - int
);
获取所有数据
SELECT *
FROM PointsDetail;
按照用户group汇总积分,得到每个用户的积分总和
SELECT UserId ,
SUM(Points) AS TotalPointsForUser
FROM PointsDetail
GROUP BY UserId;
按照公司group汇总积分,得到每个公司的积分总和
SELECT CompanyId ,
SUM(Points) AS TotalPointsForCompany
FROM PointsDetail
GROUP BY CompanyId;
举例3 特殊情况
BeneficiaryAccountID 由BeneficiaryId和CompanyId决定,所以下面的2个sql语句执行的points列是一样的
SELECT BeneficiaryAccountID ,
SUM(Points) AS TotalPoints
FROM dbo.tbm_cti_CustomTableItem_BeneficiaryPointDetail
WHERE PointsType = 1
GROUP BY BeneficiaryAccountID
ORDER BY TotalPoints; SELECT BeneficiaryID ,
CompanyID ,
SUM(Points) AS TotalPoints
FROM dbo.tbm_cti_CustomTableItem_BeneficiaryPointDetail
WHERE PointsType = 1
GROUP BY BeneficiaryID ,
CompanyID
ORDER BY TotalPoints;
group by的SQL语句的更多相关文章
- Oracle行转列、列转行的Sql语句总结
多行转字符串 这个比较简单,用||或concat函数可以实现 SQL Code 12 select concat(id,username) str from app_userselect i ...
- Oracle行转列、列转行的Sql语句总结(转)
多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...
- Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...
- LINQ to SQL语句(6)之Group By/Having
适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in db.Products group ...
- sql语句Group By用法-转载
sql语句Group By用法一则 2007-10-25 12:00 sql语句Group By用法一则 如果我们的需求变成是要算出每一间店 (store_name) 的营业额 (sales),那怎么 ...
- oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by
select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...
- [转]SQL语句:Group By总结
1. Group By 语句简介: Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若 ...
- (转载)SQL语句中Group by语句的详细介绍
转自:http://blog.163.com/yuer_d/blog/static/76761152201010203719835 SQL语句中Group by语句的详细介绍 ...
- SQL语句:Group By总结
1. Group By 语句简介: Group By语句从英文的字面意义上理解就是"根据(by)一定的规则进行分组(Group)".它的作用是通过一定的规则将一个数据集划分成若干个 ...
随机推荐
- ASP.NET操作WMI
WMI Functions from ASP.NET Introduction This article demonstrates how to use WMI in ASP.NET to cre ...
- iOS7之定制View Controller切换效果
在iOS5和iOS6前,View Controller的切换主要有4种: 1. Push/Pop,NavigationViewController常干的事儿 2. Tab,TabViewControl ...
- 短小强悍的JavaScript异步调用库
对于博文 20行完成一个JavaScript模板引擎 的备受好评我感到很惊讶,并决定用此文章介绍使用我经常使用的另一个小巧实用的工具.我们知道,在浏览器中的 JavaScript 绝大部分的操作都是异 ...
- 微软职位内部推荐-Senior Android Developer
微软近期Open的职位: Position: Senior SDE-- Mobile Products Android/iOS/WP Senior Developer Contact Person: ...
- getHeight returns 0 for all Android UI objects
It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get ar ...
- python学习小结9:面向对象
面向对象技术 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类中且 ...
- Qt websocket协议的实现
handshake(握手) client请求: GET /chat HTTP/1.1 Host: server.example.com Upgrade: ...
- Awesome (and Free) Data Science Books[转]
Post Date: September 3, 2014By: Stephanie Miller Marty Rose, Data Scientist in the Acxiom Product an ...
- 2186: [Sdoi2008]沙拉公主的困惑 - BZOJ
Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...
- Uyuw's Concert POJ2451
裸半平面交,以前没写过,先写一遍再说 我越来越不注意细节了,最后才发现空间稍微开小了(没有开那个零头,他又要多4条边,就WA了) const maxn=; eps=1e-7; type point=r ...