How do I process data in Multivalued Column in Oracle PLSQL? - sql

I am working on creating a small database in Oracle for a project at work. One of the columns will need to have multiple values recorded in it. What's the query to create a multivalued column?

If you need a user to enter multiple email addresses, I would consider creating a USER_EMAIL table to store such records.
Create Table User_Email (User_Id int, Email varchar(100));
User_Id would be a foreign key that goes back to your USER table.
Then you can have a 1-n number of email address per user. This is generally the best practice for database normalization. If your emails have different types (ie work, personal, etc.), you could have another column in that table for the type.
If you need to return the rows in a single column, you could then look at using LISTAGG:
select u.id,
listagg(ue.email, ', ') within group (order by ue.email) email_addresses
from users u
left join user_email ue on u.id = ue.user_id
group by u.id
SQL Fiddle Demo

You can try to use VARRAY columns in a Oracle column.
Look at this page: https://www.orafaq.com/wiki/VARRAY
You can see there:
Declaration of a type:
CREATE OR REPLACE TYPE vcarray AS VARRAY(10) OF VARCHAR2(128);
Declaration of a table:
CREATE TABLE varray_table (id number, col1 vcarray);
Insertion:
INSERT INTO varray_table VALUES (3, vcarray('D', 'E', 'F'));
Selection:
SELECT t1.id, t2.column_value
FROM varray_table t1, TABLE(t1.col1) t2
WHERE t2.column_value = 'A' OR t2.column_value = 'D'

Related

Why do i receive 'Duplicate column name' error when innerjoin-ing 2 tables with similar column name?

