Related
I'm trying to Find the applicant who has applied to more than 2 different companies
The applicants shown should be:
Monica (a2): jobs 101, j02, and j04
Jim (a3): jobs j02, j04, and j06
This is the script used for this!!
drop table Jobskills;
drop table Appskills;
drop table Applies;
drop table Applicant;
drop table Skills;
drop table Job;
drop table Company;
create table Company(compid char(5) primary key, compname varchar(20),
comptype varchar(15));
create table Job(jobid char(5) primary key, jobtitle varchar(20),
salarylow int, salaryhigh int, location char(10),
compid references Company(compid) on delete cascade);
create table Skills(skillid char(5) primary key, skillname varchar(15));
create table Jobskills(jobid references Job(jobid) on delete cascade,
skillid references Skills(skillid),
expertiseneeded int, primary key(jobid,skillid));
create table Applicant(appid char(5) primary key, name varchar(15),
age int, highdegree char(5), expected_salary int) ;
create table AppSkills(appid references Applicant(appid) on delete cascade,
skillid references Skills(skillid), expertise int,
primary key(appid, skillid));
create table Applies(jobid references Job(jobid),
appid references Applicant(appid) on delete cascade,
appdate date, decisiondate date, outcome char(10),
primary key(jobid, appid));
rem Initial Company data
insert into Company values('PWC', 'Price Waterhouse', 'consulting');
insert into Company values('MSFT', 'Microsoft', 'software');
insert into Company values('INTL', 'Intel', 'electronics');
insert into Company values('NCR', 'NCR Corp', 'server');
insert into Company values('WPAF', 'WP Air Force', 'defense');
insert into Company values('DLT', 'Deloitte', 'consulting');
rem Initial Job data
insert into Job values('101', 'Programmer', 55000, 60000, 'Redmond', 'MSFT');
insert into Job values('j02', 'Designer', 42000, 45000, 'Redmond', 'MSFT');
insert into Job values('j03', 'SAP impl', 30000, 40000, 'Chicago', 'PWC');
insert into Job values('j04', 'Proj mgmt', 35000, 55000, 'Chicago', 'PWC');
insert into Job values('j05', 'SOX', 60000, 65000, 'Detroit', 'PWC');
insert into Job values('j06', 'db admin', 45000, 50000, 'Dayton', 'NCR');
insert into Job values('j07', 'db designer', 35000, 40000, 'Dayton', 'NCR');
insert into Job values('j08', 'intern', 25000, 28000, 'Dayton', 'NCR');
insert into Job values('j09', 'engineer', 52000, 55000, 'Dayton','WPAF');
insert into Job values('j10', 'dba', 62000, 65000, 'Dayton','WPAF');
insert into Job values('j11', 'hardware dev', 50000, 65000, 'NYC','INTL');
insert into Job values('j12', 'pcb designer', 55000, 68000,'NYC','INTL');
insert into Job values('j13', 'chip designer', 40000, 55000,'Chicago','INTL');
insert into Job values('j14', 'IT', 40000, 60000, 'Dayton', 'DLT');
insert into Job values('j15', 'IT', 50000, 70000, 'Chicago', 'DLT');
rem initial Skills data
insert into Skills values('s1', 'database');
insert into Skills values('s2', 'programming');
insert into Skills values('s3', 'sox');
insert into Skills values('s4', 'project');
insert into Skills values('s5', 'hardware');
insert into Skills values('s6', 'sap');
insert into Skills values('s7', 'analysis');
rem Initial Jobskills data
insert into Jobskills values('101', 's2', 5);
insert into Jobskills values('101', 's7', 4);
insert into Jobskills values('j02', 's2', 3);
insert into Jobskills values('j02', 's7', 5);
insert into Jobskills values('j03', 's6', 5);
insert into Jobskills values('j04', 's7', 4);
insert into Jobskills values('j04', 's4', 5);
insert into Jobskills values('j04', 's2', 2);
insert into Jobskills values('j05', 's3', 5);
insert into Jobskills values('j06', 's1', 5);
insert into Jobskills values('j06', 's2', 3);
insert into Jobskills values('j07', 's1', 4);
insert into Jobskills values('j07', 's7', 3);
insert into Jobskills values('j08', 's1', 2);
insert into Jobskills values('j09', 's2', 4);
insert into Jobskills values('j09', 's4', 4);
insert into Jobskills values('j10', 's4', 3);
insert into Jobskills values('j10', 's1', 5);
insert into Jobskills values('j11', 's5', 3);
insert into Jobskills values('j11', 's4', 3);
insert into Jobskills values('j12', 's5', 5);
insert into Jobskills values('j13', 's1', 4);
insert into Jobskills values('j13', 's2', 5);
insert into Jobskills values('j14', 's7', 4);
rem initial Applicants data
insert into Applicant values('a1', 'Joe', 30, 'MS', 55000);
insert into Applicant values('a2', 'Monica', 25, 'BS', 62000);
insert into Applicant values('a3', 'Jim', 22, 'BS', 45000);
insert into Applicant values('a4', 'Monica', 25, 'BS', 34000);
rem initial Appskills data
insert into Appskills values('a1', 's1', 3);
insert into Appskills values('a1', 's2', 4);
insert into Appskills values('a1', 's4', 4);
insert into Appskills values('a1', 's6', 3);
insert into Appskills values('a1', 's7', 4);
insert into Appskills values('a2', 's2', 3);
insert into Appskills values('a2', 's3', 5);
insert into Appskills values('a2', 's6', 4);
insert into Appskills values('a3', 's4', 3);
insert into Appskills values('a3', 's1', 3);
insert into Appskills values('a3', 's2', 5);
rem Applies
insert into Applies values ('101', 'a1', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('101', 'a2', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('j02', 'a2', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('j04', 'a2', '01-JAN-06', '08-JAN-06', 'hire');
insert into Applies values ('j02', 'a3', '01-JAN-06', '08-JAN-06', 'nohire');
insert into Applies values ('j04', 'a3', '01-JAN-06', '08-JAN-06', 'nohire');
insert into Applies values ('j06', 'a3', '01-JAN-06', '08-JAN-06', 'nohire');
This is my code so far...
SELECT app.name, app.appid, j.jobid, c.compname
FROM applicant app INNER JOIN applies apps ON app.appid = apps.appid INNER JOIN
job j ON j.jobid = apps.jobid INNER JOIN company c ON c.compid = j.compid
WHERE apps.appid > '2'
Something is wrong though because I get Joe (a1) and I should not be getting him. I should only be getting Monica (a2) and Jim (a3). What am I missing?
Hmmm . . . You need aggregation. To answer your question, you only need a HAVING clause and the job and companies are not important. But you can put them into delimited strings if you like:
SELECT app.name, app.appid,
LIST_AGG(j.jobid, ',') WITHIN GROUP (ORDER BY j.jobid),
LIST_AGG(c.compname, ',') WITHIN GROUP (ORDER BY c.compname)
FROM applicant app INNER JOIN
applies apps
ON app.appid = apps.appid INNER JOIN
job j
ON j.jobid = apps.jobid INNER JOIN
company c
ON c.compid = j.compid
GROUP BY app.name, app.id,
HAVING COUNT(DISTINCT c.compname) > 2;
Here in common table expression I have ranked the unique university names for an applicant . Then with exists I have counted only applicants with more than one university count and showed information for those applicants only.
with cte as (
SELECT app.name, app.appid, j.jobid, c.compname, dense_rank()over(partition by app.appid order by compname)companycount
FROM applicant app INNER JOIN applies apps ON app.appid = apps.appid INNER JOIN
job j ON j.jobid = apps.jobid INNER JOIN company c ON c.compid = j.compid)
select name,appid,jobid,compname from cte c
where exists (select appid from cte ct where ct.companycount>1 and c.appid=ct.appid)
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
I'm trying to create a trigger on APEX ORACLE so that when a purchase goes through the orderline table, the quantity that is set when the customer buys the products is taken off the stock in the products table.
CREATE or Replace TRIGGER updatestock
AFTER DELETE OR UPDATE OF QUANTITY.ORDERLINE ON ORDERLINE
FOR EACH ROW
BEGIN
SET PRODUCT_STOCK.PRODUCTS = PRODUCT_STOCK.PRODUCTS - QUANTITY.ORDERLINE
WHERE PRODUCT_ID.ORDERLINE = PRODUCT_ID.PRODUCTS
END;
Basically I want the trigger to notice the quantity in the orderline and minus it from the product stock. However, I get the following error:
ORA-01748: only simple column names allowed here
Could anyone point me in the right direction?
SQL:
DROP TABLE ADMIN CASCADE CONSTRAINTS ;
DROP TABLE USERS CASCADE CONSTRAINTS ;
DROP TABLE STALLS CASCADE CONSTRAINTS ;
DROP TABLE PRODUCTS CASCADE CONSTRAINTS ;
DROP TABLE STALLHOLDER CASCADE CONSTRAINTS ;
DROP TABLE CUSTOMERORDER CASCADE CONSTRAINTS;
DROP TABLE ORDERLINE CASCADE CONSTRAINTS;
DROP TABLE COLLECTION CASCADE CONSTRAINTS;
drop sequence ADMIN_ID_SEQ;
drop sequence USER_ID_SEQ;
drop sequence STALL_ID_SEQ;
drop sequence PRODUCT_ID_SEQ;
drop sequence STALLHOLDER_ID_SEQ;
drop sequence CUSTOMERORDER_ID_SEQ;
drop sequence ORDERLINE_ID_SEQ;
drop sequence COLLECTION_ID_SEQ;
create sequence ADMIN_ID_SEQ start with 1;
create sequence USER_ID_SEQ start with 1;
create sequence STALL_ID_SEQ start with 1;
create sequence PRODUCT_ID_SEQ start with 1;
create sequence STALLHOLDER_ID_SEQ start with 1;
create sequence CUSTOMERORDER_ID_SEQ start with 1;
create sequence COLLECTION_ID_SEQ start with 1;
create sequence ORDERLINE_ID_SEQ start with 1;
CREATE table COLLECTION (
COLLECTION_ID NUMBER(20) NOT NULL PRIMARY KEY,
STATUS VARCHAR(10) NOT NULL,
TIME NUMBER(4) NOT NULL);
CREATE table CUSTOMERORDER (
ORDER_ID NUMBER(15) NOT NULL PRIMARY KEY,
ORDER_DATE NUMBER(8) NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
COLLECTION_ID NUMBER(20) NOT NULL);
CREATE TABLE STALLS (
STALL_ID NUMBER(15) NOT NULL PRIMARY KEY,
STALL_NAME VARCHAR(25) NOT NULL,
STALL_DESC VARCHAR(100),
STALL_TYPE VARCHAR(25) NOT NULL,
STALLHOLDER_ID NUMBER(25) NOT NULL);
CREATE TABLE PRODUCTS (
PRODUCT_ID NUMBER(15) NOT NULL PRIMARY KEY,
PRODUCT_NAME VARCHAR(25) NOT NULL,
PRODUCT_TYPE VARCHAR(15) NOT NULL,
PRODUCT_PRICE NUMBER(6,2) NOT NULL,
PRODUCT_STOCK NUMBER(3) NOT NULL,
STALL_ID NUMBER(15) NOT NULL);
CREATE table ORDERLINE (
ORDERLINE_ID NUMBER(15) NOT NULL PRIMARY KEY,
QUANTITY NUMBER(2) NOT NULL,
TOTALPRICE DECIMAL(19,4) NOT NULL,
ORDER_ID NUMBER(15) NOT NULL,
PRODUCT_ID NUMBER(15) NOT NULL);
CREATE table USERS (
USER_ID NUMBER(15) NOT NULL PRIMARY KEY,
USERNAME VARCHAR2(25) NOT NULL,
PASSWORD VARCHAR2(25) NOT NULL,
NAME VARCHAR2(25) NOT NULL,
SURNAME VARCHAR2(25) NOT NULL,
ADDRESS VARCHAR2(100) NOT NULL,
CONTACTNO NUMBER(11) NOT NULL);
CREATE table STALLHOLDER (
STALLHOLDER_ID NUMBER(25) NOT NULL PRIMARY KEY,
USERNAME VARCHAR2(25) NOT NULL,
PASSWORD VARCHAR2(25) NOT NULL,
NAME VARCHAR2(25) NOT NULL,
SURNAME VARCHAR2(25) NOT NULL,
CONTACTNO NUMBER(11) NOT NULL);
CREATE table ADMIN (
ADMIN_ID NUMBER(15) NOT NULL PRIMARY KEY,
USERNAME VARCHAR2(25) NOT NULL,
PASSWORD VARCHAR2(25) NOT NULL);
INSERT INTO ADMIN VALUES (ADMIN_ID_SEQ.nextval, 'Test', 'Test');
INSERT INTO ADMIN VALUES (ADMIN_ID_SEQ.nextval, 'Admin', 'Admin');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder1', 'Stallholder1', 'Stall 1', 'Stall 1', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder2', 'Stallholder2', 'Stall 2', 'Stall 2', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder3', 'Stallholder3', 'Stall 3', 'Stall 3', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder4', 'Stallholder4', 'Stall 4', 'Stall 4', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder5', 'Stallholder5', 'Stall 5', 'Stall 5', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder6', 'Stallholder6', 'Stall 6', 'Stall 6', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test', 'Test', 'Test', 'Test', 'Test', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test2', 'Test2', 'Test2', 'Test2', 'Test2', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test3', 'Test3', 'Test3', 'Test3', 'Test3', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test4', 'Test4', 'Test4', 'Test4', 'Test4', '0');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Meat Store', '', 'Meat', '1');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Meat Store 2', '', 'Meat', '2');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Confectionary Store', '', 'Confectionary', '3');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Confectionary Store 2', '', 'Confectionary', '4');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Clothing Store', '', 'Clothing', '5');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Phone Store', '', 'Clothing', '6');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '5.99', '15', '0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '9.99', '10','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '12.99', '10','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '3.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '5.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '8.99', '15','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '1.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '2.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '3.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '5.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '9.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '12.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '3.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '5.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '8.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '1.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '2.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '3.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.50', '50','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.00', '50','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix100g', 'Sweets', '2.00', '999','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix200g', 'Sweets', '3.50', '999','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'ToxicWaste', 'Sweets','1.50','50','0','3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.50', '100','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.00', '100','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix100g', 'Sweets', '2.00', '999','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix200g', 'Sweets', '3.50', '999','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'ToxicWaste', 'Sweets', '1.50', '50','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'T-Shirts', 'Clothing', '10.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Shorts', 'Clothing', '15.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Jeans', 'Clothing', '20.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Hoodies', 'Clothing', '20.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Vests', 'Clothing', '10.00', '20','0', '5');
ALTER TABLE STALLS ADD CONSTRAINT FK_STALLHOLDER_ID FOREIGN KEY (STALLHOLDER_ID) REFERENCES STALLHOLDER(STALLHOLDER_ID);
ALTER TABLE ORDERLINE ADD CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES CUSTOMERORDER(ORDER_ID);
ALTER TABLE ORDERLINE ADD CONSTRAINT FK_PRODUCT_ID FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(PRODUCT_ID);
ALTER TABLE CUSTOMERORDER ADD CONSTRAINT FK_COLLECTION_ID FOREIGN KEY (COLLECTION_ID) REFERENCES COLLECTION(COLLECTION_ID);
ALTER TABLE PRODUCTS ADD CONSTRAINT FK_STALL_ID FOREIGN KEY (STALL_ID) REFERENCES STALLS(STALL_ID);
The error is presumably coming from the OF QUANTITY.ORDERLINE because you're using a table.column pattern rather than just the column name. You also seem to be consistently putting the table and column names the wrong way around, and you're missing the UPDATE before SET, and you are trying to refer to columns in the table row you're updating rather than using the NEW pseudorow. So this should be closer:
CREATE or Replace TRIGGER updatestock
AFTER DELETE OR UPDATE OF QUANTITY ON ORDERLINE
FOR EACH ROW
BEGIN
UPDATE PRODUCTS
SET PRODUCT_STOCK = PRODUCT_STOCK - :NEW.QUANTITY
WHERE PRODUCT_ID = :NEW.PRODUCT_ID
END;
But NEW values are null on delete; you aren't doing any adjustment in insert; and even on update you maybe want to be taking both the old and new quantity into account.
Even then trying to maintain a running total in another table only really works in a single-user system, or where updates are serialised. If two people update the same orderline row at the same time, the trigger will apply both updates to the products table, but the second will block until the first has committed, and will be applied to the old value at the start of that statement - so you'll lose the first update, effectively.
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');
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