sql分组查询问题
情景一:
表中数据
name score
aaa 11
aaa 19
bbb 12
bbb 18
ccc 19
ddd 21
期望查询结果如下
name score
aaa 30
bbb 30
ccc 19
ddd 21
Sql代码
---检查表是否存在
if exists(select * from sysobjects where name='testSum')
drop table testSum
go
---创建表
create table testSum
(
tid int primary key identity(1,1),
tname varchar(30) null,
tscor int null
)
go
insert into testSum (tname,tscor)
select 'aaa',11
union all
select 'aaa',19
union all
select 'bbb',12
union all
select 'bbb',18
union all
select 'ccc',19
union all
select 'ddd',21
---查询语句
select tname ,sum(tscor) from testSum group by tname
---只查询tscor总和为30的
select tname ,sum(tscor) from testSum group by tname having sum(tscor)=30
---检查表是否存在
if exists(select * from sysobjects where name='testSum')
drop table testSum
go
---创建表
create table testSum
(
tid int primary key identity(1,1),
tname varchar(30) null,
tscor int null
)
go
insert into testSum (tname,tscor)
select 'aaa',11
union all
select 'aaa',19
union
相关文档:
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
--SQL 速查手册
/*******************************************/
SELECT
--用途:从指定表中取出指定列的数据
--语法:
SELECT column_name(s) from table_name
--主要字句可摘要为:
SELECT select_list [INTO new_table]
from table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING s ......
SQL语句优化技术分析
翻译:Jerry [2005-11-11]
原文出处:http://www.51testing.com
原文作者:不详
转载请注明:来自Sawin系统分析之窗
操作符优化
IN 操作符
用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格。
但是用IN的SQL性能总是比较低的,从ORACLE执行的步骤来分析用I ......
sql语言中有没有类似C语言中的switch case的语句??
没有,用case when 来代替就行了.
例如,下面的语句显示中文年月
select getdate() as 日期,case month(get ......
/**
* @author 糊涂鬼
* 在建立连接之前需要一些准备工作:
* 在控制面板上通过“管理工具”的“数据源(ODBC)”打开“ODBC数据源管理器”对话框,
* 单击“系统DSN”选项卡,然后单击“添加”按钮,得到“创建数据源”对话框,
......