How to alias union of three sets? - sql-server

I need alias union of three query like this:
select * from
(
select * from A
union
select * from B
union
select * from c)t1
My code appear error.
I don't know how solve it.
Please help me

First, instead of using SELECT *, specifically name your columns; that's probably where your error lies. One or more of your tables (A, B, or C) probably has more or less columns than the others.
SELECT col1, col2
FROM ( SELECT col1, col2
FROM A
UNION
SELECT col1, col2
FROM B
UNION
SELECT col1, col2
FROM c
) t1
If that doesn't fix it, then post the error message you received, and the RDBMS you are using.

Related

TSQL Conditional Select Statement

Is it possible to write a SQL query that does the following:
Select * From Table1
if there are results, return them. Otherwise, use an alternative query and return it's results:
Select * From Table2
I came ups with the following, but it does not seem to work:
IF EXISTS(select * From TableA)
begin
Select * from TableA
end
else
Select * from TableB
Is there a simple elegant way of accomplishing this?
You can do a UNION query with NOT EXISTS:
SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2 WHERE NOT EXISTS (SELECT * FROM TABLE1)
*Assuming the columns and types are the same
If both tables have same number of columns and the column names are same then you use Union, like below.
Select * from TableA
Union
Select * from TableB
If they have different column names or different number of columns then you can use below code.
Select Col_1 as col1, col_2 as col2.... From TableA
Union
Select col_a as Col1, col_b as Col2..... From TableB

How to UNION parts of different sql

I have 4 parts, each part is made out of 2 select statement. I need the 1st 2 part to be UNION'd with the 2nd part. Each part contains its own ORDER BY. I need the results to appear 1st part first and followed by the 2nd part. Only each part is sorted but not the overall result set.
(select col1,col2,col3,col4,col5 from tableA where col1 = 'x'
UNION ALL
select col1,col2,col3,col4,col5 from tableB where col1 = 'x'
ORDER BY Col3) --1st part ends here
--now I need to UNION the 2nd part
UNION ALL
(select col1,col2,col3,col4,col5 from tableA where col1 <> 'x'
UNION ALL
select col1,col2,col3,col4,col5 from tableB where col1 <> 'x'
ORDER BY Col3) --2nd part ends here
I tried wrapping each select in the SELECT * FROM (... but I'm having issues with ORDER BY. Just can't seem to get the syntax right.
Just add a column for this purpose:
select col1,col2,col3,col4,col5, ord = 1 from tableA where col1 = 'x'
UNION ALL
select col1,col2,col3,col4,col5, ord = 1 from tableB where col1 = 'x'
UNION ALL
select col1,col2,col3,col4,col5, ord = 2 from tableA where col1 <> 'x'
UNION ALL
select col1,col2,col3,col4,col5, ord = 2 from tableB where col1 <> 'x'
ORDER BY ord, Col3
The ORDER BY applies over the whole result set, after the UNION. See Example C: https://msdn.microsoft.com/en-us/library/ms180026%28v=sql.110%29.aspx
You can't have the ORDER BY in the queries within the UNION.

How to filter rows by values of one column?

I need to get several columns form sql query. Then I have to filter this answer by the "distinct" values of one column, but in the output I need to have all columns, not only this which values has to be distinct. Can anybody help me? Order by clause is not an answer for me.
A,B,C,D
E,F,G,H
I,J,C,L
M,N,Z,H
Above is a simple rows output. Please have a look onto 3rd column. Let's assume that we don't know how many rows do we have. I need to select only rows which has distinct value in 3rd column. (C,G,Z) - We need to filter anyone from "C" rows.
I've arbitrarily chosen to use col1 to break ties on col3. You can adjust the order by portion of the partition to suit your needs.
/* Set up test data */
declare #test table (
col1 char(1),
col2 char(1),
col3 char(1),
col4 char(1)
)
insert into #test
(col1, col2, col3, col4)
select 'A','B','C','D' union all
select 'E','F','G','H' union all
select 'I','J','C','L' union all
select 'M','N','Z','H'
/* Here's the query */
;with cteRowNumber as (
select col1, col2, col3, col4,
row_number() over (partition by col3 order by col1) as RowNumber
from #test
)
select col1, col2, col3, col4
from cteRowNumber
where RowNumber = 1
Returns
col1 col2 col3 col4
----------------------------
A B C D
E F G H
M N Z H
ROLL UP or CUBE could be helpful for your problem, since they can aggregate (i.e. subtotal) data based on the GROUP BY and still return the individual rows.

How to add order by for Union result?

In SQL Server 2008, we can use Union/Unino All to put two results together, but I want to add order by for the final result. How can do that?
What I want is something like:
select id1 as id, * from ...
Union All
select id2 as id, * from ...
order by id
Help please. Thanks.
As noted in the comments, the pseudocode you've given should work, applying the order by to the combined result set. If you're trying to order in such a way that the two result sets are kept distinct, you'd need to introduce an artificial sort column, such as:
select id1 as id, *, 1 as MySortKey from ...
Union All
select id2 as id, *, 2 as MySortKey from ...
order by MySortKey, id
You can use this query as well
select * from (
select id1 as id, * from ...
Union All
select id2 as id, * from ...
) aaaa
order by id
use this code:
Select * from
(select id1 as id, * from ...
Union All
select id2 ad id, * from ...
order by id) as t
order by t.id
use northwind
select OrderID, ProductID from [Order Details] a
UNION all
Select OrderID, EmployeeID from Orders b
order by a.ProductID
All of these answers included the original poster's attempt is just plain confusing or overly complicated. A simple and easy to follow explanation is here:
How to order by with union
with,
"Just write
Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like "%a%"
Order by name
the order by is applied to the complete resultset
"
Just use UNION ALL instead of UNION here.

How to reduce 10+ union select statements

I need to extract a single column of data from 10+ columns of a single table. Instead of stacking up 10+ union select statements, is there another way of doing this without repeating the union select statements for each column?
I can get it by stacking up 10+ select statements like below:
select 'column_5' from table_a
union
select 'column_6' from table_a
union
select 'column_7' from table_a
union
.
.
.
union
select 'column_18' from table_a
Thanks for your time in advance :)
Using Cross Apply or UnPivot you can avoid multiple UNION statements
select Distinct COL
from table
Cross apply
(
values
(column_1),
(column_2),
..
..
(column_18)
)
CS (COL)
Note : Since you have used UNION i have kept Distinct in select. If you don't want to remove duplicates then remove Distinct from select
A query with UNPIVOT would look something like this....
SELECT *
FROM TableName t
UNPIVOT (Vals FOR N IN (Column1, Column2, Column3,....,Column10))up
Important note all the columns in IN clause must be of the same data type, if they are not use a sub-query to convert them to a uniform data type and then unpivot them something like...
SELECT *
FROM (
SELECT CAST(Column1 AS VARCHAR(200)) AS Column1
,CAST(Column2 AS VARCHAR(200)) AS Column2
,CAST(Column3 AS VARCHAR(200)) AS Column3
,.....
,CAST(Column10 AS VARCHAR(200)) AS Column10
FROM TableName) t
UNPIVOT (Vals FOR N IN (Column1, Column2, Column3,....,Column10))up

Resources