trying to execute sql command - sql

CREATE TABLE dw.D_DAYLIGHT_SAVINGS
(
DAYLIGHT_YEAR INTEGER NOT NULL,
DAYLIGHT_START DATE,
DAYLIGHT_END DATE,
Last_Udpated_date DATE DEFAULT SYSDATE,
Last_updated_user VARCHAR2 (25) DEFAULT 'DW'
);
table got created but when i try to run in TOAD AND insert values in it error pops as command not properly ended. the query for insert is as follows
insert into dw.D_DAYLIGHT_SAVINGS values
(2013,'March 10','November 3'),
(2014,'March 9','November 2'),
(2015,'March 8','November 1'),
(2016,'March 13','November 6'),
(2017,'March 12','November 5');

You have to have a separate INSERT INTO statement for each row and add the DEFAULT for each column that you did not specify:
insert into dw.D_DAYLIGHT_SAVINGS values (2013,'March 10','November 3', DEFAULT, DEFAULT);
insert into dw.D_DAYLIGHT_SAVINGS values (2014,'March 9','November 2', DEFAULT, DEFAULT);
insert into dw.D_DAYLIGHT_SAVINGS values (2015,'March 8','November 1', DEFAULT, DEFAULT);
insert into dw.D_DAYLIGHT_SAVINGS values (2016,'March 13','November 6', DEFAULT, DEFAULT);
insert into dw.D_DAYLIGHT_SAVINGS values (2017,'March 12','November 5', DEFAULT, DEFAULT);
Or provide the list of columns into which the columns should be inserted:
insert into dw.D_DAYLIGHT_SAVINGS (DAYLIGHT_YEAR, DAYLIGHT_START, DAYLIGHT_END) values (2013,'March 10','November 3');
insert into dw.D_DAYLIGHT_SAVINGS (DAYLIGHT_YEAR, DAYLIGHT_START, DAYLIGHT_END) values (2014,'March 9','November 2');
insert into dw.D_DAYLIGHT_SAVINGS (DAYLIGHT_YEAR, DAYLIGHT_START, DAYLIGHT_END) values (2015,'March 8','November 1');
insert into dw.D_DAYLIGHT_SAVINGS (DAYLIGHT_YEAR, DAYLIGHT_START, DAYLIGHT_END) values (2016,'March 13','November 6');
insert into dw.D_DAYLIGHT_SAVINGS (DAYLIGHT_YEAR, DAYLIGHT_START, DAYLIGHT_END) values (2017,'March 12','November 5');

Related

SQL Constraint only if column has a specific value

I would like to add a unique constraint to a sql table but only if the column has a specific value. In my case, I have Geographic IDs and Status as fields. The IDs can have multiple status but there should only be one Current (C) status per ID. Is there a way to involve a where clause to have unique values only in the case of C values?
I tried adding a UNIQUE CONSTAINT as well as a filtered index, to no avail.
In SQL Server you can create filtered indexes, e.g.:
create table dbo.Demo (
GeographicID int,
[Status] char(1)
);
create unique index IXUF_Demo_GeographicID_Current
on dbo.Demo (GeographicID)
where [Status]='C';
insert dbo.Demo (GeographicID, [Status]) values (1, 'A');
insert dbo.Demo (GeographicID, [Status]) values (1, 'A');
insert dbo.Demo (GeographicID, [Status]) values (1, 'B');
insert dbo.Demo (GeographicID, [Status]) values (1, 'B');
insert dbo.Demo (GeographicID, [Status]) values (1, 'C'); --Succeeds
insert dbo.Demo (GeographicID, [Status]) values (1, 'C'); --Fails

Solving a Query on SQLDeveloper

