现有表
create table newsale
(city varchar(4),
date datetime,
money money,
valid char(1)
)
其中表valid用字符T和F来区分有效和无效交易现要求设计一个查询取出每年的每个月发生的有效交易和无效交易的笔数和总额,要求格式如下年/月 有效交易统计,无效交易统计,有效交易笔数,无效交易笔数
2005/09 1282.51 0 4 0
.
.
.
.
.
.我写的查询结构如下,
select convert(char(4),datepart(yy,date))+'/'+convert(char(2),datepart(mm,date)) as [年/月],
sum(case valid when 'T' then money else 0 end) as 有效消费统计,
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by date
但确显示出2005/9 128.916 0.00 1 0
2005/9 270.4423 0.00 1 0
2005/9 658.8989 0.00 1 0
2005/9 224.2761 0.00 1 0
.
.
.
不知这该如何处理
create table newsale
(city varchar(4),
date datetime,
money money,
valid char(1)
)
其中表valid用字符T和F来区分有效和无效交易现要求设计一个查询取出每年的每个月发生的有效交易和无效交易的笔数和总额,要求格式如下年/月 有效交易统计,无效交易统计,有效交易笔数,无效交易笔数
2005/09 1282.51 0 4 0
.
.
.
.
.
.我写的查询结构如下,
select convert(char(4),datepart(yy,date))+'/'+convert(char(2),datepart(mm,date)) as [年/月],
sum(case valid when 'T' then money else 0 end) as 有效消费统计,
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by date
但确显示出2005/9 128.916 0.00 1 0
2005/9 270.4423 0.00 1 0
2005/9 658.8989 0.00 1 0
2005/9 224.2761 0.00 1 0
.
.
.
不知这该如何处理
解决方案 »
- sqlserver通讯口问题
- 一个物理表的建立
- 请教一个排序问题!
- sql查询时能否查询多个栏目的数据?
- 指點一下:合理添加表與表之間的約束關系
- 请问set nocount on及set nocount off有什么作用?
- [馨郁星愿]如何根据某个字段的数据(日期类型),把它转换成该数据所对应的星期几??在SQL里怎么写这语句?
- 主题:在WIN2000下安装D版SQL SERVER 7.0的成功标准是什么?
- 创建表提示找不到字段名,在线等
- The OLE DB provider "Microsoft.ACE.OLEDB.12.0" has not been registered.
- GROUP BY 字句如何简单SELECT 其他非聚合函数字段?
- 2个select语句 怎么inner join
sum(case valid when 'T' then money else 0 end) as 有效消费统计,
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by convert(char(4),datepart(yy,date))+'/'+convert(char(2),datepart(mm,date)) as [年/月]
sum(case valid when 'T' then money else 0 end) as 有效消费统计,
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by convert(char(7),getdate(),111)
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by convert(varchar(7),date,111)
sum(case valid when 'T' then money else 0 end) as 有效消费统计,
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by convert(char(7),date,111)
sum(case valid when 'T' then money else 0 end) as 有效消费统计,
sum(case valid when 'F' then money else 0 end) as 无效消费统计,
sum(case valid when 'T' then 1 else 0 end) as 有效消费笔数,
sum(case valid when 'F' then 1 else 0 end) as 无效消费笔数
from newsale
group by convert(char(4),datepart(yy,date))+'/'+convert(char(2),datepart(mm,date))