0

I have a stored procedure and often that procedure will return multiple rows, for example:

ID     Type    Price
1234    A      2.260
1234    B      2.690
1234    C      2.990
1234    D      2.690
1234    D      2.790
1234    D      2.650
1234    D      2.680

And I want to output the latest value for each type. In my data reader I have:

While dr.Read
    result.price.TypeA= dr("price")
    result.price.TypeB= dr("price")
    result.price.TypeC= dr("price")
    result.price.TypeD= dr("price")
End While

and my query looks like:

select  sm_id,
        type,
        price
from    ** WITH (NOLOCK)
where   id = @id
order by id desc

I'm not sure how to store all of my results into my object so I can access them in my front end.

5
  • LINQ. LINQ is your answer. See here stackoverflow.com/questions/157786/… or stackoverflow.com/questions/14502428/… . I tried to type out an answer but there were just too many assumptions and unknowns to be coherent. One of the problems with your query is that all of the IDs appear to be the same. If that is not a typo, you will need to have a unique identifier; I suggest ROW_NUMBER() Commented Aug 26, 2015 at 16:40
  • Also, your query will be to select the max ID grouped by type Commented Aug 26, 2015 at 16:40
  • "output the latest value" - how you determine latest value? Commented Aug 26, 2015 at 16:51
  • @T.S. After posting this I realized I needed to query for another column. I now have update date as a part of the query. Commented Aug 26, 2015 at 17:03
  • @rlb.usa thanks so much! I'll check it out! Commented Aug 26, 2015 at 17:04

1 Answer 1

0

This is may be not most performant query but it will do the job as what you described it

select t1.id, t1.t, t1.maxDt, t2.price
from 
    (select id, [TYPE] t, MAX(ud) maxDt 
     from dbo.a it
     where id =1 
     group by id, [type]) t1 
         left join
     dbo.a t2 on t1.id = t2.id and t1.t = t2.[type] and t1.maxDt = t2.ud

Now, storing results in object and displaying it

Public Class MyClass
    Public Property Id As Integer
    Public Property [Type] As String
    Public Property [Date] As DateTime
    Public Property Price As Decimal
End Class

. . . . . 

Dim myList As New List(Of MyClass)()
While dr.Read()
    Dim item As New MyClass()
    item.Id = dr("id")
    item.[Type] = dr("t")
    item.[Date] = dr("maxDt")
    item.Price = dr("price")
    myList.Add(item)
End While

myDataGrid.DataSource = myList

This is what you, approximately, need

Sign up to request clarification or add additional context in comments.

4 Comments

It's the obvious answer, yes, but it sounds like changing his query isn't what he wants to do. It sounds like he wants his cake and eat it too, which can be accomplished, but is more difficult.
@rlb.usa Lest see what he says
@T.S. I'm totally fine with changing the query. Is there anyway I can just pass back an entire object? Is that what grouping accomplishes?
@ryandonohue Please see my updated answer. This should be good start for you. Grouping accomplishes selecting max date / per type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.