需要协助编写查询以获取组中的最高号码

时间:2012-10-12 08:31:16

标签: sql sql-server

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

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为125的行125,依此类推, 对于具有MAX PDNApprovalMasterID 139的137行的组,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个PDNApprovalMasterID行,不应该存在

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

1 个答案:

答案 0 :(得分:1)

尝试将max添加到select 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 Fiddle DEMO