我在 SQL Server 2008 上有大约 10 个相当复杂的 SQL 查询 - 但客户端希望能够通过 Crystal Reports XI 从其内部网络(而不是非本地 Web 应用程序)运行它们。
客户端的内部网络不允许我们 (a) 对其专有数据库具有写访问权限,也不允许 (b) 允许我们设置中间 SQL 服务器(这意味着我们不能设置存储过程或其他数据清理)。
SQL 包含row_number() over (partition by col1, col2)、group by col1、 col2 with cube|rollup和/或(多个)pivot 的多个实例。
这甚至可以做到吗?我读过的所有内容似乎都表明这只能通过存储过程实现,我仍然需要先从专有数据库中提取数据。
以下是其中一个查询的精简版本(例如,与功能不直接相关的 JOIN、WHERE 子句和已删除的六列)...
select sum(programID)
, sum([a.Asian]) as [Episodes - Asian], sum([b.Asian]) as [Eps w/ Next Svc - Asian], sum([c.Asian])/sum([b.Asian]) as [Avg Days to Next Svc - Asian]
, etc... (repeats for each ethnicity)
from (
select programID, 'a.' + ethnicity as ethnicityA, 'b.' + ethnicity as ethnicityB, 'c.' + ethnicity as ethnicityC
, count(*) as episodes, count(daysToNextService) as episodesWithNextService, sum(daysToNextService) as daysToNextService
from (
select programID, ethnicity, datediff(dateOfDischarge, nextDateOfService) as daysToNextService from (
select t1.userID, t1.programID, t1.ethnicity, t1.dateOfDischarge, t1.dateOfService, min(t2.dateOfService) as nextDateOfService
from TABLE1 as t1 left join TABLE1 as t2
on datediff(d, t1.dateOfService, t2.dateOfService) between 1 and 31 and t1.userID = t2.userID
group by t1.userID, t1.programID, t1.ethnicity, t1.dateOfDischarge, t1.dateOfService
) as a
) as a
group by programID
) as a
pivot (
max(episodes) for ethnicityA in ([A.Asian],[A.Black],[A.Hispanic],[A.Native American],[A.Native Hawaiian/ Pacific Isl.],[A.White],[A.Unknown])
) as pA
pivot (
max(episodesWithNextService) for ethnicityB in ([B.Asian],[B.Black],[B.Hispanic],[B.Native American],[B.Native Hawaiian/ Pacific Isl.],[B.White],[B.Unknown])
) as pB
pivot (
max(daysToNextService) for ethnicityC in ([C.Asian],[C.Black],[C.Hispanic],[C.Native American],[C.Native Hawaiian/ Pacific Isl.],[C.White],[C.Unknown])
) as pC
group by programID with rollup
Sooooooo.... 这样的东西甚至可以翻译成 Crystal Reports XI 吗?
谢谢!