日期:2014-05-18  浏览次数:20725 次

急急急!!!求高手帮我解决一个SQL server语句的问题,非常感谢。。。
我如果想查询本月的所有支出,在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;就可了。如果我想查询本月每天的所有支出,好像用:
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);也可以,
但我希望查询出来的每一列都代表一天的总支出,就好像今天是2011年12月21日,那么就应该显示21列,第一列是2011年12月1号那天的所有支出,第二列就是2号的所有支出,最后一列是21号的!不知道我的要求你懂不?
你看下这段代码对你有没有帮助:
//查询本月支出情况
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;

}
我用的是SQL server数据库,上面的SQL语句是oracle的.


------解决方案--------------------
SQL code

建议你提供详细的资料:
例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。
这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。

------解决方案--------------------
SQL code
declare @sql varchar(8000)
set @sql = 'select isnull(userid,''合计'') as 合计'
select @sql = @sql + ' , sum(case convert(varchar(10),time,120) when ''' + convert(varchar(10),time,120) + ''' then price else 0 end) [' + convert(varchar(10),time,120) + ']'
from (select distinct convert(varchar(10),time,120) from tb) as a
set @sql = @sql + ' from tb group by userid with rollup'
exec(@sql)

------解决方案--------------------
datediff(month,[date],getdate())=0筛选本月,做行转列。
------解决方案--------------------
SQL code
create table pay(--支出
id int identity(1,1) primary key,--主键
pay_title varchar(100),--支出名称
pay_name varchar(100),--支出人姓名
type_id int ,--foreign key references paytype(id),--支出类型(外键)
price float,--支出金额
time date,--支出时间
brief varchar(1000),--支出简介
userid int --foreign key references users(id)--支出人Id
)
insert into pay(pay_title,[time],price) select 'a','2011-07-01',542.21
insert into pay(pay_title,[time],price) select 'a','2011-07-01',985.25
insert into pay(pay_title,[time],price) select 'a','2011-07-02',246.55
insert into pay(pay_title,[time],price) select 'a','2011-07-03',81849.55
insert into pay(pay_title,[time],price) select 'a','2011-07-04',57154.21
insert into pay(pay_title,[time],price) select 'a','2011-08-04',74.21
go
select time,price into # from pay where CONVERT(varchar(7),[time],120)='2011-07'
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+'['+ convert(varchar(10),time,120) +']' from 
(select distinct time from #)t
exec('select '+@s+'from # pivot (sum([price]) for [time] in('+@s+'))b')
/*
2011-07-01             2011-07-02             2011-07-03             2011-07-04
---------------------- ---------------------- ---------------------- ----------------------
1527.46                246.55                 81849.55               57154.21

(1 行受影响)

*/
go
drop table pay,#
go