CREATE TABLE student_activestudent AS
(
SELECT *
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
I am expecting a table with 2 columns of studentnumber but I received Duplicate error instead --> Duplicate column name 'studentnumber'
A database table must have unique column names.
When you do select * you will get all columns from all tables and studentnumber exists on both student table and activestudent table. So to solve you problem specify the columns you want instead of *
CREATE TABLE student_activestudent AS
(
SELECT
student.studentnumber,
..Other columns..
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
At the very least, studentnumber is duplicated. In general, I strongly recommend that a view list all the columns explicitly. This protects the view if underlying columns change.
That said, if studentnumber is the only column, then you can do:
CREATE TABLE student_activestudent AS
SELECT *
FROM student s JOIN
activestudent ast
USING (studentnumber);
With using, the * does not repeat the join keys.
You cannot select two tables that have same column's name.
The best way is not to select *
Select by column and if the column is same you can put [as]
Example
SELECT student.studentnumber as stuNumber, activestudent.studentnumber as actstuNumber

Call function that returns table in a view in SQL Server 2000

SQL Server - Compatibility Level 2000
Person table - PersonId, PersonName, etc.. (~1200 records)
Two user functions - GetPersonAddress(#PersonId), GetPaymentAddress(#PersonId)
These two functions return data in a table with Street, City etc...(one record in the return table for the PersonId)
I have to create a view that joins the person table with these two user functions by passing in the person id.
Limitations:
Cross Apply is not supported on a function in SQL Server 2000
Cursor, temp table and temp variables are not supported in views so that I can loop upon the person table and call the functions.
Can someone help?
You could create functions GetPeopleAddresses() and GetPaymentsAddresses() which return PersonId as a field and then you can use them in JOIN:
SELECT t.PersonId, PersonName, etc..., a1.Address, a2.Address
FROM YourTable t
LEFT JOIN GetPeopleAddresses() a1 ON a1.PersonId = t.PersonId
LEFT JOIN GetPaymentsAddresses() a2 ON a2.PersonId = t.PersonId
Of course, your functions have to return only unique records
I'm afraid that you can't do that with a view in SQL Server 2000 because of the limitations you listed. The next best option as suggested in the comments is a stored procedure that returns the rows that would return the view.
If you need to use the results of the procedure in another query, you can insert the values returned by the procedure in a temporal table. Is not pretty, and you have to make two DB calls (one for creating/populating the temp table, and the other for using it), but it works. For example:
create table #TempResults (
PersonID int not null,
Name varchar(100),
Street varchar(100),
City varchar(100),
<all the other fields>
constraint primary key PK_TempResults (PersonID)
)
insert into #TempResults
exec spTheProcedureThatReplaceTheView #thePersonID
go -- end of the first DB call
select <fields>
from AnotherTable
join #TempResults on <condition>
-- don't forget to drop table when you don't need its current data anymore
drop table #TempResults

PostgreSQL: Get last updates by joining 2 tables

I have 2 tables that I need to join to get the last/latest update in the 2nd table based on valid rows in the 1st table.
Code below is en example.
Table 1: Registered users
This table contains a list of users registered in the system.
When a user gets registered it gets added into this table. A user is registered with a name, and a registration time.
A user can get de-registered from the system. When this is done, the de-registration column gets updated to the time that the user was removed. If this value is NULL, it means that the user is still registered.
CREATE TABLE users (
entry_idx SERIAL PRIMARY KEY,
name TEXT NOT NULL,
reg_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
dereg_time TIMESTAMP WITH TIME ZONE DEFAULT NULL
);
Table 2: User updates
This table contains updates on the users. Each time a user changes a property (example position) the change gets stored in this table. No updates must be removed since there is a requirement to keep history in the table.
CREATE TABLE user_updates (
entry_idx SERIAL PRIMARY KEY,
name TEXT NOT NULL,
position INTEGER NOT NULL,
time TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
Required output
So given the above information, I need to get a new table that contains only the last update for the current registered users.
Test Data
The following data can be used as test data for the above tables:
-- Register 3 users
INSERT INTO users(name) VALUES ('Person1');
INSERT INTO users(name) VALUES ('Person2');
INSERT INTO users(name) VALUES ('Person3');
-- Add some updates for all users
INSERT INTO user_updates(name, position) VALUES ('Person1', 0);
INSERT INTO user_updates(name, position) VALUES ('Person1', 1);
INSERT INTO user_updates(name, position) VALUES ('Person1', 2);
INSERT INTO user_updates(name, position) VALUES ('Person2', 1);
INSERT INTO user_updates(name, position) VALUES ('Person3', 1);
-- Unregister the 2nd user
UPDATE users SET dereg_time = NOW() WHERE name = 'Person2';
From the above, I want the last updates for Person 1 and Person 3.
Failed attempt
I have tried using joins and other methods but the results are not what I am looking for. The question is almost the same as one asked here. I have used the solution in answer 1 and it does give the correct answer, but it takes too long to get too the answer in my system.
Based on the above link I have created the following query that 'works':
SELECT
t1.*
, t2.*
FROM
users t1
JOIN (
SELECT
t.*,
row_number()
OVER (
PARTITION BY
t.name
ORDER BY t.entry_idx DESC
) rn
FROM user_updates t
) t2
ON
t1.name = t2.name
AND
t2.rn = 1
WHERE
t1.dereg_time IS NULL;
Problem
The problem with the above query is that it takes very long to complete. Table 1 contains a small list of users, while table 2 contains a huge amount of updates. I think that the query might be inefficient in the way that it handles the 2 tables (based on my limited understanding of the query). From pgAdmin's explain it does a lot of sorting and aggregation on the updates 1st before joining with the registered table.
Question
How can I formulate a query to efficiently and fast get the latest updates for registered users?
PostgreSQL have a special distinct on syntax for such type of queries:
select distinct on(t1.name)
--it's better to specify columns explicitly, * just for example
t1.*, t2.*
from users as t1
left outer join user_updates as t2 on t2.name = t1.name
where t1.dereg_time is null
order by t1.name, t2.entry_idx desc
sql fiddle demo
you can try it, but for me your query should work fine too.
I am using q1 to get the last update of each user. Then joining with users to remove entries that have been deregistered. Then joining with q2 to get rest of user_update fields.
select users.*,q2.* from users
join
(select name,max(time) t from user_updates group by name) q1
on users.name=q1.name
join user_updates q2 on q1.t=q2.time and q1.name=q2.name
where
users.dereg_time is null
(I haven't tested it. have edited some things)

SQL query to select * from table, but return ID column as looked-up usernames

I'm using Bugzilla, and I essentially want to SELECT * FROM bugs table in the "bugs" database. However, the "assigned_to" column actually contains integer values (IDs) instead of a string with the user name.
These IDs match primary keys in the "profiles" table (the "userid" column), and the string I want my query to return is actually stored in the "realname" column in that table.
How can I modify this query to capture all columns in "bugs," but perform a lookup on the assigned_to column and return usernames?
SELECT b.*, p.realname FROM bugs b
JOIN profiles p
ON b.assigned_to = p.userid

Select from multiple tables with different columns

Say I got this SQL schema.
Table Job:
id,title, type, is_enabled
Table JobFileCopy:
job_id,from_path,to_path
Table JobFileDelete:
job_id, file_path
Table JobStartProcess:
job_id, file_path, arguments, working_directory
There are many other tables with varying number of columns and they all got foreign key job_id which is linked to id in table Job.
My questions:
Is this the right approach? I don't have requirement to delete anything at anytime. I will require to select and insert mostly.
Secondly, what is the best approach to get the list of jobs with relevant details from all the different tables in a single database hit? e.g I would like to select top 20 jobs with details, their details can be in any table (depends on column type in table Job) which I don't know until runtime.
select (case when type = 'type1' then (select field from table1) else (select field from table2) end) as a from table;
Could it be a solution for you?