Trigger created with compilation error - sql

I am trying to create a trigger that increases the discnt of a customer by .04 every time that customer places an order. Next I need to insert a new order in the orders table.
The following is the Customers table:
CID CNAME CITY DISCNT
c001 Tiptop Duluth 10
c002 Basics California 12
c003 7/11 California 8
c004 ACME Duluth 8
c006 ACME Kyoto 0
c007 Goldberg NYC 15
The following is the orders table:
ORDNO MON CID AID PID QTY DOLLARS
1011 jan c001 a01 p01 1000 450
1012 jan c001 a01 p01 1000 450
1019 feb c001 a02 p02 400 180
1017 feb c001 a06 p03 95959 540
1018 feb c001 a03 p04 600 540
1023 mar c001 a04 p05 500 450
1022 mar c001 a05 p06 400 720
1025 apr c001 a05 p07 800 720
1013 jan c002 a03 p03 1000 880
1026 may c002 a05 p03 800 704
1015 jan c003 a03 p05 1200 1104
1014 jan c003 a03 p05 1200 1104
1021 feb c004 a06 p01 1000 460
1016 jan c006 a01 p01 1000 500
1020 feb c006 a03 p07 600 600
1024 mar c006 a06 p01 800 400
The trigger I have created is:
create or replace trigger UpdateDiscnt
after insert or update on orders
for each row
begin
update customers set discnt = 0.4 + :old.discnt where
customers.cid=:new.cid;
end;
/

The error is an oracle error and there is no discnt in the order table so any version of old.discnt is incorrect.
try
create or replace trigger UpdateDiscnt
after insert or update on orders
for each row
begin
update customers set discnt = 0.4 + discnt
where customers.cid= :new.cid;
end;
/

Related

calling many table in one query based on week

if there are multiple tables with the same contents like this:
table one:
CustomerID
CustTrans
Weeks
C001
2022-09-03
36
C002
2022-09-02
36
C003
2022-09-03
36
C004
2022-09-02
36
C002
2022-09-08
37
C001
2022-09-05
37
C002
2022-09-11
38
C002
2022-09-23
39
C004
2022-09-19
39
C001
2022-09-18
39
C003
2022-09-26
40
C005
2022-09-17
38
C006
2022-09-25
40
C001
2022-09-25
40
table 2:
CustomerID
CustTrans
Weeks
C001
2022-09-03
36
C002
2022-09-02
36
C003
2022-09-03
36
C004
2022-09-02
36
C002
2022-09-08
37
C001
2022-09-05
37
C002
2022-09-11
38
C002
2022-09-23
39
C004
2022-09-19
39
C001
2022-09-18
39
C003
2022-09-26
40
C005
2022-09-17
38
C006
2022-09-25
40
C001
2022-09-25
40
table 3
CustomerID
CustTrans
Weeks
C001
2022-09-03
36
C002
2022-09-02
36
C003
2022-09-03
36
C004
2022-09-02
36
C002
2022-09-08
37
C001
2022-09-05
37
C002
2022-09-11
38
C002
2022-09-23
39
C004
2022-09-19
39
C001
2022-09-18
39
C003
2022-09-26
40
C005
2022-09-17
38
C006
2022-09-25
40
C001
2022-09-25
40
it's possible to make many table in just single table?
this is my query
CREATE DATABASE manydata;
CREATE TABLE trydata1
(
CustomerID CHAR(7) not null,
CustTrans date,
CustSales int,
)
insert into trydata1(CustomerID,CustSales,CustTrans)
values('C001',34,'2022-09-03'),('C002',23,'2022-09-02'),('C003',132,'2022-09-03'),
('C004',95,'2022-09-02'),('C002',68,'2022-09-08'),('C001',54,'2022-09-05'),
('C002',34,'2022-09-11'),('C002',98,'2022-09-23'),('C004',34,'2022-09-19'),
('C001',30,'2022-09-18'),('C003',34,'2022-09-26'),('C005',34,'2022-09-17'),
('C006',34,'2022-09-25'),('C001',34,'2022-09-25');
CREATE TABLE trydata2
(
CustomerID CHAR(7) not null,
CustTrans date,
CustSales int,
)
insert into trydata2(CustomerID,CustSales,CustTrans)
values('C001',34,'2022-09-03'),('C002',23,'2022-09-02'),('C003',132,'2022-09-03'),
('C004',95,'2022-09-02'),('C002',68,'2022-09-08'),('C001',54,'2022-09-05'),
('C002',34,'2022-09-11'),('C002',98,'2022-09-23'),('C004',34,'2022-09-19'),
('C001',30,'2022-09-18'),('C003',34,'2022-09-26'),('C005',34,'2022-09-17'),
('C006',34,'2022-09-25'),('C001',34,'2022-09-25');
CREATE TABLE trydata3
(
CustomerID CHAR(7) not null,
CustTrans date,
CustSales int,
)
insert into trydata3(CustomerID,CustSales,CustTrans)
values('C001',34,'2022-09-03'),('C002',23,'2022-09-02'),('C003',132,'2022-09-03'),
('C004',95,'2022-09-02'),('C002',68,'2022-09-08'),('C001',54,'2022-09-05'),
('C002',34,'2022-09-11'),('C002',98,'2022-09-23'),('C004',34,'2022-09-19'),
('C001',30,'2022-09-18'),('C003',34,'2022-09-26'),('C005',34,'2022-09-17'),
('C006',34,'2022-09-25'),('C001',34,'2022-09-25');
You seem to be looking for UNION or UNION ALL.
select *
from
(
select * from table1
union all
select * from table2
union all
select * from table3
) t
where ...;
According to your sample data, this gets you duplicates, as some entries exist in more than one table. If you want to remove the duplicates, use UNION instead of UNION ALL.

