sql conditional union on rowcount - sql

I have an SQL function which returns a list of Teams.
I want to join an additional list to that list with a union, but only if first select returns more than one rows.
Something like:
CREATE FUNCTION Teams()
RETURNS TABLE
AS
RETURN
(
SELECT * FROM TABLE1
if ##rowcount>1
UNION
SELECT * FROM TABLE2
end if
)

Not pretty but this should work:
CREATE FUNCTION Teams()
RETURNS TABLE
AS
RETURN
(
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2 WHERE EXISTS (SELECT * FROM TABLE1)
)
If the select from the first table is complicated you could put it into a CTE:
CREATE FUNCTION Teams()
RETURNS TABLE
AS
RETURN
(
WITH Result AS
(
SELECT * FROM TABLE1 WHERE ComplicatedConditions = 1
)
SELECT * FROM Result
UNION
SELECT * FROM TABLE2 WHERE EXISTS (SELECT * FROM Result)
)

If I understand correctly
CREATE FUNCTION Teams()
RETURNS TABLE
AS
RETURN
(
if (select COUNT(*) from TABLE2)>1
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
else
SELECT * FROM TABLE1
)

One way would be to do:
IF EXISTS(SELECT 1 FROM TABLE1)
SELECT * FROM TABLE1
UNION
SELECT * FROM TABLE2
ELSE
SELECT * FROM TABLE1

Related

Save a Select/Except Union into a Temp Table

This code does precisely what I want: finds the difference between two tables, including nulls, and returns them. Thanks to: sql query to return differences between two tables
(
SELECT * FROM table1
EXCEPT
SELECT * FROM table2
)
UNION ALL
(
SELECT * FROM table2
EXCEPT
SELECT * FROM table1
)
I am having trouble getting this to turn into a temporary table (or even a regular table) to store its results for later use. Is there a way that I can tack on INSERT INTO here or generate a temp table from this beautiful query?
Select from your existing query as a sub-query INTO the temp table of your choice.
SELECT *
INTO #temp1
FROM (
(
SELECT * FROM #table1
EXCEPT
SELECT * FROM #table2
)
UNION ALL
(
SELECT * FROM #table2
EXCEPT
SELECT * FROM #table1
)
) X

Run UNION SELECT only if the fist select is not empty

I have the following stored procedure that returns rows for an endless scroll page. How can I check that newTable is empty? If it's empty then I don't want the UNION SELECT to be executed. Note that newTable is a very complex query so I don't want to execute more than once.
#offset INT,
#fetch INT
WITH newTable AS
(
SELECT * FROM table1
ORDER BY id OFFSET #offset ROWS FETCH NEXT #fetch ROWS ONLY
)
Don't run the following if newTable has reached the last row and is empty
SELECT * FROM newTable
UNION
SELECT * FROM table3
UNION
SELECT * FROM table4
ORDER BY id
You can probably do something like this:
DECLARE #offset INT,
#fetch INT;
WITH newTable AS
(
SELECT * FROM table1
ORDER BY id OFFSET #offset ROWS FETCH NEXT #fetch ROWS ONLY
)
SELECT *
FROM newTable
UNION
SELECT *
FROM table3
WHERE EXISTS (SELECT 1 FROM newTable)
UNION
SELECT *
FROM table4
WHERE EXISTS (SELECT 1 FROM newTable)
ORDER BY id
I'm pretty sure it will not execute the cte again. If it does, you can use a temporary table instead of a cte:
SELECT <ColumnsList> INTO #TemporaryTable
FROM....
and then your UNION query.
SELECT *
FROM #TemporaryTable
UNION
SELECT *
FROM table3
WHERE EXISTS (SELECT 1 FROM #TemporaryTable)
UNION
SELECT *
FROM table4
WHERE EXISTS (SELECT 1 FROM #TemporaryTable)
ORDER BY id

Choose which query will be used / Conditional query in Oracle

I have two queries, query1 and query2. What I would like to do is, if returned rows of query1 is empty, return query2 instead. Is that possible using basic SQL query alone? They have same returning columns btw, but different table sources.
eg:
query1:
SELECT name, message
FROM table1
query2:
SELECT name, message
FROM table 2
If query1 is empty, return name, message from query2.
This will select from table1 if not empty, otherwise select from table2:
SELECT * FROM table1
WHERE EXISTS (select * from table1)
UNION ALL
SELECT * FROM table2
WHERE NOT EXISTS (select * from table1)
This checks if table1 has no rows:
EXISTS (SELECT * FROM TABLE)
Found an answer here: https://stackoverflow.com/a/25122516/3747493
Basically:
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2
WHERE (SELECT COUNT(*) FROM table1) = 0
Thanks guys!

Count rows in more than one table with tSQL

I need to count rows in more than one table in SQL Server 2008. I do this:
select count(*) from (select * from tbl1 union all select * from tbl2)
But it gives me an error of incorrect syntax near ). Why?
PS. The actual number of tables can be more than 2.
In case you have different number of columns in your tables try this way
SELECT count(*)
FROM (
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
) T
try this:
You have to give a name to your derived table
select count(*) from
(select * from tbl1 union all select * from tbl2)a
I think you have to alias the SELECT in the FROM clause:
select count(*)
from
(
select * from tbl1
union all
select * from tbl2
) AS SUB
You also need to ensure that the * in both tables tbl1 and tbl2 return exactly the same number of columns and they have to be matched in their type.
I don't like doing the union before doing the count. It gives the SQL optimizer an opportunithy to choose to do more work.
AlexK's (deleted) solution is fine. You could also do:
select (select count(*) from tbl1) + (select count(*) from tbl2) as cnt

SQL conditional union

Question: I have an SQL function which returns a list of files
now I should join an additional list to that list with an union, but only if the user is admin.
Is that possible? Something like:
CREATE FUNCTION tfu_CMS_Process(#bIsAdmin bit )
-- Add the parameters for the function here
RETURNS TABLE
AS
RETURN
(
SELECT * FROM TABLE1
if bIsAdmin
UNION ALL
SELECT * FROM TABLE2
end if
)
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2
WHERE #isAdmin = 1