PlotDirector/PlotLine/Sql/279_InvestigationSavedActivity.sql

20 lines
1.2 KiB
Transact-SQL

-- Include explicit follow-through in the saved investigation activity timestamp.
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE OR ALTER PROCEDURE dbo.WriterInvestigationSavedSummaries @UserID int,@BookID int AS
BEGIN
SET NOCOUNT ON;
SELECT TOP(50) i.ID,i.BookID,i.CatalogueVersion,JSON_VALUE(i.Json,'$.Concern') Concern,
CASE WHEN JSON_VALUE(i.Json,'$.CurrentQuestion') IS NOT NULL THEN 'In progress' WHEN e.Event IS NULL THEN 'Older saved investigation' ELSE 'Results available' END Status,
CASE WHEN g.UpdatedUtc>i.UpdatedUtc THEN g.UpdatedUtc ELSE i.UpdatedUtc END UpdatedUtc,ISNULL(g.Version,0) GuidanceVersion,ISNULL(g.IsActive,0) IsActive,ISNULL(g.Finished,0) Finished
FROM dbo.WriterInvestigations i
JOIN dbo.Books b ON b.BookID=i.BookID AND b.ProjectID=i.ProjectID AND b.IsArchived=0
JOIN dbo.Projects p ON p.ProjectID=b.ProjectID AND p.IsArchived=0
JOIN dbo.ProjectUserAccess a ON a.ProjectID=p.ProjectID AND a.UserID=@UserID AND a.IsActive=1
LEFT JOIN dbo.WriterInvestigationGuidance g ON g.InvestigationID=i.ID AND g.UserID=@UserID
LEFT JOIN dbo.WriterInvestigationEvents e ON e.InvestigationID=i.ID AND e.Version=i.Version
WHERE i.UserID=@UserID AND i.BookID=@BookID ORDER BY UpdatedUtc DESC,i.ID;
END;
GO