Do not use on any
shared computer
August 30, 2008 04:25am pdt
 
[x]
Attachment Details

SQL Union query with itself with multiple sets of variables

Tags: Microsoft, SQL Server, 2005, T-SQL
Hello Experts,

I have a statement that has a set of variables in the WHERE clause. Those variables are set in SQL Reporting by the user running the report. I'd like to put all of the results together in one query, but I don't want to UNION the same query 10 times with the different sets of variables to get what I want.
Is there an easy way to have the ONE query that UNIONS itself over and over until I'm done with my list of variables. ... almost like a For-Next / Do-Loop Until statement. I've never done this before in SQL.

Thanks
1:
2:
3:
4:
5:
where (c.accrual_plan_key = @accrual_plan_key and d.active = @active and b.project_key = @project_key and b.task_key = @task_key and b.work_date >= '8/1/2007' and b.work_date <= @report_date)
			group by a.person_key, b.project_key, b.task_key, c.end_date
			) 
			V1 on V1.person_key = a.person_key
where (e.chain_key = @chain_key and c.accrual_plan = @plan and b.active = @active and d.exempt_status = 'E' and a.posted_timestamp <= (@report_date + 4)  and d.end_date = '12/31/2099')
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Microsoft
Question Asked By: njmatt
Solution Provided By: dragos_craciun
Participating Experts: 2
Solution Grade: A
Views: 18
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by elimesika

Rank: Master

Expert Comment by elimesika:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by dragos_craciun
Accepted Solution by dragos_craciun:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628