0

各タイムスロットの容量とともに、1日を通してタイムスロットを返すストアドプロシージャがあります。

    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 に答える