need some help on solving this query.
I want to get projects that had no change in their team (i.e. because project0002 has two different project managers it must not appear on the result of the query)
Can you help me ?
CREATE TABLE sprintMembers (
userID NUMBER(10) NOT NULL,
sprintNumber NUMBER(10) NOT NULL,
sprintProjectCode VARCHAR(255) NOT NULL,
memberRole VARCHAR(255) NOT NULL,
PRIMARY KEY (userID, sprintNumber, sprintProjectCode));
INSERT INTO sprintMembers VALUES (4,1,'PROJ0001', 'Project Manager');
INSERT INTO sprintMembers VALUES (5,1,'PROJ0001', 'Product Owner');
INSERT INTO sprintMembers VALUES (6,1,'PROJ0001', 'Scrum Master');
INSERT INTO sprintMembers VALUES (4,1,'PROJ0002', 'Project Manager');
INSERT INTO sprintMembers VALUES (3,2,'PROJ0002', 'Project Manager');
INSERT INTO sprintMembers VALUES (4,3,'PROJ0002', 'Project Manager');
INSERT INTO sprintMembers VALUES (5,1,'PROJ0002', 'Product Owner');
INSERT INTO sprintMembers VALUES (5,2,'PROJ0002', 'Product Owner');
INSERT INTO sprintMembers VALUES (7,1,'PROJ0002', 'Team Member');
INSERT INTO sprintMembers VALUES (7,2,'PROJ0002', 'Team Member');
INSERT INTO sprintMembers VALUES (4,1,'PROJ0003', 'Project Manager');
INSERT INTO sprintMembers VALUES (4,2,'PROJ0003', 'Project Manager');
INSERT INTO sprintMembers VALUES (5,1,'PROJ0003', 'Product Owner');
INSERT INTO sprintMembers VALUES (5,2,'PROJ0003', 'Product Owner');
INSERT INTO sprintMembers VALUES (7,1,'PROJ0003', 'Scrum Master');
INSERT INTO sprintMembers VALUES (7,2,'PROJ0003', 'Scrum Master');
INSERT INTO sprintMembers VALUES (6,1,'PROJ0003', 'Team Member');
INSERT INTO sprintMembers VALUES (6,2,'PROJ0003', 'Team Member');
INSERT INTO sprintMembers VALUES (8,1,'PROJ0004', 'Project Manager');
INSERT INTO sprintMembers VALUES (8,2,'PROJ0004', 'Project Manager');
INSERT INTO sprintMembers VALUES (8,3,'PROJ0004', 'Project Manager');
INSERT INTO sprintMembers VALUES (9,1,'PROJ0004', 'Product Owner');
INSERT INTO sprintMembers VALUES (9,2,'PROJ0004', 'Product Owner');
INSERT INTO sprintMembers VALUES (9,3,'PROJ0004', 'Product Owner');
INSERT INTO sprintMembers VALUES (10,1,'PROJ0004', 'Scrum Master');
INSERT INTO sprintMembers VALUES (10,2,'PROJ0004', 'Scrum Master');
INSERT INTO sprintMembers VALUES (10,3,'PROJ0004', 'Scrum Master');
INSERT INTO sprintMembers VALUES (6,1,'PROJ0004', 'Team Member');
INSERT INTO sprintMembers VALUES (6,2,'PROJ0004', 'Team Member');
INSERT INTO sprintMembers VALUES (6,3,'PROJ0004', 'Team Member');
If I understand correctly you can aggregate and count distinct userId
select sprintProjectCode
from sprintMembers
group by sprintProjectCode
having Count(distinct case when memberRole='project manager' then userid end)=1
Result:
PROJ0001
PROJ0003
PROJ0004
Edit
Another approach applying to all member role values
with groups as (
select sprintProjectCode, Count(*) cnt
from sprintMembers
group by sprintProjectCode, userid, memberrole
)
select sprintProjectCode
from groups
group by sprintProjectCode
having Min(cnt) = Max(cnt)

SQL Query: To list the majors with more than 2 students in it

