求高手帮我解决一个SQL server语句的问题!!!
时间:2011-12-20
来源:互联网
我如果想查询本月的所有支出,在SQL server中用
select SUM(price) as sumpayM from pay where DATEPART(MM,time)
=DATEPART(MM,GETDATE()) and DATEPART(YY,time)
=DATEPART(YY,GETDATE()) and userid=1;就可了。
但如果我想查询本月的每天所有支出,应该怎么写啊!
我有这么一段代码,不过是oracle数据库的语句:
//查询本月支出情况
public List getPayByM(Connection con,Long userId)throws SQLException{
Statement st = con.createStatement();
int s = this.GetDay(con);
List list = new ArrayList();
String a ="";
for(int i=0;i<s;i++){
int z = i+1;
if(i==(s-1)){
a+="sum(decode(extract(day from b.times),"+(i+1)+",b.price,0)) as a"+(i+1);
}else{
a+="sum(decode(extract(day from b.times),"+(i+1)+",b.price,0)) as a"+(i+1)+" , ";
}
}
String sql ="SELECT "+a+" FROM pay b where b.userid="+userId+" and extract(month from b.times)=07";
ResultSet rs1 = st.executeQuery(sql);
if(rs1.next()){
for(int i=0;i<s;i++){
list.add(rs1.getInt("a"+(i+1)));
}
}
return list;
}
select SUM(price) as sumpayM from pay where DATEPART(MM,time)
=DATEPART(MM,GETDATE()) and DATEPART(YY,time)
=DATEPART(YY,GETDATE()) and userid=1;就可了。
但如果我想查询本月的每天所有支出,应该怎么写啊!
我有这么一段代码,不过是oracle数据库的语句:
//查询本月支出情况
public List getPayByM(Connection con,Long userId)throws SQLException{
Statement st = con.createStatement();
int s = this.GetDay(con);
List list = new ArrayList();
String a ="";
for(int i=0;i<s;i++){
int z = i+1;
if(i==(s-1)){
a+="sum(decode(extract(day from b.times),"+(i+1)+",b.price,0)) as a"+(i+1);
}else{
a+="sum(decode(extract(day from b.times),"+(i+1)+",b.price,0)) as a"+(i+1)+" , ";
}
}
String sql ="SELECT "+a+" FROM pay b where b.userid="+userId+" and extract(month from b.times)=07";
ResultSet rs1 = st.executeQuery(sql);
if(rs1.next()){
for(int i=0;i<s;i++){
list.add(rs1.getInt("a"+(i+1)));
}
}
return list;
}
作者: xiechao_gd 发布时间: 2011-12-20
SQL code
select SUM(price) as sumpayD from pay where DATEPART(MM,time) =DATEPART(MM,GETDATE()) and DATEPART(YY,time) =DATEPART(YY,GETDATE()) and userid=1 group by cast(time as varchar(10))
作者: sjcss 发布时间: 2011-12-20
SQL code
select convert(varchar(10),time,120) D,SUM(price) as sumpayD from pay where DATEPART(MM,time) =DATEPART(MM,GETDATE()) and DATEPART(YY,time) =DATEPART(YY,GETDATE()) and userid=1 group by convert(varchar(10),time,120)
作者: sjcss 发布时间: 2011-12-20
SQL code
select convert(varchar(10),time,120),d,sum(price) as sumpayD from pay where datediff(mm,time,getdate())=0 and userid=1 group by convert(varchar(10),time,120),d
作者: fredrickhu 发布时间: 2011-12-20
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28