RE: Merging two select cases

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Um, you aren't providing enough info but I am pretty sure adding a union
between the two lines would return the result you want.

Also, you do realize that the unions only apply the where clause to the #b
and #d selects right?


SELECT ID, PR_ID, [USER_ID] FROM #A a
UNION
SELECT ID, PR_ID, [USER_ID] FROM #B
WHERE [User_ID] IS NOT NULL
Union All
SELECT ID, PR_ID, [USER_ID] FROM #C
UNION
SELECT ID, PR_ID, [USER_ID] FROM #D
WHERE ([User_ID] IS NULL)

"mpaine" wrote:


I am trying to produce a single select statement which is true for two
separate cases (so I can reuse it within an IN expression.. which means no IF
statements). The answer is probably so simple I'll smack my head. Here is the
setup:

DROP TABLE #A; DROP TABLE #B; DROP TABLE #C; DROP TABLE #D;
CREATE TABLE #A (ID int primary key clustered, PR_ID int null, [User_ID] int
null)
INSERT INTO #A (ID, PR_ID, [User_ID]) VALUES (99, NULL, 904551)
CREATE TABLE #B (ID int primary key clustered, PR_ID int null, [User_ID] int
null)
INSERT INTO #B (ID, PR_ID, [User_ID]) VALUES (96, 57, NULL)
CREATE TABLE #C (ID int primary key clustered, PR_ID int null, [User_ID] int
null)
CREATE TABLE #D (ID int primary key clustered, PR_ID int null, [User_ID] int
null)
INSERT INTO #D (ID, PR_ID, [User_ID]) VALUES (56, 57, NULL)

SELECT ID, PR_ID, [USER_ID] FROM #A a
UNION
SELECT ID, PR_ID, [USER_ID] FROM #B
WHERE [User_ID] IS NOT NULL

SELECT ID, PR_ID, [USER_ID] FROM #C
UNION
SELECT ID, PR_ID, [USER_ID] FROM #D
WHERE ([User_ID] IS NULL)

Do you know of a way to merge both of these select statements? I am using
SQL Server 2008, so CTE, CROSS APPLY, etc are welcomed. Basically, I want the
rows with a non-null User_ID to be selected, have priority if you will. If no
User_ID is available but a PR_ID case is not null, go ahead and select that
instead (but never both).

THANK YOU!



--
msdn premium subscriber
.



Relevant Pages

  • Re: Establishing Precedence In ORDERBY Condition Causing Problems.
    ... tab4.login_date from (select distinct emailAddress from Users union ... I created the UserPrecedence table that describes the ordered ... ORDER BY CASE @sort ... The sp_ prefix is reserved for system procedures, and SQL Server first ...
    (comp.databases.ms-sqlserver)
  • Re: Upgraded to SQL 2005, now FOR XML AUTO clause doesnt work
    ... column-to-table association in UNION ALL in SQL Server 2000. ... FOR XML AUTO heuristics on grouping XML tags relies on column associations ...
    (microsoft.public.sqlserver.xml)
  • Re: Upgraded to SQL 2005, now FOR XML AUTO clause doesnt work
    ... > Hi Andre, ... > column-to-table association in UNION ALL in SQL Server 2000. ... > FOR XML AUTO heuristics on grouping XML tags relies on column associations ...
    (microsoft.public.sqlserver.xml)
  • Re: UNION over VIEWS
    ... the Stored Procedure makes the UNION from this two views... ... > Vyas, MVP (SQL Server) ...
    (microsoft.public.sqlserver.programming)
  • Re: Error using a derived table
    ... to re-write your query without all of the nested SELECTs and the CASE ... > union all ... > select s.misspelledname as 'Resortname' ... I am using SQL Server 2000 EE. ...
    (microsoft.public.sqlserver.programming)