finding duplicate values with join

ITEMS
ITEM_ID NAME_ID ITEM_NAME
1001 2001 Office chair
1002 2002 Writing Desk
1003 2003 Filing cabinet
1004 2004 Bookshelf bookcase
1005 2005 Table lamp
1006 2001 Office chair
1007 2002 Writing Desk
1008 2003 Filing cabinet
1009 2004 Bookshelf bookcase
1010 2005 Table lamp
1011 2001 Office chair
1012 2002 Writing Desk
1013 2003 Filing cabinet
1014 2004 Bookshelf bookcase
1015 2005 Table lamp
1016 2016 Triangle window
1017 2017 Screen
1018 2018 Cradle
1019 2017 Screen
1020 2018 Cradle
1021 2017 Screen
1022 2018 Cradle
1023 2023 Futon
1024 2024 Single bed
1025 2025 Bunk beds
1026 2026 Sofa bed
1027 2027 Camp bed cot sleeping bag
1028 2028 Airbed air mattress
1029 2029 Hammock
1030 2030 Loveseat
1031 2031 Sleeper sofa
1032 2032 Settee
1032 2032 Settee
1033 2001 Office chair
1034 2002 Writing Desk
1035 2003 Filing cabinet
1036 2004 Bookshelf/bookcase
1037 2005 Table lamp
1038 2001 Office chair
1039 2002 Writing Desk
1040 2003 Filing cabinet
1041 2004 Bookshelf/bookcase
1042 2005 Table lamp
1043 2017 Screen
1044 2018 Cradle
1045 2017 Screen
1046 2018 Cradle
1047 2017 Screen
1048 2018 Cradle
1049 2017 Screen
1050 2018 Cradle
ITEMS_DETAILS:
CITY ITEM_ID SHOP_ID
NEW YORK 1001 4001
NEW YORK 1002 4002
NEW YORK 1003 4003
NEW YORK 1004 4004
NEW YORK 1005 4005
DALLAS 1006 4006
DALLAS 1007 4007
DALLAS 1008 4008
DALLAS 1009 4001
DALLAS 1010 4002
DALLAS 1011 4003
DALLAS 1012 4004
WASHINGTON 1013 4005
WASHINGTON 1014 4006
WASHINGTON 1015 4007
WASHINGTON 1016 4008
WASHINGTON 1017 4009
WASHINGTON 1018 4010
WASHINGTON 1019 4011
SANFRANSISCO 1020 4012
SANFRANSISCO 1021 4013
CHICAGO 1022 4014
CHICAGO 1023 4015
CHICAGO 1024 4016
CHICAGO 1025 4017
BOSTON 1026 4018
BOSTON 1027 4019
BOSTON 1028 4020
BOSTON 1029 4021
BOSTON 1030 4022
SANFRANSISCO 1031 4023
SANFRANSISCO 1032 4024
SANFRANSISCO 1032 4025
SANFRANSISCO 1033 4026
Las Vegas 1034 4027
Austin 1035 4028
Houston 1036 4029
Los Angeles 1037 4030
Seattle 1038 4031
Atlanta 1039 4032
McKinney 1040 4033
Vancouver 1041 4034
Las Vegas 1042 4035
Austin 1043 4036
Houston 1044 4037
Los Angeles 1045 4038
Seattle 1046 4034
Atlanta 1047 4035
McKinney 1048 4036
Vancouver 1049 4037
Las Vegas 1050 4043
Austin 1051 4044
Houston 1052 4045
Los Angeles 1053 4046
Seattle 1054 4047
Atlanta 1055 4048
McKinney 1056 4049
Vancouver 1057 4050
Las Vegas 1058 4051
Austin 1059 4052
Houston 1060 4053
Hi All,
I am trying to find the duplicates values of the columns after the result of the join ITEMS & ITEM_DETAILS.
I know the sql for duplicate values of column on a single table. A bit confused with join.
Logic: If ITEM_NAME is the same but SHOP_ID is different, it should show as duplicate. If SHOP_ID is the same, it should show as unique
Please help me.
I tried as below:
select * from (
select a.NAME_ID from ITEMS a inner join ITEMS_DETAILS b on b.ITEM_ID = a.ITEM_ID) x
inner join ITEMS y on y.NAME_ID=x.NAME_ID
inner join ITEMS_DETAILS z on z.ITEM_ID=y.ITEM_ID
If you are interested in grouping and counting dups then try the query below:
SELECT
COUNT(*) As DupCount,
y.ITEM_ID
FROM
ITEMS y
INNER JOIN ITEMS_DETAILS z ON z.ITEM_ID=y.ITEM_ID
GROUP BY
y.ITEM_ID
HAVING
COUNT(*) > 1

