当前位置: 代码迷 >> Sql Server >> SQL分组查询有关问题,
  详细解决方案

SQL分组查询有关问题,

热度:12   发布时间:2016-04-27 15:00:48.0
SQL分组查询问题,急
ALTER proc [dbo].[proc_getZhanDianShuJuData1]
 
  @stime varchar(200),--时间从
  @etime varchar(200),--至
  @start1 varchar(200),
  @end1 varchar(200)
as


declare @dian1 decimal(18,2),@dian2 decimal(18,2),@liuliang1 decimal(18,2),@liuliang2 decimal(18,2)

select @dian1=sum(UsePower) from History where Testtime>[email protected] and Testtime<[email protected] group by StationName
select @dian2=sum(UsePower) from History where Testtime>[email protected] and Testtime<[email protected] group by StationName
select @liuliang1=sum(Alltotalrunning) from History where Testtime>[email protected] and Testtime<[email protected] group by StationName
select @liuliang2=sum(Alltotalrunning) from History where Testtime>[email protected] and Testtime<[email protected] group by StationName
select distinct Stationinfo.StationName as 站名,@dian1 as 起始电量,@dian2 as 终止电量,@liuliang1 as 起始累计流量,@liuliang2 as 终止累计流量,
@[email protected] as 用电量,@[email protected] as 用水量,Convert(decimal(18,2),(@[email protected])/(@[email protected])) as 吨水耗电 from History,Stationinfo
where Testtime between @stime and @end1 and History.StationName=Stationinfo.Agreement 


这样写查询出来的数据是重复的。
一店 1193989.00 1937564.00 563617.07 783854.06 743575.00 220236.99 3.38
二店 1193989.00 1937564.00 563617.07 783854.06 743575.00 220236.99 3.38



------解决方案--------------------
重复的时候看下是不是在有个字段的判断有重复的没有注意到。。
------解决方案--------------------
select @dian1=sum(UsePower) from History where Testtime>[email protected] and Testtime<[email protected] and StationName = '一店' group by StationName

------解决方案--------------------
如果你里面有多个店的数据,group by 出来不是有多条数据吗?不这样指定,你怎么赋值给你的变量?
  相关解决方案