I am trying to figure out a query that will list the major that has more than 2 students in it (CS) but I am not sure how to go about it.
This is the script to create the table for SQL!
REM drop all the tables. Note that you need to drop the
REM dependent table first before dropping the base tables.
drop table Reg;
drop table Student;
drop table Course;
REM Now create all the tables.
create table Student
(
sid char(10) primary key,
sname varchar(20) not null,
gpa float,
major char(10),
dob DATE
);
create table Course
(
cno char(10) primary key,
cname varchar(20) not null,
credits int,
dept char(10)
);
create table Reg
(
sid references Student(sid) on delete cascade,
cno references Course(cno) on delete cascade,
grade char(2),
primary key (sid, cno)
);
REM Now insert all the rows.
insert into Student values ('111', 'Joe', 3.5 , 'MIS', '01-AUG-2000');
insert into Student values ('222', 'Jack', 3.4 , 'MIS', '12-JAN-1999');
insert into Student values ('333', 'Jill', 3.2 , 'CS', '15-MAY-1998');
insert into Student values ('444', 'Mary', 3.7 , 'CS', '17-DEC-2001');
insert into Student values ('555', 'Peter', 3.8 , 'CS', '19-MAR-1999');
insert into Student values ('666', 'Pat', 3.9, 'Math', '31-MAY-2000');
insert into Student values ('777', 'Tracy', 4.0, 'Math', '18-JUL-1997');
insert into Course values ('c101', 'intro', 3 , 'CS');
insert into Course values ('m415', 'database', 4 , 'Bus');
insert into Course values ('m215', 'programming', 4 , 'Bus');
insert into Course values ('a444', 'calculus', 3 , 'Math');
insert into Reg values ('111', 'c101', 'A');
insert into Reg values ('111', 'm215', 'B');
insert into Reg values ('111', 'm415', 'A');
insert into Reg values ('222', 'm215', 'A');
insert into Reg values ('222', 'm415', 'B');
insert into Reg values ('333', 'c101', 'A');
insert into Reg values ('444', 'm215', 'C');
insert into Reg values ('444', 'm415', 'B');
insert into Reg values ('555', 'c101', 'B');
insert into Reg values ('555', 'm215', 'A');
insert into Reg values ('555', 'm415', 'A');
insert into Reg values ('666', 'c101', 'A');
Thank you so much!
this will list group by major within CS and having more than 2
select major ,count(1) from Student
where major ='CS'
group by major
having count(1)>2

Unknown column in where clause?

My code:
drop table if exists HSstudents;
create table HSstudents
(
HSsID int,
vNAAM text,
aNAAM text,
LT int,
GM float
);
insert into HSstudents values (1, 'Thomas', 'Jansen', 18, 7.0);
insert into HSstudents values (2, 'Jesse', 'Bakker', 19, 6.5);
insert into HSstudents values (3, 'Tom', 'Smit', 20, 7.1);
insert into HSstudents values (4, 'Jelle', 'Visser', 17, 9.6);
insert into HSstudents values (5, 'Sem', 'Dekker', 17, 8.1);
insert into HSstudents values (6, 'Anna', 'Peters', 18, 6.8);
insert into HSstudents values (7, 'Michelle', 'Hendriks', 19, 8.2);
insert into HSstudents values (8, 'Senna', 'Mulder', 20, 5.9);
insert into HSstudents values (9, 'Sven', 'Linden', 21, 6.0);
insert into HSstudents values (10, 'Ilse', 'Jacobs', 21, 7.5);
insert into HSstudents values (11, 'Harm', 'Schouten', 19, 7.0);
insert into HSstudents values (12, 'Emma', 'Dijkstra', 18, 8.1);
drop table if exists students;
create table students
(
sID int,
vNAAM text,
aNAAM text,
LT int
);
insert into students values (1, 'Thomas', 'Jansen', 18);
insert into students values (2, 'Jesse', 'Bakker', 19);
insert into students values (3, 'Tom', 'Smit', 20);
insert into students values (4, 'Jelle', 'Visser', 17);
insert into students values (5, 'Sem', 'Dekker', 17);
insert into students values (6, 'Anna', 'Peters', 18);
insert into students values (7, 'Michelle', 'Hendriks', 19);
insert into students values (8, 'Senna', 'Mulder', 20);
insert into students values (9, 'Sven', 'Linden', 21);
insert into students values (10, 'Ilse', 'Jacobs', 21);
insert into students values (11, 'Harm', 'Schouten', 19);
insert into students values (12, 'Emma', 'Dijkstra', 18);
drop table if exists applications;
create table applications
(
sID int,
aPROV text,
sPROV text,
taal text
);
insert into applications values (1, 'Overijssel', 'Drenthe', 'HTML');
insert into applications values (2, 'Gelderland', 'Overijssel', 'CSS');
insert into applications values (3, 'Groningen', 'Flevoland', 'CSS');
insert into applications values (4, 'Overijssel', 'Zuid-Holland', 'SQL');
insert into applications values (5, 'Noord-Holland', 'Drenthe', 'C#');
insert into applications values (6, 'Flevoland', 'Groningen', 'C#');
insert into applications values (7, 'Limburg', 'Groningen', 'JAVA');
insert into applications values (8, 'Limburg', 'Limburg', 'JAVASCRIPT');
insert into applications values (9, 'Drenthe', 'Noord-Brabant', 'CSS');
insert into applications values (10, 'Drenthe', 'Zeeland', 'Python');
insert into applications values (11, 'Zuid-Holland', 'Friesland', 'C++');
insert into applications values (12, 'Zeeland', 'Friesland', 'JAVA');
select
S.sID, S.vNAAM, S.aNAAM, S.LT, aPROV, sPROV, taal
from
HSstudents HS, students S, applications A
where
HSstudents.HSsID = students.sID
This results in an error
Code: 1054. Unknown column 'HSstudents.HSsID' in 'where clause'
How? Shouldn't it just work?
WHERE clause should follow the remane on the FROM clause:
where HS.HSsID = S.sID

