views:

46

answers:

4

I am wondering if it's possible to use a view to get the top 5 lines from a table. I am finding that Crystal reports doesn't seem to have anything built in to do this, or I'd do it there.

When I query the view Select * from qryTranHistory, it returns the first 5 items, but if I try to select a specific type Select * from qryTranHistory Where tID = 45 it returns nothing, since there are no tID=45 in the top 5 normally.

Is it possible to do this?
Can it be accomplished in a sub report in Crystal Reports?

A: 

Can you put the TOP in your SELECT statement instead of in the view?

SELECT TOP 5
    col1,
    col2,
    ...
FROM
    qryTranHistory
WHERE
    tid = 45
Tom H.
I don't know of a way to edit the query in Crystal Reports like that.That's why I was hoping to use a view, because I can generally make the view do whatever I want, and then just tell Crystal reports to grab all the needed rows from that. CR kinda sucks when it comes to complex joins and fancy things like Top 5.
AndyD273
A: 

If your table has more then 5 rows I hope this query:

SELECT * FROM qryTranHistory

Returns more then 5 rows because you never mentioned TOP 5. Your question doesn't make a lot of sense as I am not sure waht you are after. You mentioned if you ran your query with WHERE tID=45, it returns nothing, what exactly do you want it to return ?

Read up on TOP in BOL:

SELECT TOP 10 Recs FROM Records WHERE...

By the way you do not want to do this in the report / a form interface, you want to do this in your db layer.

JonH
qryTranHistory is a view and within that view it has, "SELECT TOP 5 ... FROM theRealTable..."
Tom H.
Ok. Lets say the table `tblTranHistory` has 1000 rows, with a couple dozen different tID's as a foreign key. If I try `Select * From tblTranHistory Where tID = 45` I get 36 rows returned, but I only want the top 5 in my report, and if the user wants the full list they can look in a different report. Since I do in fact want to do it my db layer, and I dont know of a way to change what the query is in CR, I was hoping to just have a query that would return the top 5 fields with a certain tID. So the query in the view is simply `Select Top 5 * from tblTranHistory`
AndyD273
+2  A: 

You can reference a sproc from Crystal Reports. In the sproc, use a conditional on the parameter.


ALTER PROCEDURE dbo.Get_TOP5
    (
    @tID INT = NULL
    )
AS
IF @tID IS NULL
    BEGIN
        SELECT TOP 5            
            FIELD1,
            FIELD2

        FROM qryTranHistory 
    END
ELSE
    BEGIN
        SELECT          
            FIELD1,
            FIELD2

        FROM qryTranHistory 

        WHERE tID =@tID
    END
Josaph
When you run the crystal report, the prompt for the parameter will pop up unless you specify it in a call from your application. This will either give you the top 5 rows if you don't specify a tID, or the row(s) for the tID specified.
Josaph
Ok, got it. I learned something new!
AndyD273
A: 

You can do Top N processing in Crystal Reports, but it's a little obscure - you have to use the group sort expert (and in order to use that, you need to have groups and summary fields inserted into the groups.)

Doing the Top N processing in the query should be more efficient, where possible.

Mark Bannister