因为要重复查询一个结果集,这个结果集的数据的查询条件是不同的,但查出来的数据是将所有的数据都默认为第一个结果集,所以查询出来的数据都是一样的!请问下如何解决这个问题
DECLARE @SQL NVARCHAR(4000), @Day INT set @Day=1 select case when grouping(AreaName)=1 then '合计' else cast(AreaName as nvarchar(100)) end AreaName, case when grouping(BuildName)=1 and grouping(AreaName)=0 then '小计' else cast(BuildName as nvarchar(100)) end BuildName, Sum([COUNTER]) as FailureCount,SUM([FAX]) AS FAX from ( SELECT COUNT(f.FailureID) AS [COUNTER], COUNT(k.FailureID) AS [FAX], max(w.[LocationName]) AS BuildName, (SELECT LocationName FROM dbo.ReportLocation WHERE LocationID = w.ParentLocationID) AS AreaName FROM dbo.ReportLocation w LEFT JOIN ( select FailureID,ReportDateTime,LocationID,BuildingID from View_ReportFailure WHERE DATEPART(Day, ReportDateTime) = 1 )f ON f.[BuildingID] = w.[LocationID] LEFT JOIN ( select FailureID,ReportDateTime,LocationID,BuildingID from View_ReportFailure WHERE DATEPART(Day, ReportDateTime) = 1 and [status]=4 )k on w.[LocationID]=k.[BuildingID] WHERE w.IsEnabled = 1 AND w.ParentLocationID IS NOT NULL GROUP BY w.[LocationID],w.ParentLocationID )z group by AreaName,BuildName with rollup
以下是查出来的数据:
select * from T t1
join T t2 on t1.id=t2.pid
join T t3 on t2.id=t3.pid
你这个没效果的
@未找到对象:自连接就是这样。不明白你是想连接还是union。
@幻天芒: COUNT(f.FailureID) AS [COUNTER]和 COUNT(k.FailureID) AS [FAX]值这样写确实结果是一样的!
@未找到对象: 条件呢,带上条件就不一样了。
@幻天芒: 我这里是 一个表一个字段多次统计,我上面的写法是不对的。
@未找到对象: 那就分开统计,再汇总。
@幻天芒: 谢谢!我自己解决的。挺热心的,所以分给了你
@未找到对象:...
没搞清楚题目意思!~