SQL Query count

HI there I have this table,
Recipe = (idR, recipeTitle, prepText, cuisineType, mealType)
Ingredient = (idI, ingrDesc)
RecipIngr = (idR*, idI*)
and I'm trying to query a list for ingrDesc with a count of how many recipies that ingrDesc is in. I want to list only those ingrDesc that occur more than 10 times.
Here's what I have:
SELECT a.idI, a.recipeTitle
FROM Recipe a
INNER JOIN recpingr b
ON a.idr = b.idr
WHERE a.preptext = '>10'
Any help as I don't know how to carry on with this query
Use GROUP BY with HAVING:
SELECT i.idI, i.ingrDesc, COUNT(*)
FROM Ingredient i
INNER JOIN RecipIngr ri ON i.idI = ri.idI
GROUP BY i.idI, i.ingrDesc
HAVING COUNT(*) > 10
You need to use a group by clause and having. I have created a quick sample here but my sample data does not go up to 10 so I used any ingredient that was used more than once (> 1).
Here is the sample data:
create table dbo.recipe (
idR int not null,
recipeTitle varchar(100) not null,
prepText varchar(4000) null,
cuisineType varchar(100) null,
mealType varchar(100) null
)
go
insert into dbo.recipe values (1, 'Eggs and Bacon', 'Prep Text 1', 'American', 'Breakfast')
insert into dbo.recipe values (2, 'Turkey Sandwich', 'Prep Text 2', 'American', 'Lunch')
insert into dbo.recipe values (3, 'Roast Beef Sandwich', 'Prep Text 3', 'American', 'Lunch')
go
create table dbo.ingredient (
idI int not null,
ingrDesc varchar(200) not null
)
go
insert into dbo.ingredient values (1, 'Large Egg')
insert into dbo.ingredient values (2, 'Bacon');
insert into dbo.ingredient values (3, 'Butter');
insert into dbo.ingredient values (4, 'Sliced Turkey');
insert into dbo.ingredient values (5, 'Lettuce');
insert into dbo.ingredient values (6, 'Tomato');
insert into dbo.ingredient values (7, 'Onion');
insert into dbo.ingredient values (8, 'Bread');
insert into dbo.ingredient values (9, 'Mustard');
insert into dbo.ingredient values (10, 'Horseradish');
insert into dbo.ingredient values (11, 'Sliced Roast Beef');
go
create table dbo.recipingr(
idR int not null,
idI int not null
)
go
insert into dbo.recipingr values (1, 1);
insert into dbo.recipingr values (1, 2);
insert into dbo.recipingr values (2, 4);
insert into dbo.recipingr values (2, 5);
insert into dbo.recipingr values (2, 6);
insert into dbo.recipingr values (2, 7);
insert into dbo.recipingr values (2, 8);
insert into dbo.recipingr values (2, 9);
insert into dbo.recipingr values (3, 11);
insert into dbo.recipingr values (3, 10);
insert into dbo.recipingr values (3, 8);
insert into dbo.recipingr values (3, 6);
insert into dbo.recipingr values (3, 5);
go
Here is the query:
select
i.ingrDesc,
count(*) ingrCount
from
dbo.recipe r
inner join dbo.recipingr ri on ri.idR = r.idR
inner join dbo.ingredient i on i.idI = ri.idI
group by
i.ingrDesc
having
count(*) > 1