日期:2014-05-17  浏览次数:20472 次

求改一个sql语句
SELECT COUNT(DISTINCT AccessFile) AS qqq FROM  dbo.Log201305
GROUP BY AccessName)

我要sum(qqq),请问怎么改
谢谢!!!
SQL

------解决方案--------------------

--也许你要的结果是这样
if object_id('Log201305') is not null
drop table Log201305
go
create table Log201305
(
AccessName nvarchar(10),
AccessFile int
)
go
insert into Log201305
select '张三',1 union all
select '张三',1 union all
select '张三',2 union all
select '张三',2 union all
select '张三',2 union all
select '李四',1 union all
select '李四',1 union all
select '李四',1 

select * from Log201305
go
select sum(qqq)qqq from
(
SELECT COUNT(DISTINCT AccessFile) AS qqq FROM  dbo.Log201305
GROUP BY AccessName
)a

------解决方案--------------------

--也许你要的结果是这样
if object_id('Log201305') is not null
drop table Log201305
go
create table Log201305
(
AccessName nvarchar(10),
AccessFile int
)
go
insert into Log201305
select '张三',1 union all
select '张三',1 union all
select '张三',2 union all
select '张三',2 union all
select '张三',2 union all
select '李四',1 union all
select '李四',1 union all
select '李四',1 

with cte as
(
SELECT AccessName,COUNT(DISTINCT AccessFile) AS qqq 
FROM  dbo.Log201305
GROUP BY AccessName
)
select *
from
cte
union all
select '总计',sum(qqq)qqq 
from
cte