1

我的表有以下列,(复制以创建测试表)

CREATE TABLE dbo.[PDNApprovalDetail](
    [PDNApprovalMasterID] [int] NOT NULL,
    [PDNMasterID] [int] NOT NULL,
    [PDNApprovalDetailID] [int] IDENTITY(1,1) NOT NULL,
    [DocumentToApproveID] [int] NULL,
    [RulesID] [int] NULL,
    [RuleSignatureTypeID] [smallint] NULL,
    [SignatureTypeID] [smallint] NULL,
    [ApprovalOrder] [smallint] NULL,
 CONSTRAINT [PK_PDNApprovalDetail] PRIMARY KEY CLUSTERED 
(
    [PDNApprovalDetailID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

-- use this insert statement

INSERT INTO [dbo].[PDNApprovalDetail]
           ([PDNApprovalMasterID]
           ,[PDNMasterID]
           ,[DocumentToApproveID]
           ,[RulesID]
           ,[RuleSignatureTypeID]
           ,[SignatureTypeID]
           ,[ApprovalOrder])
     VALUES
           (136
           ,125
           ,7
           ,2
           ,1
           ,0
           ,1)

我表中的数据可能如下所示(复制并粘贴以插入查询)

136,    125,    7,  2,  1,  0,  1
135,    125,    7,  2,  1,  1,  1
136,    125,    7,  2,  1,  1,  1
135,    125,    7,  2,  0,  0,  1
137,    126,    7,  2,  1,  0,  1
137,    126,    7,  2,  1,  0,  1
139,    126,    7,  2,  0,  1,  1
139,    126,    7,  2,  0,  1,  1
138,    127,    7,  2,  1,  1,  1
140,    127,    7,  2,  0,  0,  1
140,    127,    7,  2,  0,  0,  1

我希望通过分组和仅获取具有 MAX PDNApprovalMasterID 的行来输出查询,对于 125 个 PDNMasterID 的组,结果不应该包含 ID 为 135 的行,对于第 125 组,依此类推,对于 126 个组,只有具有 MAX PDNApprovalMasterID 139、137 的行不应该在那里,PDNMasterID 127 结果行应该包含 140 并且不应该包含 138

查看查询如何获取其余列的总和,这是我正在尝试但没有得到正确结果的方法

SELECT DISTINCT pa.PDNMasterID, pa.DocumentToApproveID, pa.PDNApprovalMasterID
    , SUM(CASE WHEN pa.SignatureTypeID = pa.RuleSignatureTypeID THEN 1 ELSE 0 END) AS [Approved]
    , SUM(CASE WHEN pa.SignatureTypeID != pa.RuleSignatureTypeID THEN 1 ELSE 0 END) AS [Unapproved]
    , COUNT(*) AS RuleCount
    FROM dbo.PDNApprovalDetail pa
    WHERE pa.[DocumentToApproveID] = 7
    group by pa.PDNMasterID, pa.DocumentToApproveID, pa.PDNApprovalMasterID
    having pa.PDNApprovalMasterID = MAX(pa.PDNApprovalMasterID)

上述查询的结果,它包含不应该存在的 135 PDNAapprovalMasterID 行

PDNMasterID DocumentToApproveID PDNApprovalMasterID Approved Unapproved RuleCount
125                 7                   135             2         0         2
125                 7                   136             1         1         2
4

1 回答 1

1

尝试添加maxselect statement

SELECT pa.PDNMasterID, pa.DocumentToApproveID, MAX(pa.PDNApprovalMasterID)
    , SUM(CASE WHEN pa.SignatureTypeID = pa.RuleSignatureTypeID THEN 1 ELSE 0 END) AS [Approved]
    , SUM(CASE WHEN pa.SignatureTypeID != pa.RuleSignatureTypeID THEN 1 ELSE 0 END) AS [Unapproved]
    , COUNT(*) AS RuleCount
    FROM dbo.PDNApprovalDetail pa
    WHERE pa.[DocumentToApproveID] = 7  
    and pa.PDNApprovalMasterID = (select MAX(pa1.PDNApprovalMasterID)
                                  from  dbo.PDNApprovalDetail pa1 
                                  where pa.PDNMasterID = pa1.PDNMasterID
                                   and pa.DocumentToApproveID = pa1.DocumentToApproveID
                                  ) 
    group by pa.PDNMasterID, pa.DocumentToApproveID;

SQL 小提琴演示

于 2012-10-12T08:34:42.997 回答