0

我有一个存储过程,它返回全天的时间段,以及每个时间段的容量。

    ALTER PROCEDURE [dbo].[TimeSlots_Sel] 
    ( 
        @dateReqd date, 
        @typeReqd varchar(5) = 'ATI' 
    ) 
    AS 
    BEGIN 
        SET NOCOUNT ON; 
        DECLARE @timeNow time(0) = CONVERT(time(0),GETDATE()) 

        IF @dateReqd >= CONVERT(date, GETDATE()) 
                BEGIN 
                        DECLARE @tblWIP AS dbo.tvpWIP 

                        INSERT INTO @tblWIP 
                                SELECT [Type], [status_id], Scheduled_Call 
                                FROM dbo.WIP 
                                WHERE status_id = 70 
                                        AND Scheduled_Call >= @dateReqd 
                                        AND Scheduled_Call < DATEADD(dd,1,@dateReqd) 

                        SELECT ts.SlotTime, 
                                dbo.funCapacity(@tblWIP, @dateReqd, ts.SlotTime, @typeReqd) AS [Capacity] 
                        FROM dbo.TimeSlots ts 
                        WHERE NOT EXISTS 
                        ( 
                                SELECT * 
                                FROM dbo.Downtime dt 
                                WHERE dbo.funStrToDateTime(YEAR(@dateReqd),MONTH(@dateReqd),DAY(@dateReqd), DATEPART(HOUR, ts.SlotTime),DATEPART(MINUTE, ts.SlotTime)) 
                                        BETWEEN 
                                                dbo.funStrToDateTime(YEAR(@dateReqd),dt.StartMonth, dt.StartDay, dt.StartHour, dt.StartMin) 
                                        AND dbo.funStrToDateTime(YEAR(@dateReqd),dt.EndMonth, dt.EndDay, dt.EndHour, dt.EndMin) 
                        ) 
                        AND dbo.funCapacity(@tblWIP, @dateReqd, ts.SlotTime, @typeReqd) > 0 
                        AND 
                        ( 
                                @dateReqd > GETDATE() 
                                OR 
                                ts.SlotTime >= @timeNow 
                        ) 
                        ORDER BY SlotTime 
                END 
   END 

所以我得到这样的信息

SlotTime    Capacity
--------------------
09:00:00    3
09:30:00    3
10:00:00    3
10:30:00    3
11:00:00    3
11:30:00    2
12:00:00    2
12:30:00    2

但现在我需要按日期显示一个显示未来 7 天容量的网格。任何想法如何修改存储过程以返回类似这样的内容

SlotTime Sun 14/11/2010 Mon 15/11/2010 Tue 16/11/2010 Wed 17/11/2010 Thu 18/11/2010 Fri 19/11/2010 Sat 20/11/2010
09:00:00 3              3              3              1              3              1              1
09:30:00 3              3              3              3              3              3              3
10:00:00 3              3              3              1              3              3              3
10:30:00 3              3              3              1              3              1              2
11:00:00 3              3              3              3              3              3              3
11:30:00 3              3              3              1              3              3              3

我确实有一个类似的程序来创建一个做类似事情的日历,但我发现很难显示我想要的东西。这是另一个存储过程:

