Need SQL Query to Get distinct of Column with max of that with Other Columns

Indudhar Gowda 426 Reputation points
2022-05-19T17:20:31.47+00:00

I have Provided the DB Backup and Screenshots to make life easier.

DB backup : https://1drv.ms/u/s!Ag8QU6ar3yRugYxQbVegOrsJImt3fg?e=O9Ud3d

Steps required in Order with Screenshot

1. Total Row Count of ReportableResults is 309,290
203814-1.png

2. Take DISTINCT of TestPRepetitionId Now the Row Count will be 100,886
203815-2.png

3.. For this DISTINCT of TestPRepetitionId Take the Highest Version in Version Column
203795-3.png

4. Based on DISTINCT of [TestPRepetitionId] and Highest Version in [Version ] Column get [ReportableResultId] & [AggregateResultId]
203766-4.png

End of the Day Row Count will 100,886

Azure SQL Database
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,061 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-05-19T19:22:17.07+00:00
    ;with mycte as (SELECT  [ReportableResultId]
          ,[ReportableResultCode]
          ,[ParameterCode]
          ,[IsAggregated]
          ,[TestPRepetitionId]
          ,[AggregateResultId]
          ,[Version]
          ,row_number() Over(partition by TestPRepetitionId order by  Version desc) rn
      FROM [Test].[dbo].[ReportableResults]
      )
    
      select 
       [ReportableResultId]
          ,[ReportableResultCode]
          ,[ParameterCode]
          ,[IsAggregated]
          ,[TestPRepetitionId]
          ,[AggregateResultId]
          ,[Version]
          from mycte
      where rn=1
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2022-05-20T06:18:36.237+00:00

    Screenshots to make life easier.

    It don't, because I can't query screenshots.
    Please post table design as DDL, some sample data as DML statement and the expected result.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.