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 6818857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:13:46+00:00 2026-05-26T21:13:46+00:00

I am working on a small reporting application. I have two tables Agent Table

  • 0

I am working on a small reporting application. I have two tables

Agent Table Data

AgentID  AgentName
-------  ---------
1001     ABC
1002     XYZ
1003     POI
1004     JKL

Report Table Data

ReportID  AgentId  Labor  Mandays  Amount  SubmitDate
--------  -------  -----  -------  ------  ----------
1         1001     30     10       5000    11/12/2011
2         1001     44     18       8000    11/14/2011
3         1002     33     75       3022    11/12/2011
4         1001     10     10       1500    11/14/2011
5         1002     10     10       1800    11/14/2011
6         1001     10     10       1400    11/14/2011
7         1003     40     40       1500    11/14/2011
8         1003     40     40       1800    11/14/2011

I want to generate a report which gives us output like

ReportID  AgentId  Labor  Mandays  Amount  SubmitDate
--------  -------  -----  -------  ------  ----------
1         1001     30     10       5000    11/12/2011
3         1002     33     75       3022    11/12/2011
6         1001     10     10       1400    11/14/2011
5         1002     10     10       1800    11/14/2011
8         1003     40     40       1800    11/14/2011

Thanks in Advance

  • 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-05-26T21:13:47+00:00Added an answer on May 26, 2026 at 9:13 pm

    You didn’t mention what VERSION of SQL Server you’re using – if you’re on 2005 or newer, you can use a CTE (Common Table Expression) with the ROW_NUMBER function:

    ;WITH LastPerAgent AS
    (
       SELECT 
           AgentID, ReportID, Labor, Mandays, Amount, SubmitDate,
           ROW_NUMBER() OVER(PARTITION BY AgentID,SubmitDate
                             ORDER BY ReportID DESC) AS 'RowNum'
       FROM dbo.Report
    )
    SELECT 
       AgentID, ReportID, Labor, Mandays, Amount, SubmitDate,
    FROM LastPerAgent
    WHERE RowNum = 1
    

    This CTE “partitions” your data by AgentID and SubmitDate, and for each partition, the ROW_NUMBER function hands out sequential numbers, starting at 1 and ordered by ReportID DESC – so the “last” row (with the highest ReportID) for each (AgentID, SubmitDate) pair gets RowNum = 1 which is what I select from the CTE in the SELECT statement after it.

    PS: this doesn’t work 100% on your input data, since you’ve not defined how to group and how to eliminate rows…. you might need to adapt this query a bit, based on your requirements…

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

Sidebar

Related Questions

I have an small ASP.NET application that reads data from a table and sends
Im working on a small application to try out an idea that I have.
We have service that's working with small to large sets of data (document generation),
I have a small working application in ASP.NET and C#, and I would like
I am working on small application of sharepoint. There two ways in which WE
is there any small working program for recieving from and sending data to client
I am working on a small application in VB.NET. The program needs administrator privilege
Ok so, I have been working on a small webapp in sinatra. After taking
Currently I am working on small application in asp.net mvc. It is a some
I am new in android and currently working on small application that works on

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.