why the query is returning every combination

My DBMS is oracle.
I have a table trans with the following column.
Trans_id
Emp_no
From_date
To_date
102
100
2021-05-10
2021-05-16
And an other table time_result with the following columns
Emp_no
Date
W_code
100
2021-05-10
01
100
2021-05-10
02
100
2021-05-10
7.5
100
2021-05-11
08
100
2021-05-12
01
100
2021-05-12
7.5
100
2021-05-13
01
100
2021-05-13
7.5
100
2021-05-14
01
100
2021-05-14
7.5
And another table grade with the following columns
Emp_no
Date
W_code
grade
100
2021-05-10
100
2021-05-11
08
100
2021-05-12
100
2021-05-13
CS
100
2021-05-13
100
2021-05-14
I want this out put
Emp_no
Date
W_code
grade
100
2021-05-10
01
100
2021-05-10
02
100
2021-05-10
7.5
100
2021-05-11
08
100
2021-05-12
01
100
2021-05-12
7.5
100
2021-05-13
01
CS
100
2021-05-13
7.5
100
2021-05-14
01
100
2021-05-14
7.5
In order to achieve this, I have written this query
`
select distinct t.trans_id,
r.emp_no,
r.date,
r.w_code,
g.grade
from trans t
left join time_result r on t.emp_no=r.emp_no
right join grade g on r.date=g.date
`'''
The only problem with this query is, for transaction on 2021-05-13,it’s giving every combination of job grade like this
Trans_id
Emp_no
Date
W_code
grade
102
100
2021-05-10
01
102
100
2021-05-10
02
102
100
2021-05-10
7.5
102
100
2021-05-11
08
102
100
2021-05-12
01
102
100
2021-05-12
7.5
102
100
2021-05-13
01
CS
102
100
2021-05-13
7.5
102
100
2021-05-13
01
102
100
2021-05-13
7.5
CS
102
100
2021-05-14
01
102
100
2021-05-14
7.5
Please advise, in what way the query should be changed to get the desired output.
Any help will be greatly appreciated!
Thanks!!

grouping a table with different dates

I have a table like below,
SalesId ItemId DateSale USDVal
ABC 01A 2018-04-01 52
ABC 01B 2018-04-01 300
ABC 01C 2018-04-01 12
ABC 01D 2018-04-01 62
ABC 01A 2018-03-23 66
MNB 01A 2018-01-01 584
MNB 01A 2018-02-20 320
MNB 01F 2018-02-20 5
I want to write a query that selects the last date for each SalesId and shows those records so the result would look something like below.
Result
SalesId ItemId DateSale USDVal
ABC 01A 2018-04-01 52
ABC 01B 2018-04-01 300
ABC 01C 2018-04-01 12
ABC 01D 2018-04-01 62
MNB 01A 2018-02-20 320
MNB 01F 2018-02-20 5
In SQL Server, the fastest way is often a correlated subquery:
select t.*
from t
where t.datesale = (select max(t2.datesale) from t t2 where t2.salesid = t.salesid);

SQL server select from 3 tables

I have three tables in my database Books, Borrowers and Movement:
Books
BookID Title Author Category Published
----------- ------------------------------ ------------------------- --------------- ----------
101 Ulysses James Joyce Fiction 1922-06-16
102 Huckleberry Finn Mark Twain Fiction 1884-03-24
103 The Great Gatsby F. Scott Fitzgerald Fiction 1925-06-17
104 1984 George Orwell Fiction 1949-04-19
105 War and Peace Leo Tolstoy Fiction 1869-08-01
106 Gullivers Travels Jonathan Swift Fiction 1726-07-01
107 Moby Dick Herman Melville Fiction 1851-08-01
108 Pride and Prejudice Jane Austen Fiction 1813-08-13
110 The Second World War Winston Churchill NonFiction 1953-09-01
111 Relativity Albert Einstein NonFiction 1917-01-09
112 The Right Stuff Tom Wolfe NonFiction 1979-09-07
121 Hitchhikers Guide to Galaxy Douglas Adams Humour 1975-10-27
122 Dad Is Fat Jim Gaffigan Humour 2013-03-01
131 Kick-Ass 2 Mark Millar Comic 2012-03-03
133 Beautiful Creatures: The Manga Kami Garcia Comic 2014-07-01
Borrowers
BorrowerID Name Birthday
----------- ------------------------- ----------
2 Bugs Bunny 1938-09-08
3 Homer Simpson 1992-09-09
5 Mickey Mouse 1928-02-08
7 Fred Flintstone 1960-06-09
11 Charlie Brown 1965-06-05
13 Popeye 1933-03-03
17 Donald Duck 1937-07-27
19 Mr. Magoo 1949-09-14
23 George Jetson 1948-04-08
29 SpongeBob SquarePants 1984-08-04
31 Stewie Griffin 1971-11-17
Movement
MoveID BookID BorrowerID DateOut DateIn ReturnCondition
----------- ----------- ----------- ---------- ---------- ---------------
1 131 31 2012-06-01 2013-05-24 good
2 101 23 2012-02-10 2012-03-24 good
3 102 29 2012-02-01 2012-04-01 good
4 105 7 2012-03-23 2012-05-11 good
5 103 7 2012-03-22 2012-04-22 good
6 108 7 2012-01-23 2012-02-12 good
7 112 19 2012-01-12 2012-02-10 good
8 122 11 2012-04-14 2013-05-01 poor
9 106 17 2013-01-24 2013-02-01 good
10 104 2 2013-02-24 2013-03-10 bitten
11 121 3 2013-03-01 2013-04-01 good
12 131 19 2013-04-11 2013-05-23 good
13 111 5 2013-05-22 2013-06-22 poor
14 131 2 2013-06-12 2013-07-23 bitten
15 122 23 2013-07-10 2013-08-12 good
16 107 29 2014-01-01 2014-02-14 good
17 110 7 2014-01-11 2014-02-01 good
18 105 2 2014-02-22 2014-03-02 bitten
What is a query I can use to find out which book was borrowed by the oldest borrower?
I am new to SQL and am using Microsoft SQL Server 2014
Here are two different solutions:
First using two sub querys and one equi-join:
select Title
from Books b , Movement m
where b.BookID = m.BookID and m.BorrowerID = (select BorrowerID
from Borrowers
where Birthday = (select MIN(Birthday)
from Borrowers))
Using two equi-joins and one sub query:
select Title
from Books b, Borrowers r, Movement m
where b.BookID = m.BookID
and m.BorrowerID = r.BorrowerID
and Birthday = (select MIN(Birthday) from Borrowers)
Both above queries give the following answer:
Title
------------------------------
Relativity