求问问题所在。。。
declare @day date
set @day='2013-12-05'
declare @day_2 date
set @day_2='2013-12-08'
while(@day<@day_2)
begin
SELECT distinct opttype as 消耗项目,sum(ABS(lijinchange))as 消耗量,sum(count(distinct account))as 消费人数,SUM(sum(abs(lijinchange)))as 一天总消耗量,@day as 日期
FROM [mydb_extendsgamelog1].[dbo].[12_TS_PlayerLiJinChange_log]where lijinchange<0 and @day=(convert(date,logtime,105))and opttype not like 'auction buy item comm-%'group by opttype,@day
set @day=dateadd(dd,1,@day)
end
这个语句运行后出错显示
消息 164,级别 15,状态 1,第 8 行
每个 GROUP BY 表达式必须至少包含一个不是外部引用的列。
求大神指点group by后面条件该怎么改。。
[解决办法]
declare @day date
set @day='2013-12-05'
declare @day_2 date
set @day_2='2013-12-08'
while(@day<@day_2)
begin
SELECT distinct opttype as 消耗项目,
sum(ABS(lijinchange))as 消耗量,
sum(count(distinct account))as 消费人数,
SUM(sum(abs(lijinchange)))as 一天总消耗量,
@day as 日期
FROM [mydb_extendsgamelog1].[dbo].[12_TS_PlayerLiJinChange_log]
where lijinchange<0 and @day=(convert(date,logtime,105))and opttype not like 'auction buy item comm-%'
group by opttype,@day -->group by opttype,日期
set @day=dateadd(dd,1,@day)
end
[解决办法]
再试试:
declare @day date
declare @day1 date
set @day='2013-12-05'
set @day1 = @day
declare @day_2 date
set @day_2='2013-12-08'
while(@day<@day_2)
begin
SELECT distinct opttype as 消耗项目,
sum(ABS(lijinchange))as 消耗量,
count(distinct account)as 消费人数,
sum(abs(lijinchange))as 一天总消耗量,
@day as 日期
FROM [mydb_extendsgamelog1].[dbo].[12_TS_PlayerLiJinChange_log]
where lijinchange<0 and @day=(convert(date,logtime,105))
and opttype not like 'auction buy item comm-%'
group by opttype
set @day=dateadd(dd,1,@day)
end
SELECT distinct opttype as 消耗项目,
sum(ABS(lijinchange))as 消耗量,
count(distinct account)as 消费人数,
sum(abs(lijinchange))as 一天总消耗量,
@day as 日期 --合计
FROM [mydb_extendsgamelog1].[dbo].[12_TS_PlayerLiJinChange_log]
where lijinchange<0
and (convert(date,logtime,105))>=@day1
and (convert(date,logtime,105))<@day2
and opttype not like 'auction buy item comm-%'
group by opttype