ALTER PROCEDURE [dbo].[Calendar_Sel] ( @StartDate DATE ) 
AS 
BEGIN 
        SET NOCOUNT ON 

        DECLARE @EndDate DATE = DATEADD(Day,6,@StartDate) 
        DECLARE @dates VARCHAR(MAX) 
        DECLARE @datevals VARCHAR(MAX) 
        DECLARE @sql NVARCHAR(MAX) 
        DECLARE @ParmDefinition NVARCHAR(100) 

        ;WITH cte AS 
        ( 
                SELECT @StartDate AS DateColumn UNION ALL 
                SELECT DATEADD(Day,1,DateColumn) FROM cte WHERE DateColumn < @EndDate 
        ) 

        SELECT DateColumn INTO #temp FROM cte 

        SET @dates = STUFF((SELECT ',[' + CONVERT(VARCHAR,DateColumn,112) + ']' FROM #temp FOR XML PATH('') ),1,1,'') 
        SET @datevals = STUFF((SELECT ',COALESCE([' + CONVERT(VARCHAR,DateColumn,112) + '],'''') AS [' + CONVERT(VARCHAR,DateColumn,112) + ']' FROM #temp FOR XML PATH('') ),1,1,'') 

        SET @sql = ' 
                DECLARE @EndDate DATE = DATEADD(Day,6,@StartDate) 
                ;WITH cte AS 
                ( 
                        SELECT @StartDate AS DateColumn UNION ALL 
                        SELECT DATEADD(Day,1,DateColumn) FROM cte WHERE DateColumn < @EndDate 
                ) 
                ,cte1 AS 
                ( 
                SELECT DISTINCT r.RoomID, CONVERT(VARCHAR,DateColumn,112) AS DateColumn,  
                        CAST(CASE       WHEN EXISTS(SELECT * FROM dbo.Payments WHERE BookingID = b.BookingID) THEN 1 ELSE 0     END AS CHAR(1)) 
                        + CAST(b.BookingID AS CHAR(15)) + c.Surname AS CompoundKey 
                FROM dbo.Bookings b 
                JOIN dbo.BookingsDetails bd 
                        ON b.BookingID = bd.BookingID 
                JOIN dbo.Customers c 
                        ON c.CustomerId = b.CustomerId 
                JOIN dbo.Rooms r 
                        ON r.RoomId = bd.RoomId 
                JOIN cte 
                        ON cte.DateColumn BETWEEN b.DateArrive AND DATEADD(Day,-1,b.DateLeave) 
                ) 
                SELECT Room, 
                        Price1 = dbo.funStayPrice(@StartDate,DATEADD(Day,1,@StartDate),RoomTypeID,0), 
                        Price2 = dbo.funStayPrice(DATEADD(Day,1,@StartDate),DATEADD(Day,2,@StartDate),RoomTypeID,0), 
                        Price3 = dbo.funStayPrice(DATEADD(Day,2,@StartDate),DATEADD(Day,3,@StartDate),RoomTypeID,0), 
                        Price4 = dbo.funStayPrice(DATEADD(Day,3,@StartDate),DATEADD(Day,4,@StartDate),RoomTypeID,0), 
                        Price5 = dbo.funStayPrice(DATEADD(Day,4,@StartDate),DATEADD(Day,5,@StartDate),RoomTypeID,0), 
                        Price6 = dbo.funStayPrice(DATEADD(Day,5,@StartDate),DATEADD(Day,6,@StartDate),RoomTypeID,0), 
                        Price7 = dbo.funStayPrice(DATEADD(Day,6,@StartDate),DATEADD(Day,7,@StartDate),RoomTypeID,0), 
                        SUBSTRING([' + CONVERT(VARCHAR,@StartDate,112) + '],16,12) AS Entry1, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,1,@StartDate),112) + '],16,12) AS Entry2, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,2,@StartDate),112) + '],16,12) AS Entry3, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,3,@StartDate),112) + '],16,12) AS Entry4, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,4,@StartDate),112) + '],16,12) AS Entry5, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,5,@StartDate),112) + '],16,12) AS Entry6, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,6,@StartDate),112) + '],16,12) AS Entry7, 
                        SUBSTRING([' + CONVERT(VARCHAR,@StartDate,112) + '],2,15) AS ID1, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,1,@StartDate),112) + '],2,15) AS ID2, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,2,@StartDate),112) + '],2,15) AS ID3, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,3,@StartDate),112) + '],2,15) AS ID4, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,4,@StartDate),112) + '],2,15) AS ID5, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,5,@StartDate),112) + '],2,15) AS ID6, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,6,@StartDate),112) + '],2,15) AS ID7, 
                        SUBSTRING([' + CONVERT(VARCHAR,@StartDate,112) + '],1,1) AS Status1, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,1,@StartDate),112) + '],1,1) AS Status2, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,2,@StartDate),112) + '],1,1) AS Status3, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,3,@StartDate),112) + '],1,1) AS Status4, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,4,@StartDate),112) + '],1,1) AS Status5, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,5,@StartDate),112) + '],1,1) AS Status6, 
                        SUBSTRING([' + CONVERT(VARCHAR,DATEADD(Day,6,@StartDate),112) + '],1,1) AS Status7 
                FROM 
                ( 
                        SELECT r.RoomName, r.RoomName + '' ['' + rt.TypeDescription + '']'' AS Room, rt.RoomTypeID, '+ @datevals + ' 
                        FROM cte1 
                        PIVOT (MAX(CompoundKey) FOR DateColumn IN (' + @dates + ')) AS p 
                        RIGHT OUTER JOIN dbo.Rooms r 
                                ON r.RoomID = p.RoomID 
                        JOIN dbo.RoomTypes rt 
                                ON rt.RoomTypeID = r.RoomTypeID 
                ) x 
                ORDER BY CASE WHEN ISNUMERIC(RoomName) = 1 THEN CAST(RoomName AS INT) ELSE 0 END, RoomName'; 

        SET @ParmDefinition = N'@StartDate DATE'; 

        EXEC sp_executesql @sql, @ParmDefinition, 
                @StartDate = @StartDate; 
END

如果有人可以帮助我归还容量表,我将不胜感激。我希望在 GridView 中显示这些信息并且已经有了列标题,所以我只需要正确的数据存储过程。

非常感谢您的帮助。我相信任何建议都会有很大帮助。

4

1 回答 1

2

我试图稍微简化您的请求并提出这个解决方案。基本上,我们得到一个日期和时间列表,并与一个函数交叉连接以获取容量(我在这里使用了一个随机数),然后以日期为中心。由于列将是动态的,我们需要使用动态查询来获取列标题。

希望您可以使用它来应用到您自己的查询。

CREATE TABLE #TimeSlots (SlotTime TIME)

INSERT  INTO #TimeSlots VALUES  ('09:00:00')
INSERT  INTO #TimeSlots VALUES  ('09:30:00')
INSERT  INTO #TimeSlots VALUES  ('10:00:00')
INSERT  INTO #TimeSlots VALUES  ('10:30:00')
INSERT  INTO #TimeSlots VALUES  ('11:00:00')
INSERT  INTO #TimeSlots VALUES  ('11:30:00')

CREATE TABLE #DateSlots (Dates DATE)

INSERT  INTO #DateSlots VALUES  (GETDATE())
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 1, GETDATE()))
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 2, GETDATE()))
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 3, GETDATE()))
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 4, GETDATE()))
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 5, GETDATE()))
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 6, GETDATE()))
INSERT  INTO #DateSlots VALUES  (DATEADD(DAY, 7, GETDATE()))


DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT  @PivotColumnHeaders = COALESCE(@PivotColumnHeaders + ',[' + CAST(Dates AS VARCHAR) + ']', '[' + CAST(Dates AS VARCHAR) + ']')
FROM    #DateSlots
WHERE   Dates BETWEEN GETDATE() AND DATEADD(DAY, 7, GETDATE())

DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
    SELECT *
    FROM (
        SELECT SlotTime, cast(Dates as varchar) Dates, Abs(Checksum(NewId())) % 4 AS Capacity
        FROM #TimeSlots
        CROSS JOIN #DateSlots
        WHERE Dates BETWEEN GETDATE() AND DATEADD(DAY, 7, GETDATE())
    ) AS Source
    PIVOT
    (
        MAX(Capacity)
        FOR Dates IN (
          ' + @PivotColumnHeaders + '
        )
    ) AS PivotTable
'

EXECUTE(@PivotTableSQL)

DROP TABLE #TimeSlots
DROP TABLE #DateSlots
于 2012-11-15T00:11:45.937 回答