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
相关文档:
系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......
sql语言中有没有类似C语言中的switch case的语句??
没有,用case when 来代替就行了.
例如,下面的语句显示中文年月
select getdate() as 日期,case month(get ......
SQL中文问排序
首先把这些信息在editplus中,转换为一列,拷贝到excel中,拷贝后记着在第一行前插入一行,作为列标头,例如为name,然后在sql中表中,导入数据,生成一个新表,例如fenlei,打开sql语句行,执行:
SELECT *
from Fenlei
ORDER BY name COLLATE Chinese_PRC_CS_AS_KS_WS
......
/**
* @author 糊涂鬼
* 在建立连接之前需要一些准备工作:
* 在控制面板上通过“管理工具”的“数据源(ODBC)”打开“ODBC数据源管理器”对话框,
* 单击“系统DSN”选项卡,然后单击“添加”按钮,得到“创建数据源”对话框,
......
连接sql server数据库用到的命名空间是using System.Data.SqlClient;
数据库连接代码:可以在大类之下声明 public SqlConnection myConnection; //sql连接对象
&nbs ......