I have two table's Table A (ID,Name,Location) and Table B(ID , Name)
When we do Union in SQL, Instead of adding any blank location column in TableB, we can perform Union like this
Select ID, name, Location from TableA
Union
Select ID, name, '' as Location from TableB
Here I am dynamically adding column in select statement I am not adding blank column in table B itself.
Can we achieve the same in PowerBI?
You can Append the tables in Power Query. https://learn.microsoft.com/en-us/power-query/append-queries
Related
Is it possible to do something like this:
select name from table1 order by name
union
select name from table2 order by name
I know I can do this:
select name from table1
union
select name from table2 order by name
However, I want the names from table1 to appear first. I have spent the last hour Googling this and I have go nowhere. For example, I have looked here: How to order by with union in SQL?
The query needs to be a bit more complicated:
select name
from ((select distinct name, 1 as is_1 from table1)
union
(select distinct name, 0 from table2)
) n
group by name
order by max(is_1), name;
This uses select distinct in the subqueries because that can take advantage of an index on name.
Add a "sort" field and put the union inside a subquery so you can sort after the union.
untested
select a.name
from (
select name, 1 sort
from table1
union all
select name, 2 sort
from table2
) a
order by a.sort, a.name
I changed it to union all to make it clear this approach won't do a union. You could also select the sort column if you want to see it. If you don't want duplicate names, then this approach won't work.
You need another column to sort on. UNION does not allow the individual queries to have an ORDER BY clause.
Adding in a column to sort on before name allows for it to sort the individual result sets. See my example below:
CREATE TABLE #Table1 (Name VARCHAR(50))
CREATE TABLE #Table2 (Name VARCHAR(50))
INSERT INTO #Table1 VALUES ('Bart'), ('Lisa'), ('Maggie')
INSERT INTO #Table2 VALUES ('Chris'), ('Meg'), ('Stewie')
SELECT Name, 0 AS Sort FROM #Table1
UNION
SELECT Name, 1 AS Sort FROM #Table2
ORDER BY Sort, Name
Aim:
Select all distinct values from three different columns, each located in a different table: [QueryA].Att1, [QueryB].Att2, [QueryC].[Att C].
Return all selected values in a single column in a 4th table: [Table1].Field1
The 'INSERT INTO' line isn't working. The rest of it works (makes query) when 1st line is removed.
The rest of it works (makes query) when 1st line is removed.
INSERT INTO [Table1].Field1
SELECT DISTINCT [QueryA].Att1
FROM [QueryA]
UNION
SELECT DISTINCT [QueryB].Att2
FROM [QueryB]
UNION
SELECT DISTINCT [QueryC].[Att C]
FROM [QueryC];
Error message:
Syntax error in FROM clause.
In MS Access, you need to place UNION in a subquery or entirely separate query to be included in an append. Additionally, period qualifier between table and column should not be used in INSERT INTO:
INSERT INTO [Table1] (Field1)
SELECT Att1
FROM
(SELECT [QueryA].Att1
FROM [QueryA]
UNION
SELECT [QueryB].Att2
FROM [QueryB]
UNION
SELECT [QueryC].[Att C]
FROM [QueryC]
) AS sub
Alternatively, with a separate query:
INSERT INTO [Table1] (Field1)
SELECT Att1
FROM mySavedUnionQuery
could be your insert and select columns don't match (you have more than a col inf table1)
INSERT INTO [Table1] (Field1 )
SELECT [QueryA].Att1
FROM [QueryA]
UNION
SELECT [QueryB].Att2
FROM [QueryB]
UNION
SELECT [QueryC].[Att C]
FROM [QueryC];
and if you use UNION you don't need distinct
I have several tables of which most contain distinct ID columns and most have an additional column containing dates.
Now I need to retrieve all IDs with their corresponding dates from all tables.
Is there a way to output all IDs and dates from all tables at once and without creating duplicates ?
You can use UNION ALL to combine multiple queries:
SELECT 'table_a' source, table_a_id id, creation_date FROM table_a
UNION ALL
SELECT 'table_b' source, table_b_id id, creation_date FROM table_b
UNION ALL
SELECT 'table_c' source, table_c_id id, creation_date FROM table_c;
If you need to remove duplicate rows, you can use UNION instead of UNION ALL, at the cost of some performance.
I have the following problem.
Let TableA(Id int, Name nvarchar(200)) and TableB(Id int, Name nvarchar(200)).
If we run the following query:
SELECT *
FROM
(SELECT *
FROM TableA)
UNION
(SELECT *
FROM TableB)
we get the union of the two datasets.
My Problem is that I want the results of the second dataset to be the ordered by the Name column.
The reason why I need this, is the fact that TableA is a temporary table in my query, that always will hold one record, and this record I want to be the first in the resulting dataset from the union of the two datasets. Also, I want the multiple records of the TableB to be ordered by the Name column.
Unfortunately, when I try to execute the following query
SELECT *
FROM
(SELECT *
FROM TableA)
UNION
(SELECT *
FROM TableB
ORDER BY Name)
I get an ambiguous error message, that informs me that I have an incorrect syntax near the keyword order.
Thanks in advance for any help.
try this:
select id
, name
from
(select 1 as ordercol
, a.id
, a.name
from tableA
union
select 2 as ordercol
, b.id
, b.name
from tableB) i
order by ordercol, name
the error message resulted in you trying to union two subselects. you can put union between two selects that will then be put into a subselect. there is always a select after a union (or union all). i would also suggest you use a union all, that saves time because sql-server will otherwise try and remove records that are in both selects (which in this case is impossible due to the ordercol-column)
i have included a second order-by column that will order the first select before the second. if you order by that first and then by name, you should get the desired result.
I have imported some data from an excel table in to an SQL table and I now need to write a view over the data to combine it with some other fields.
My problem is that the table is in the following form with these columns;
Name Project_One_ID Project_Two_ID Project_Three_ID
Rather than the form I could use, which would be a link table with columns like this;
Name ProjectID
Is it possible to convert this type of table? Or use it as it is? I could do it in code but im struggling in SQL. I have two other tables that need to link on to either side of this link table to create my overall view.
Thanks for any pointers,
You could do it as a UNION and then join to other tables:
SELECT * FROM
(
SELECT Name, Project_One_ID ProjectID
FROM Projects
UNION ALL
SELECT Name, Project_Two_ID ProjectID
FROM Projects
UNION ALL
SELECT Name, Project_Three_ID ProjectID
FROM Projects
) AS P
INNER JOIN ProjectData PD
ON P.ProjectID = PD.ProjectID
Depending on your version of SQL Server you can use CROSS APPLY to UNPIVOT the data.
CROSS APPLY and VALUES will work in SQL Server 2008+:
select name,
ProjectId
from yourtable
cross apply
(
values
('Project_One_ID', Project_One_ID),
('Project_Two_ID', Project_Two_ID),
('Project_Three_ID', Project_Three_ID)
) c (col, ProjectId);
If you are using SQL Server 2005, then you can use CROSS APPLY with UNION ALL:
select name,
ProjectId
from yourtable
cross apply
(
select 'Project_One_ID', Project_One_ID union all
select 'Project_Two_ID', Project_Two_ID union all
select 'Project_Three_ID', Project_Three_ID
) c (col, ProjectId)
if name is a foreign key (unusual - but i suppose possible)
then you could union these things together..
select name, project_one_id as project_id
from newtable
union
select name, project_two_id
from newtable
union
select name, project_three_id
from newtable
The most efficient way to write this query is as an unpivot. This only scans the table once. Here is an example:
with t as (select 'a' as name, 1 as project_one, 2 as project_two, 3 as project_three)
select name, project
from t
unpivot (project for col in (project_one, project_two, project_three)) as unpvt
You can put the unpivot in a view for easy access or use it to create a new table with the columns you want.