views:

754

answers:

2

I have a report that needs to display accounting data from different locations or all locations.

Table_sales:

PartId    Rev     LocId
1         $10     1
2         $2      1
3         $5      2

Lets say the query is something basic like this to get all revenue:

SELECT SUM(rev) FROM Table_sales

If I want to be able to limit the report to a particular Location I would have to change it to:

SELECT SUM(Rev) FROM Table_sales WHERE LocId = @param

But if I did that how would I get all locations without having multiple queries?

FYI the SQL 2008 Filter option doesn't seem work on the actual query I am using since the actual query uses group by functions.

Also SSRS 2008 DataSet queries can be expressions, but I can't seem to get that to work.

+3  A: 

I'm not sure about the filter options or dataset queries you mentioned in SSRS 2008, but in some of the older versions of SSRS, I've seen stuff like this:

SELECT SUM(Rev) FROM Table_sales WHERE LocId = @param or 'All' = @param

I'm doubtful that it's the most efficient way to do this, but it allows you to have 1 query where you can select either all results (by passing the term 'All' as the parameter) or just the LocId's you want (by passing the appropriate LocId as the parameter).

Bo Schatzberg
This is what we usually do for cases like this. You can also do something like WHERE LocId = COALESCE(@param, LocId) and pass null as your parameter for 'all' case... but I doubt either is noticably faster/more efficient.
Yadyn
Interesting idea on COALESCE - that hadn't occurred to me before.
Bo Schatzberg
Thanks, both methods work and are good ideas.
MaxGeek
A: 

Maybe you can use that:

SELECT SUM(Rev) FROM Table_sales WHERE LocId IN (@param)
Does that handle the case where I want to get them all? Like I would pass a null parameter or a specific string like 'All' or something.
MaxGeek