Insert Into with multiple columns and multiple values in SQL - sql

INSERT INTO MANAGING(SUPID, EMPID)
VALUES (76, 1, 4, 8);
currently this is the code I have, I just simply want to have the 1,4,8 go into the EMPID as three separate values.
Any help would be great, thanks!

Insert them using separate scripts.
Simply try this.
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 1);
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 4);
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 8);

Depending on your db engine, you can do it also using
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 1),(76, 4),(76, 8);

if you have emp table
INSERT INTO MANAGINS(SUPID,EMPID) SELECT 76, ID FROM EMP_TABLE

Related

How to select distinct multi-column values in Oracle SQL?

I am trying to get distinct values with multi column select.
Sample table:
CREATE TABLE DUP_VALUES (ID NUMBER, NAME VARCHAR2(64));
INSERT INTO DUP_VALUES values (1, 'TEST1');
INSERT INTO DUP_VALUES values (2, 'TEST1');
INSERT INTO DUP_VALUES values (3, 'TEST2');
INSERT INTO DUP_VALUES values (4, 'TEST2');
INSERT INTO DUP_VALUES values (5, 'TEST1');
INSERT INTO DUP_VALUES values (6, 'TEST1');
INSERT INTO DUP_VALUES values (7, 'TEST1');
I want to get
ID NAME
1 TEST1
3 TEST2
I tried with SELECT DISTINCT ID, NAME FROM DUP_VALUES
But, I got all values, because ID is unique.
Use aggregation:
select min(id) as id, name
from dup_values
group by name;

Sample observations per group without replacement in SQL

Using the provided table I would like to sample let's say 2 users per day so that users assigned to the two days are different. Of course the problem I have is more sophisticated, but this simple example gives the idea.
drop table if exists test;
create table test (
user_id int,
day_of_week int);
insert into test values (1, 1);
insert into test values (1, 2);
insert into test values (2, 1);
insert into test values (2, 2);
insert into test values (3, 1);
insert into test values (3, 2);
insert into test values (4, 1);
insert into test values (4, 2);
insert into test values (5, 1);
insert into test values (5, 2);
insert into test values (6, 1);
insert into test values (6, 2);
The expected results would look like this:
create table results (
user_id int,
day_of_week int);
insert into results values (1, 1);
insert into results values (2, 1);
insert into results values (3, 2);
insert into results values (6, 2);
You can use window functions. Here is an example . . . although the details do depend on your database (functions for random numbers vary by database):
select t.*
from (select t.*, row_number() over (partition by day_of_week order by random()) as seqnum
from test t
) t
where seqnum <= 2;

PostgreSQL Inserting 2 relationships at once

I have 3 tables professionals, services, and feature_professional I need to insert data into when inserting a professional. This is the sql I've tried.
WITH professionals as (INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *)
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut'),
INSERT INTO feature_professional(id, professional_id, feature_id, enabled)
VALUES
(1, (select professionals.id from professionals), 1, true)
I've also tried different variations of this wrapping all the insert statements inside parenthesis but still no luck. The error I'm getting is a syntax error.
ERROR: syntax error at or near "INSERT"
LINE 106: INSERT INTO feature_professional
What is the correct syntax to accomplish this? I know that if I take out the third insert statement the query works. For example:
WITH professionals as (INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *)
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut')
You cant do two insert after a cte, so wrap each one in a different cte
SQL DEMO
WITH professionals as (
INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *
),
services as (
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut')
)
INSERT INTO features(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'autobook');
select * from professionals;
select * from services;
select * from features;
INSERT INTO feature_professional (id, professional_id, feature_id, enabled)
select ?, professionals.id from professionals, ?, ?
Provided that the question marks are values you can insert. If they aren't required you can just omit them.
INSERT INTO feature_professional (professional_id)
select professionals.id from professionals
Here is how to insert from a select instead of static values:
https://www.w3schools.com/sql/sql_insert_into_select.asp

How to insert with loops in postgresql

I am trying to insert a number of rows to a table where values came from a select query
insert into article_rel (x_id, y_id) VALUES (12, (SELECT id from table where name = 'string')
this should be the same as executing this
insert into article_rel (x_id, y_id) VALUES (12, 1)
insert into article_rel (x_id, y_id) VALUES (12, 3)
insert into article_rel (x_id, y_id) VALUES (12, 4)
and so on.
I saw this How to use a SQL for loop to insert rows into database? but I'm not sure how can this help me
Thanks in advance,
Use this query:
INSERT INTO article_rel (x_id, y_id)
SELECT 12, id
FROM table_name
WHERE name = 'string'

Oracle PIVOT, twice?

I have been trying to move away from using DECODE to pivot rows in Oracle 11g, where there is a handy PIVOT function. But I may have found a limitation:
I'm trying to return 2 columns for each value in the base table. Something like:
SELECT somethingId, splitId1, splitName1, splitId2, splitName2
FROM (SELECT somethingId, splitId
FROM SOMETHING JOIN SPLIT ON ... )
PIVOT ( MAX(splitId) FOR displayOrder IN (1 AS splitId1, 2 AS splitId2),
MAX(splitName) FOR displayOrder IN (1 AS splitName1, 2 as splitName2)
)
I can do this with DECODE, but I can't wrestle the syntax to let me do it with PIVOT. Is this even possible? Seems like it wouldn't be too hard for the function to handle.
Edit: is StackOverflow maybe not the right Overflow for SQL questions?
Edit: anyone out there?
From oracle-developer.net it would appear that it can be done like this:
SELECT somethingId, splitId1, splitName1, splitId2, splitName2
FROM (SELECT somethingId, splitId
FROM SOMETHING JOIN SPLIT ON ... )
PIVOT ( MAX(splitId) ,
MAX(splitName)
FOR displayOrder IN (1 AS splitName1, 2 as splitName2)
)
I'm not sure from what you provided what the data looks or what exactly you would like. Perhaps if you posted the decode version of the query that returns the data you are looking for and/or the definition for the source data, we could better answer your question. Something like this would be helpful:
create table something (somethingId Number(3), displayOrder Number(3)
, splitID Number(3));
insert into something values (1, 1, 10);
insert into something values (2, 1, 11);
insert into something values (3, 1, 12);
insert into something values (4, 1, 13);
insert into something values (5, 2, 14);
insert into something values (6, 2, 15);
insert into something values (7, 2, 16);
create table split (SplitID Number(3), SplitName Varchar2(30));
insert into split values (10, 'Bob');
insert into split values (11, 'Carrie');
insert into split values (12, 'Alice');
insert into split values (13, 'Timothy');
insert into split values (14, 'Sue');
insert into split values (15, 'Peter');
insert into split values (16, 'Adam');
SELECT *
FROM (
SELECT somethingID, displayOrder, so.SplitID, sp.splitname
FROM SOMETHING so JOIN SPLIT sp ON so.splitID = sp.SplitID
)
PIVOT ( MAX(splitId) id, MAX(splitName) name
FOR (displayOrder, displayOrder) IN ((1, 1) AS split, (2, 2) as splitname)
);