DATABASE SQL
Task 4.1.2. Find the earliest po_date for each vendor_id in the pos table, include all the columns from the pos table and order by vendor_id. This cannot be done by a GROUP BY. You will need to use a correlated query based on the vendor_id.
For example:
Result |
---|
job_id po_id po_date vendor_id ---------- ---------- ---------- ---------- 005 FFF 1989-12-01 ABC 004 CCC 1990-01-05 SOS 006 GGG 1988-07-15 XYZ |
table
job_id po_id po_date vendor_id
---------- ---------- ---------- ----------
002 AAA 1990-05-20 ABC
002 BBB 1990-03-15 XYZ
004 CCC 1990-01-05 SOS
004 DDD 1990-01-01 ABC
005 EEE 1990-01-15 SOS
005 FFF 1989-12-01 ABC
006 GGG 1988-07-15 XYZ
cannot use group by clause!
NOTE : GROUP BY clause is aggregate while the PARTITION BY clause is analytic
WITH correlatedResult AS
(
SELECT *
,ROW_NUMBER()OVER(PARTITION BY vendor_id ORDER BY po_date DESC ) as
row_num,
FROM pos
)
SELECT job_id,po_id,po_date,vendor_id
FROM correlatedResult
WHERE row_num = 1;
DATABASE SQL Task 4.1.2. Find the earliest po_date for each vendor_id in the pos table, include...
Create SQL statements based on the given tables: Code to create the tables: CREATE DATABASE LAB4DB; USE LAB4DB; CREATE TABLE CUSTOMER ( CUS_CODE int primary key, CUS_LNAME varchar(15), CUS_FNAME varchar(15), CUS_INITIAL varchar(1), CUS_AREACODE varchar(3), CUS_PHONE varchar(8), CUS_BALANCE float(8) ); INSERT INTO CUSTOMER VALUES('10010','Ramas','Alfred','A','615','844-2573','0'); INSERT INTO CUSTOMER VALUES('10011','Dunne','Leona','K','713','894-1238','0'); INSERT INTO CUSTOMER VALUES('10012','Smith','Kathy','W','615','894-2285','345.86'); INSERT INTO CUSTOMER VALUES('10013','Olowski','Paul','F','615','894-2180','536.75'); INSERT INTO CUSTOMER VALUES('10014','Orlando','Myron','','615','222-1672','0'); INSERT INTO CUSTOMER VALUES('10015','O''Brian','Amy','B','713','442-3381','0'); INSERT INTO CUSTOMER VALUES('10016','Brown','James','G','615','297-1228','221.19'); INSERT INTO CUSTOMER VALUES('10017','Williams','George','','615','290-2556','768.93'); INSERT INTO CUSTOMER VALUES('10018','Farriss','Anne','G','713','382-7185','216.55'); INSERT INTO CUSTOMER VALUES('10019','Smith','Olette','K','615','297-3809','0'); /*...
Write MySQL query statements for the questions below including the output that proves the accuracy of your solution. Your answers should be stored in a text file that captures your interaction with MySQL. 1. Find the movieID, title, year and DVDPrice of all movies where the DVD-Price is equal to the discountPrice. 2. Find the actorID, lastName, firstName, middleName, and suffix of all actors whose middleName is not NULL. 3. Suppose you remember a movie quote as “Play it again,...
CUSTOMER EMP INVOICE LINE P CODE V CODE CUS CODE EMP CODE NV NUMBER NV NUMBER, LINE NUMBER Create SQL queries for displaying the following results 5. List the customers who have ordered product bearing product code "23109-HB. Display first names, last names of customers, invoice numbers and product codes 6. Display Vendor details (V_CODE, V NAME) and product details (P CODE, P DESCRIPT) and product quantity on hand in excess of product min quantity (give alias of Quantity above...