Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8680893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:21:55+00:00 2026-06-12T21:21:55+00:00

My table have following columns, (copy to create test table) CREATE TABLE dbo.[PDNApprovalDetail]( [PDNApprovalMasterID]

  • 0

My table have following columns, (copy to create test table)

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)

data in my table may look like this (copy and paste to insert query)

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

i want the out put of query by grouping and getting only rows with MAX PDNApprovalMasterID, for group of 125 PDNMasterID, result should not contain rows with ID 135 for group 125 and so on,
for group of 126 only row with MAX PDNApprovalMasterID 139, 137 should not be there, PDNMasterID 127 result row should contain 140 and should not contain 138

Look at query how to get sum of rest of columns, this is what im trying but not getting right results

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)

result of above query, its contains 135 PDNApprovalMasterID row which should not be there

PDNMasterID DocumentToApproveID PDNApprovalMasterID Approved Unapproved RuleCount
125                 7                   135             2         0         2
125                 7                   136             1         1         2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:21:57+00:00Added an answer on June 12, 2026 at 9:21 pm

    Try add max to 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with following columns: ContractorId ......... INT ............. IDENTITY ContractorName ........
I have a table that contains the following columns: ID int, DISTANCE float, EVENT
Consider the following design: Company TABLE ( CompanyId int NOT NULL IDENTITY PRIMARY KEY,
Lets say I have table with following columns 1. Client - string. 2. Profit
I have three mysql table from same database Db1. Three tables have following columns.
I have a table with several employees. They have the following columns empid,datecolumn1,is_valid. Very
I have a table with the following columns in SQL Server: MEMBERID, MEMBEREMAIL, FATHEREMAIL,
I have a table with the following columns: ref_year, ref_no, inv_id, start_date, end_date The
I have a table with the following columns (I simplified it a little): PRODUCT_name
I have a table with the following columns: EntityId, EntityName, EntityProfile, ................. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.