PlotDirector/PlotLine/Sql/278_InvestigationSavedSummaries.sql

19 lines
1.1 KiB
Transact-SQL

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,
i.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 i.UpdatedUtc DESC,i.ID;
END;
GO