Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Selecting columns based on user input?
13 posts, Page 1 of 2
1
2
»»
Selecting columns based on user input?
Rate Topic
Display Mode
Topic Options
Author
Message
gage.trader
gage.trader
Posted Monday, July 20, 2009 8:13 AM
Forum 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
Jack Corbett
Jack Corbett
Posted Monday, July 20, 2009 8:40 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 10,613,
Visits: 11,955
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
Steve Jones - SSC Editor
Steve Jones - SSC Editor
Posted Monday, July 20, 2009 8:59 AM
SSC-Dedicated
Group: Administrators
Last Login: Today @ 5:09 AM
Points: 31,526,
Visits: 13,864
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
gage.trader
gage.trader
Posted Monday, July 20, 2009 9:00 AM
Forum 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
gage.trader
gage.trader
Posted Monday, July 20, 2009 9:03 AM
Forum 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
Steve Jones - SSC Editor
Steve Jones - SSC Editor
Posted Monday, July 20, 2009 9:22 AM
SSC-Dedicated
Group: Administrators
Last Login: Today @ 5:09 AM
Points: 31,526,
Visits: 13,864
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
Jack Corbett
Jack Corbett
Posted Monday, July 20, 2009 9:36 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 10,613,
Visits: 11,955
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
gage.trader
gage.trader
Posted Monday, July 20, 2009 10:17 AM
Forum 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
Steve Jones - SSC Editor
Steve Jones - SSC Editor
Posted Monday, July 20, 2009 10:21 AM
SSC-Dedicated
Group: Administrators
Last Login: Today @ 5:09 AM
Points: 31,526,
Visits: 13,864
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
Jack Corbett
Jack Corbett
Posted Monday, July 20, 2009 10:30 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 10,613,
Visits: 11,955
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 »
13 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.