I have two fields, tblIT.Person_Name and tblEng.Full_Name. I want to create a new column that contains all of the names from the first table and adds them to the second column and places it all into one main column
Person_name
-------------
John Smith |
-------------
Gary Porter |
Full_name
-------------
Gary Porter |
-------------
Nancy Becker|
I am looking for this:
People
-------------
Gary Porter |
-------------
Nancy Becker|
-------------
John Smith |
It is not concatenating, which a lot of people have been telling me, it is simply joining the two tables.
Create a UNION of the 2 tables. This will weed out duplicate values.
SELECT Person_name AS People FROM tblIT
UNION
SELECT Full_Name FROM tblEng
You would use UNION ALL if you wanted all the rows from both tables, including the duplicates. But it looks like you don't want duplicates.
The result set will not be editable.
If you want to store those in a column in another table, you can insert the results from the UNION query. The UNION keyword may cause a syntax error with a simple INSERT statement, so include the UNION as a subquery in the INSERT.
INSERT INTO YourTable (People)
SELECT sub.People
FROM
(
SELECT Person_name AS People FROM tblIT
UNION
SELECT Full_Name FROM tblEng
) AS sub
use UNION (without the ALL because it will allow duplicate) to merge all the results.
SELECT col1 FROM Person_name
UNION
SELECT col1 FROM Full_name
It's combining the results, not joining the results :)
Related
I have a table that contains data like below:
Name
ID
Dept
Joe
1001
Accounting
Joe
1001
Marketing
Mary
1003
Administration
Mary
1009
Accounting
Each row is uniquely identified with a combo of Name and ID. I want the resulting table to combine rows that have same Name and ID and put their dept's together separated by a comma in alpha order. So the result would be:
Name
ID
Dept
Joe
1001
Accounting, Marketing
Mary
1003
Administration
Mary
1009
Accounting
I am not sure how to approach this. So far I have this, which doesn't really do what I need:
SELECT Name, ID, COUNT(*)
FROM employees
GROUP BY Name, ID
I know COUNT(*) is irrelevant here, but I am not sure what to do. Any help is appreciated! By the way, I am using PostgreSQL and I am new to the language.
Apparently there is an aggregate function for string concatenation with PostgreSQL. Find documentation here. Try the following:
SELECT Name, ID, string_agg(Dept, ', ' ORDER BY Dept ASC) AS Departments
FROM employees
GROUP BY Name, ID
I want to select from a table where a name appears twice.
For example I have a table like this,
ID Name
---- ------
1 Jane John
2 Kevin Smith
3 Jane John
What I want is for the output to show where Jane John appear twice so it should look something like this:
ID Name
---- ------
1 Jane John
3 Jane John
I tried looking around on stackoverflow but couldn't find an exact and easy answer.
I'm using oracle SQL Developer.
You ask for a record that appears twice. If a row appears three times it won't show unless you modify the having clause as commented.
SELECT id
,NAME
FROM tablen
WHERE NAME IN (
SELECT NAME
FROM TableN n
GROUP BY (NAME)
HAVING counT(NAME) = 2 --Use >1 instead of =2 for more than one record
)
EDIT
I'll add a new solution in regard to your last comment.
As you can only ask for one field in IN() I'll use a special character or string making sure it does not belongs to valid values in any field.
Look at this: http://sqlfiddle.com/#!6/2af55/3
SELECT id
,NAME
,name2
FROM tablen
WHERE concat(NAME,'=',name2) IN (
SELECT concat(NAME,'=',name2)
FROM TableN n
GROUP BY concat(NAME,'=',name2)
HAVING count(concat(NAME,'=',name2)) = 2
)
Note I wrote this thinking in SQL Server, not sure if concat function works as well in Oracle or look for an alternative.
I'm trying to write an oracle SQL query that returns a single column containing values from multiple columns.
I have a table named CLIENT
clientid firstname Lastname
1 Steve Smith
2 James Hill
I want to return a single column "ALL" like so:
ALL
1
2
Steve
James
Smith
Hill
Is there a simple way to write this query?
This involves UNION
SELECT ClientID AS [All]
FROM Client
UNION ALL
SELECT FirstName
FROM Client
UNION ALL
SELECT LastName
FROM Client
I have a table: tblperson
There are two columns in tblperson:
person1 person2
Anna Sarah
Louis Mike
Kisha
I want to add the second column (person2) below the first column (person1) to create only one column (persons)
persons
Anna
Louis
Kisha
Sarah
Mike
I've tried using UNION but it would make my query large especially when there are more than two columns involved
Can anyone give hints to query for generating these records?
Thank you
You would do this using union all, but also with order by:
select person
from (select person1 as person, 1 as which from tblperson union all
select person2 as person, 2 from tblperson
) p
order by which;
The order by is pretty important. It is true that you can simply do:
select person1 as person from tblperson union all
select person2 as person from tblperson
However, I consider this a little dangerous, because SQL standards do not guarantee that the first subquery is executed before the second. In practice, SQL Server does execute these in order, but there is no ongoing guarantee.
I'm selecting data from multiple tables and I also need to get maximum "timestamp" on those tables. I will need that to create custom cache control.
tbl_name tbl_surname
id | name id | surname
--------- ------------
0 | John 0 | Doe
1 | Jane 1 | Tully
... ...
I have following query:
SELECT name, surname FROM tbl_name, tbl_surname WHERE tbl_name.id = tbl_surname.id
and I need to add following info to result set:
SELECT MAX(ora_rowscn) FROM (SELECT ora_rowscn FROM tbl_name
UNION ALL
SELECT ora_rowscn FROM tbl_surname);
I was trying to use UNION but I get error - mixing group and not single group data - or something like that, I know why I cannot use the union.
I don't want to split this into 2 calls, because I need the timestamp of the current snapshot I took from DB for my cache management. And between select and the call for MAX the DB could change.
Here is result I want:
John | Doe | 123456
Jane | Tully | 123456
where 123456 is approximate time of last change (insert, update, delete) of tables tbl_name and tbl_surname.
I have read only access to DB, so I cannot create triggers, stored procedures, extra tables etc...
Thanks for any suggestions.
EDIT: The value *ora_rowscn* is assigned per block of rows. So in one table this value can differ per row. I need the maximal value from both (all) tables involved in query.
Try:
SELECT name,
surname,
max(greatest(tbl_name.ora_rowscn, tbl_surname.ora_rowscn)) over () as max_rowscn
FROM tbl_name, tbl_surname
WHERE tbl_name.id = tbl_surname.id
There's no need to aggregate here - just include both ora_rowscn values in your query and take the max:
SELECT
n.name,
n.ora_rowscn as n_ora_rowscn,
s.surname,
s.ora_rowscn as s_ora_rowscn,
greatest(n.ora_rowscn, s.ora_rowscn) as last_ora_rowscn
FROM tbl_name n
join tbl_surname s on n.id = s.id
BTW, I've replaced your old-style joins with ANSI style - better readable, IMHO.