We help IT Professionals succeed at work.

Get first date and last date from datatable.

emi_sastra
emi_sastra asked
on
Medium Priority
314 Views
Last Modified: 2012-05-06
Hi,

I have a datatable with transaction date on it, let's say TrsDate, and other data. The data is not sorted by TrsDate.
How to get the first transaction date and last transaction date from the datatable?

Thank you.
Comment
Watch Question

Project manager
CERTIFIED EXPERT
Commented:
I see several solutions:

1. use the DataTable.Compute function.

maximum:  datatable.Compute("Max(myColumn)", Nothing)
minimum: datatable.Compute("Min(myColumn)", Nothing)

The second parameter is a filter, incase you want to limit the rows that are
applied to the aggregate function.

2. Create a dataview and order it
dv as new dataview(datatable)
dv.sort="StaffIdColumn, DESC"
Minimum --> dv(dv.count-1)("mycolumn")
Maximum --> dv(0)("mycolumn")

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hi Dhaest,

Could I use :

dim dteMinDate as date =datatable.Compute("Min(TrsDate)", Nothing)
dim dteMaxDate as date =datatable.Compute("Max(TrsDate)", Nothing)

Thank you.

Commented:
Hi

is the TrsDate attribute type of date ? then sort the table and then select the first and last rows date

Dim sortstr As String = "TrsDate,otherColumn,otherColumn DESC"
YourDataTable.DefaultView.Sort = sortstr
Return YourDataTable

vbturbo

Author

Commented:
Hi Dhaest,

I've tried it. It works.

Thank you very much for your help.

Author

Commented:
Hi VBTurbo,

I am sorry, Dhaest has solved my problem.

Thank you.

Commented:
No problem at all.

I didn't see Dhaest post wich solves your question in many ways.
and then i would not even have posted my own comment

/vbturbo

Author

Commented:
The hints (not exact command) provided by Dhaest obviously solve my problem.

1. use the DataTable.Compute function.

maximum:  datatable.Compute("Max(myColumn)", Nothing)
minimum: datatable.Compute("Min(myColumn)", Nothing)

The second parameter is a filter, incase you want to limit the rows that are
applied to the aggregate function.

2. Create a dataview and order it
dv as new dataview(datatable)
dv.sort="StaffIdColumn, DESC"
Minimum --> dv(dv.count-1)("mycolumn")
Maximum --> dv(0)("mycolumn")

Thank you.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.