我在SQL Server 2008上有大约10个相当复杂的SQL查询 - 但客户希望能够通过Crystal Reports XI从内部网络(而非非本地Web应用程序)运行它们。
客户端的内部网络不允许我们(a)对其专有数据库具有写访问权限,也不允许(b)允许我们设置中间SQL服务器(意味着我们无法设置存储过程或其他数据清理)
SQL包含多个 row_number()over(按col1,col2划分), group by col1,col2 with cube | rollup 和/或(multiple枢轴。
甚至可以这样做吗?我读过的所有内容似乎都表明这只能通过存储过程实现,我仍然需要先从专有数据库中提取数据。
以下是其中一个查询的剥离版本(例如,JOIN与功能没有直接关系,WHERE子句,以及已删除了6个列)...
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吗?
谢谢!
答案 0 :(得分:0)
创建报告而不是选择表格或存储过程时,请选择add command
这将允许您在其中放置所需的任何有效TSQL语句。使用公用表表达式(CTE)和内联视图我已经设法为Oracle和SQL Server创建了一些相当大的复杂语句(超过400行),所以它确实可行,但是如果你使用参数你应该考虑使用你必须弄清楚如何避免SQL注入。sp_executesql