Avatar of njmatt
njmatt

asked on 

SQL Union query with itself with multiple sets of variables

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
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')

Open in new window

Microsoft SQL Server 2005

Avatar of undefined
Last Comment
njmatt
Avatar of elimesika
elimesika
Flag of Israel image

HI

You can use multiple statements in your where clause with OR between them

where
(first set of variables) or
(second set of variables) or
........
..........
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)
OR
(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')

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dragos_craciun
dragos_craciun
Flag of Romania image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of njmatt
njmatt

ASKER

thanks, it was the simplest route afterall
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo