我试图跟踪多个提案的状态,这些提案将出现在"草稿中," "提交,"或者"批准"状态。提案可以在草稿之间来回切换,并在批准之前多次提交,因此创建了一个联结表,以通过proposalPackageID,approvalTypeID和reviewDate跟踪提案状态更改。简单的查询(好的,不那么简单)需要返回各种状态的计数。
我已经接近以下查询了(感谢此网站上的各种帖子和谷歌 - 没有什么真正击中头部的钉子)但我错过了一些创造虚假计数的作品。
SELECT COUNT(distinctified.approvalTypeID) AS MyCount, distinctified.approvalTypeID, refProposalApprovalType.approvalDescription
FROM (SELECT r.proposalPackageID, r.approvalTypeID, MAX(reviewedDate) AS MRD
FROM proposalReviewAction as r INNER JOIN proposalPackage ON r.proposalPackageID = proposalPackage.proposalPackageID
WHERE proposalPackage.proposalCallID = 7 -- 7=MAIN 8=Sat
GROUP BY r.approvalTypeID, r.proposalPackageID) as distinctified INNER JOIN proposalPackage ON distinctified.proposalPackageID = proposalPackage.proposalPackageID
INNER JOIN refProposalApprovalType ON distinctified.approvalTypeID = refProposalApprovalType.approvalTypeID
WHERE (proposalPackage.proposalCallID = 7) -- 7=MAIN 8=Sat
group by distinctified.approvalTypeID, refProposalApprovalType.approvalDescription
HAVING (distinctified.approvalTypeID = 25) or (distinctified.approvalTypeID = 50) --25 = submitted 50 = approved
order by approvalTypeID
抱歉,不得不离开,我忘了粘贴架构和数据......
CREATE TABLE [dbo].[proposalPackage](
[proposalPackageID] [int] IDENTITY(1,1) NOT NULL,
[title] [varchar](500) NULL,
[proposalCallID][int] NULL,
[startDate] [datetime] NULL,
[endDate] [datetime] NULL,
) ON [PRIMARY]
INSERT INTO [dbo].[proposalPackage]
([title]
,[proposalCallID]
,[startDate]
,[endDate])
VALUES
('test proposal 1',7,'2018-10-01','2019-09-30')
,('test proposal 2',7,'2018-10-01','2019-09-30')
,('test proposal 3',7,'2018-10-01','2019-09-30')
,('test proposal 4',7,'2018-10-01','2019-09-30')
,('test proposal 5',7,'2018-10-01','2019-09-30')
,('test proposal 6',7,'2018-10-01','2019-09-30')
,('test proposal 7',7,'2018-10-01','2019-09-30')
,('test proposal 8',7,'2018-10-01','2019-09-30')
,('test proposal 9',7,'2018-10-01','2019-09-30')
,('test proposal 10',7,'2018-10-01','2019-09-30')
CREATE TABLE [dbo].[refProposalApprovalType](
[approvalTypeID] [int] NOT NULL,
[approvalDescription] [varchar](50) NULL
) ON [PRIMARY]
INSERT INTO [dbo].[refProposalApprovalType]
([approvalTypeID]
,[approvalDescription])
VALUES
(5,'Rejected by Approving Official')
,(10,'Rejected by PM')
,(15,'Rejected by Executive Review Board')
,(25,'Submitted for Approval')
,(50,'Approved by Approving Official')
,(75,'Approved by PM')
,(100,'Approved by Executive Review Board')
CREATE TABLE [dbo].[proposalReviewAction](
[proposalReviewActionID] [int] IDENTITY(1,1) NOT NULL,
[proposalPackageID] [int] NULL,
[approvalTypeID] [int] NULL,
[comments] [varchar](2000) NULL,
[reviewedByID] [int] NULL,
[reviewedDate] [datetime] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[proposalReviewAction]
([proposalPackageID]
,[approvalTypeID]
,[comments]
,[reviewedByID]
,[reviewedDate])
VALUES
(1,25,'Submit',101,'2018-05-01')
,(2,25,'Submit',102,'2018-05-01')
,(3,25,'Submit',103,'2018-05-01')
,(4,25,'Submit',104,'2018-05-01')
,(5,25,'Submit',105,'2018-05-01')
,(1,10,'Incomplete',1001,'2018-05-10')
,(2,10,'Incomplete',1001,'2018-05-10')
,(3,10,'Incomplete',1001,'2018-05-10')
,(4,50,'Approved',1001,'2018-05-10')
,(1,25,'Resubmit',101,'2018-05-21')
,(2,25,'Resubmit',102,'2018-05-21')
,(3,25,'Resubmit',103,'2018-05-21')
,(7,25,'Submit',107,'2018-05-22')
,(1,10,'REJECTED',1001,'2018-05-22')
,(2,10,'REJECTED',1001,'2018-05-22')
,(3,10,'Approved',1001,'2018-05-22')
,(1,25,'Resubmit',101,'2018-05-23')
,(8,25,'Submit',108,'2018-05-23')
,(10,25,'Submit',110,'2018-05-24')
,(3,15,'REJECTED',1010,'2018-05-25')
我希望看到
MyCount approvalTypeID approvalDescription
6 25 Submitted for Review
1 50 Approved by Approving Official
感谢任何帮助...
答案 0 :(得分:0)
我认为您的子查询未正确获取每个包的最新状态。
如果可能,使用ROW_NUMBER()
窗口函数可以轻松识别每个包的最新状态行,同时保留该行的其他列值。
以下查询将为您提供每个包的最新状态:
SELECT ls.*
FROM (
SELECT r.proposalPackageID,
r.approvalTypeID,
RowNr = ROW_NUMBER() OVER (PARTITION BY r.proposalPackageID ORDER BY r.reviewedDate DESC)
FROM proposalReviewAction AS r
JOIN proposalPackage AS pp ON pp.proposalPackageID = r.proposalPackageID
WHERE pp.proposalCallID = 7) AS ls -- 7=MAIN 8=Sat
WHERE ls.RowNr = 1
AND ls.approvalTypeID IN (25, 50); -- 25 = sumbitted 50 = approved
请注意WHERE ls.RowNr = 1
这是删除旧状态的过滤器。确保approvalTypeID IN (25, 50)
的过滤器位于子查询的外部,以便我们获得每个包的真实最新状态。
然后,连接到其他表以从这些表返回其他列以匹配您喜欢的任何输出格式是一项简单的任务。