I have two recordsets from a single table:
SELECT * FROM userconfig WHERE userid = 'user1'
AND
SELECT * FROM userconfig WHERE userid = 'user2'
I would like to update or merge (whatever is easier) one column from a specific user's recordset into the recordset of the other user. IE: user1 has a column configvalue whose values I want to insert into user2's configvalue column. I need both columns to have the same value. What is the simplest way to achieve this?
Update
user2.userkey = user1.userkey,
user2.uservalue = user1.uservalue
FROM
MyTable user2 INNER JOIN MyTable user1 ON user1.YourKey = user2.YourKey
The same table, MyTable (replace with your table name). Join this table back to itself based on the YourKey column (replace YourKey with the column where the join matches).
Then simply assign the alias values of user1 into user2. Aliases and joining back to the same table is the key.
Related
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
I have table A with an id column and a bunch of others. I also have table B which has an id column like table A and also another column called countries. However, not all id in table A are in table B. I want to insert a new column into table A called countries2 that basically inserts the countries from table B that corresponds to the ids in both tables. If a specific id from A does not exist in B, then it always puts in the VARCHAR 'none' into countries2.
So for example if Table A has the ids 1, 2, 3, 4 and table B looks like:
id--country
1---'foo1'
3---'foo2'
I want table A to become something like:
id--country2--other original data from table A
1---'foo1'--...
2---'none'--...
3---'foo2'--...
4---'none'--...
You need to first alter your TableA to add an extra column (i.e yourNewColumn) of type varchar/varchar2
to add the column try something like
ALTER TABLE TableA ADD COLUMN yourNewColumn varchar(10);
Then you can use something like below to update TableA
UPDATE TableA
SET yourNewColumn = ISNULL(TableB.countries, 'none')
FROM TableA
LEFT JOIN TableB ON TableA.id = TableB.id
The ISNULL function in PostgreSQL might be something like
SET yourNewColumn = CASE WHEN TableB.countries IS NULL THEN 'none' ELSE TableB.countries END
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'
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
We are using audit tables for each operational table, which stores the previous value of its operational equivalent plus change date, change type (UPDATE or DELETE) and its own auto incremental Primary Key.
So, for a table Users with columns UserID, Name, Email there would be a table xUsers with columns ID, OpererationType, OperationDate, UserID, Name, Email.
See that the xTable contains every column that its 'parent' does with 3 extra fields. This pattern is repeated for all tables used by our system.
table Users:
UserID int
Name nvarchar
Email nvarchar
table xUsers:
xUserID int
OpererationType int
OperationDate datetime
UserID int
Name nvarchar
Email nvarchar
Now, my question:
If I have a certain UserID, for which there is 2 entries in the xUsers table when the email was changed twice,
how would I construct a query that identifies which columns (can be more than 1) differ between the two rows in the audit table?
If I'm understanding this correctly, you'd like to create a query passing in the UserID as a parameter, which I'll call #UserID for the following example.
This query will select all rows from xUsers joined onto itself where there is a difference in a non-UserID column, using a series of case statements (one per column) to pull out specifically which columns differ.
SELECT *
, CASE
WHEN a.OperationType <> b.OperationType
THEN 1
ELSE 0
END AS OperationTypeDiffers
, CASE
WHEN a.OperationDate <> b.OperationDate
THEN 1
ELSE 0
END AS OperationDateDiffers
FROM xUsers a
JOIN xUsers b
ON a.xUserID < b.xUserID
AND a.UserID = b.UserID
AND (a.OperationType <> b.OperationType
OR a.OperationDate <> b.OperationDate) -- etc.
WHERE a.UserID = #UserID
You can put the rows of xUsers in a temporary table and then make a while cycle to go for each one and compare the results.
OR
You can do some dynamic SQL and use sysobjects and syscolumns tables to compare each result. It would be more dynamic and then it would be easy to implement for other tables.