September 1, 2008 at 7:21 am
Hi,
I have batches of 3 rows, what is the easiest way to compare these 3 rows to determine which row has the highest value in a specific column within these 3 rows please?
Thanks,
Best Regards,
BMM
September 1, 2008 at 9:40 am
Rather obscure question ... need more information in order to assist you
Suggest you read the article in my signature block and supply requested information so that some one may help.
September 2, 2008 at 4:47 am
My project involves the input and calculation of horse racing related data.
I have a button that loads a table's contents into a datagridview component - this can be
all the data, information for a specific date or information from a specific race.
One of the databases stores the results of a calculation which produces 3 horses from any
given race - this database will contain 3 horses from any race stored in the main database.
Within each set of 3 horses, I am looking for a method of comparing specific columns of
data to find the highest value within a specific column. The above screen capture should
provide a better understanding of what I am trying to achieve.
If further information is required to assist me in finding a solution to this problem, then
please let me know.
Thanks
September 2, 2008 at 7:59 am
Its not a SQL Solution, but you can do it in your .NET application by enumerating the DataGridView.Rows collection and for each DataGridViewRow look at the column you want by name.
for (int i=0; i<dgv_Actions.Rows.Count; i++)
{
DataGridViewRow dgvr = dgv_Actions.Rows;
DataGridViewTextBoxCell namecell = (DataGridViewTextBoxCell)dgvr.Cells["dgvActions_Name"];
DataGridViewCheckBoxCell chkcell = (DataGridViewCheckBoxCell)dgvr.Cells["dgvActions_Selected"];
if (sessionevent.ActionString.Contains(namecell.Value.ToString()))
_ActionList.Selected = true;
}
The above is an example from a opensource app that I am working on that is on codeplex. There is a DataGridView name dgv_Actions that has a DataGridViewTextBoxCell named dgvActions_Name and a DataGridViewCheckBoxCell named dgvActions_Selected. I use it to iterate over check the checked items and set a flag on an object based on whether the box it checked, but the code sample should be enough for you to see how to enumerate the collection, and access the specific column by name.
To do it in TSQL, if you are on SQL 2005, you could use a series of CTE's to mark the row in each grouping with a bit 1 = max row, 0 = not max row, or you could use a table variable/temp table as a intermediate storage point, and update a bit column for the max row, and then select the data from the table variable/temp table for return to the application.
Enumerating the collection in the .NET app may or may not be faster, but probably should be faster, depending on the complexity of the logic.
Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
My Blog | Twitter | MVP Profile
Training | Consulting | Become a SQLskills Insider
Troubleshooting SQL Server: A Guide for Accidental DBAs[/url]
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply