Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Selecting columns based on user input? Expand / Collapse
Author
Message
Posted Monday, July 20, 2009 8:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 20, 2009 4:21 PM
Points: 6, Visits: 11
I am not sure how to describe this and I'm sorry if the subject isn't very clear.

Here is what I want to do: I have to generate reports for my employer and sometimes he wants to see things at varying degrees of detail. For example:

(The example will have a Table called "Cars" with columns "Yr", "Make", "Model", and "Price")

Sometimes I need to know, say...the average price of all cars for a year (this query):

SELECT Yr, AVG(Price)
FROM Cars
GROUP BY Yr

And Sometimes I need the average price for all cars of a year and make (this query):

SELECT Yr, Make, AVG(Price)
FROM Cars
GROUP BY Yr, Make

Is there any way using just SQL to write a query that could handle both cases? I am integrating this within a .NET application so of course I could always build up the query string in my C# file depending on the requirements, but that feels so messy and is a real pain to debug, especially when you start adding rollups and whatnot. Also it seems that doing as much in SQL and as little in .NET is always best for performance.

I've thought of is to write a procedure for each "level" of detail, but the amount of redundancy within those procedures would be really high, and the last thing I want to do is update 4 procedures when I need to make a change.

I've also thought of writing a query that returns all of the data at every level and then just pick what is needed out of that, but that takes too much time.

If you have any suggestions, throw them my way! Thanks in advance for any help,
Post #755794
Posted Monday, July 20, 2009 8:40 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
Your options are either to limit the selections the manager has and run a specific stored procedure for each option, return the all the detail data and do all the manipulation and roll up in your application, or do a single stored procedure that uses dynamic sql that returns the data as desired. The downside to the third option is that you need to be sure to protect against SQL Injection. Here are a couple of links to articles acknowledged to be some of the best about dynamic sql.

http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/dyn-search-2005.html




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #755832
Posted Monday, July 20, 2009 8:59 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 5:13 PM
Points: 33,100, Visits: 15,210
The thing I'd suggest is that you write two stored procedures, which is essentially two methods. Then if you want to build an abstraction, write a 3rd that takes both parameters, if only one comes in (check for NULL), call one proc, otherwise call the other.

That seems like work, but if you build it up in your app, you're essentially doing the same thing. By encapsulating this into multiple procs, you can easily call them from other places, and also it is tune-able if more results or columns are needed.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #755864
Posted Monday, July 20, 2009 9:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 20, 2009 4:21 PM
Points: 6, Visits: 11
Aha, dynamic SQL is the term I needed.

I think I'm pretty well protected from SQL Injection by using .Net's SqlParameter object and specifying the type. I've seen in other projects where it has prevented some potentially malicious injection attacks.

I'll get to reading...thanks a bunch!
Post #755865
Posted Monday, July 20, 2009 9:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 20, 2009 4:21 PM
Points: 6, Visits: 11
Steve Jones - Editor (7/20/2009)
The thing I'd suggest is that you write two stored procedures, which is essentially two methods. Then if you want to build an abstraction, write a 3rd that takes both parameters, if only one comes in (check for NULL), call one proc, otherwise call the other.

That seems like work, but if you build it up in your app, you're essentially doing the same thing. By encapsulating this into multiple procs, you can easily call them from other places, and also it is tune-able if more results or columns are needed.


That seems like a good solution, but it would ultimately lead to a lot of duplicating a lot of the same code. But, maybe if I put all of the complex joins and temporary tables into a view and then use the various procedures to aggregate the data from the view I could avoid having to rewrite all the crap. That might be just what I need.
Post #755872
Posted Monday, July 20, 2009 9:22 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 5:13 PM
Points: 33,100, Visits: 15,210
You're not necessarily duplicating lots of code. You might dup some, but you're also separating out and abstracting the functions to do a specific thing. That's what's done in many methods in OOP programming. You separate out those different items that perform different functions.

Don't forget, you copy this code and alter it a couple times. It's very little of your time. Trying to munge things together and having the end-user suffer later will happen over, and over again.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #755910
Posted Monday, July 20, 2009 9:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
I understood you to mean that you could have several different ways the manager wants to look at the data. If it is only those 2 then I would do as Steve has suggested.

To be honest, I have always preferred to write static reports for specific purposes. This way you can more easily optimize the T-SQL.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #755926
Posted Monday, July 20, 2009 10:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 20, 2009 4:21 PM
Points: 6, Visits: 11
Jack Corbett (7/20/2009)
I understood you to mean that you could have several different ways the manager wants to look at the data. If it is only those 2 then I would do as Steve has suggested.

To be honest, I have always preferred to write static reports for specific purposes. This way you can more easily optimize the T-SQL.


It's possible...One minute he wants one thing and then 2 years later he could decide he wants something completely different

In general though, the data can really be seen on 4 levels: customer, item category, item subcategory, and item. So there are potentially 4 of them in the specific case I'm working on now. I'd kind of like to set up a best practice sort of thing though, for all of my future reports because that's really a lot of my job and it's not something that's ever been done very well here.

I definitely see your point about optimization though. At least you've both given me a good bit to chew on
Post #755972
Posted Monday, July 20, 2009 10:21 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 5:13 PM
Points: 33,100, Visits: 15,210
I'd be curious to know what you decide to do and why. It's a good debate, and others can learn from this. If you have a blog, post it there and link from here. Or write us an article about why you did what you did.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #755976
Posted Monday, July 20, 2009 10:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
I definitely agree with Steve that you should post your choice. Especially why you chose that route.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #755984
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse