SQL Server:在SELECT

时间:2016-07-22 15:08:20

标签: sql sql-server ssrs-2008

在SSRS 2008中,我的查询格式为

WITH CTE ( *that makes a list of dates, unfortunately only starting from today, not from whatever past date the report demands*)
    SELECT 
        *a bunch of columns*
    FROM 
        asimpletable p1
    CROSS APPLY 
        (*CTE*) dates1
    LEFT OUTER JOIN 
        (*demand list subquery with several selects pulling from several tables*) d1 ON (*simple.PartID AND CTE.demandDate*)
    LEFT OUTER JOIN 
        (*supply list subquery with several selects pulling from several tables*) s1 ON (*yada*)  
    *..and 4 other joins to subqueries*
    WHERE 
        *enough conditions that I don't want to copy it 6 times for 6 different INSERT INTO statements*

因为CTE只能从今天开始,当我显示所有按日期选择时,过去的任何日期都会无序排列。

所以我想制作一个临时表来交叉应用并提供我需要的所有日期。我可以看到如何使用6 INSERT INTO,复制连接的子查询,但不仅会看起来很难看,所以保持所有子查询同步会更糟。

我喜欢这个声音,但看不到如何返回选择,也不知道如何将它应用于多个子查询https://stackoverflow.com/a/1101516/5612361

CTE的想法仅从今天开始:

SET @CTEStartDate = cast(getdate() as date); 
SET @CTEEndDate = cast(dateadd(day,100,getdate()) as date);

WITH Dates(eaDate) AS (
    SELECT @CTEStartDate AS eaDate  --Select First day in range
    UNION ALL
    SELECT DATEADD(DAY, 1, eaDate) FROM Dates WHERE eaDate < @CTEEndDate  --Add a record for every day in the range
)

当我尝试使用

SET @CTEStartDate = cast(dateadd(day,-30,getdate()) as date);

我得到了

  

有关此错误的详细信息,请导航到报表服务器   本地服务器计算机,或启用远程错误   ----------------------------无法读取数据集DataSet1的下一个数据行。 (rsErrorReadingNextDataRow)   ----------------------------报告处理期间发生错误。 (rsProcessingAborted)

一旦我拿出那个日期,它就会毫不费力地解决。

0 个答案:

没有答案