COSC 2307: Row Functions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

COSC 2307

Module 9
Row Functions
Row Functions
• Row functions calculate a new value based on the data in a single row
of a table
• The value can be based on the date in one column or several different
columns
• Some row functions operate on numbers, and others operate on text
or on dates
Row Functions
• The new values may appear in the results table
• May be used in
• SELECT clause
• WHERE clause
• ORDER BY clause

• The new column of information is not stored on the disk with the
other data of the table
• It does not become a permanent part of the table itself
• It is held in memory while the select statement is being processed
Row Functions
You can create a new table that adds a new column by using a row
function.
• Example in MySQL:
CREATE TABLE product_copy AS
SELECT *, msrp * onhand AS inventory_value
FROM product;
• Example in MS SQL Server
SELECT *, msrp * onhand AS inventory_value
INTO product_copy
FROM product;
Your turn
Create a new table, named customer_new, that adds new column to
the customer table (GeneralStore database). Create new column by
using the following row function:

• Column name: new_credit_limit


• Function: creditlimit + 1000
Solution in MySQL
Create a new table, named customer_new, that adds new column to
the customer table (GeneralStore database). Create new column by
using the following row function:
• Column name: new_credit_limit
• Function: creditlimit + 1000

CREATE TABLE customer_new AS


SELECT *, creditlimit + 1000 AS new_credit_limit
FROM customer;
Solution using MS SQL Server
Create a new table, named customer_new, that adds new column to
the customer table (GeneralStore database). Create new column by
using the following row function:
• Column name: new_credit_limit
• Function: creditlimit + 1000

SELECT *, creditlimit + 1000 AS new_credit_limit


INTO customer_new
FROM customer;
Row Functions
You can display new msrp increased by 10% in the result table without
changing the data in the underlying table.
• Example in MySQL:
SELECT productnum, descr, producttype,msrp+ (msrp * 0.10) AS
newMSRP, onhand
FROM product;
Your turn
• Modify the statement below
SELECT custnum,custname,city,creditlimit
FROM customer
WHERE city = 'Toronto'
ORDER BY custname;
• Show same data but credit limit increased by 20% without changing
data in underlining table.
Your turn - SOLUTION
• Modify the statement below
SELECT custnum,custname,city,creditlimit
FROM customer
WHERE city = 'Toronto'
ORDER BY custname;
• Show same data but credit limit increased by 20% without changing data in
underlining table.
SELECT custnum,custname,city,creditlimit +(creditlimit * 0.2) AS creditlimit
FROM customer
WHERE city = 'Toronto'
ORDER BY custname;
Your turn
• Create a view called v_orderline from the orderline table. Have the
following columns in that view: ordernum, productnum, quantity,
salesprice, quantity * salesprice AS totalprice,(quantity * salesprice)
* 0.13 AS tax, (quantity * salesprice)+((quantity * salesprice) * 0.13)
AS totalwithtax
SELECT ordernum, productnum, quantity, salesprice, quantity * salesprice AS
totalprice,(quantity * salesprice) * 0.13 AS tax, (quantity * salesprice)+((quantity *
salesprice) * 0.13) AS totalwithtax
FROM orderline;
OrderNum ProductNum Quantity SalesPrice TotalPrice Tax TotalWithTax
1 7 2 34.99 69.98 9.0974 79.0774
2 6 1 980 980 127.4 1107.4
3 4 16 25 400 52 452
4 1 1 44.95 44.95 5.8435 50.7935
5 2 1 99.99 99.99 12.999 112.9887
5 8 1 25.99 25.99 3.3787 29.3687
6 7 1 34.99 34.99 4.5487 39.5387
7 3 1 299.99 299.99 38.999 338.9887
8 2 1 99.99 99.99 12.999 112.9887
9 5 2 29.99 59.98 7.7974 67.7774
10 5 11 27.5 302.5 39.325 341.825
11 3 1 295 295 38.35 333.35
11 4 3 42 126 16.38 142.38
11 5 4 27.5 110 14.3 124.3
12 8 1 25.99 25.99 3.3787 29.3687
13 6 1 975 975 126.75 1101.75
14 7 2 34.99 69.98 9.0974 79.0774
15 2 1 105 105 13.65 118.65
15 7 1 34.99 34.99 4.5487 39.5387
Your turn – sample solution
• Create a view called v_orderline from the orderline table. Have the following
columns in that view: ordernum, productnum, quantity, salesprice,
quantity * salesprice AS totalprice,(quantity * salesprice) * 0.13 AS tax,
(quantity * salesprice)+((quantity * salesprice) * 0.13) AS totalwithtax
CREATE VIEW vorderline AS
SELECT ordernum, productnum, quantity, salesprice, quantity * salesprice
AS totalprice,(quantity * salesprice) * 0.13 AS tax, (quantity *
salesprice)+((quantity * salesprice) * 0.13) AS totalwithtax
FROM orderline;
You can now use that view to select data from
it using calculated fields
SELECT *
FROM vorderline
WHERE totalwithtax >300
ORDER BY totalwithtax DESC;
You can combine fields into single field
SELECT CONCAT ('Customer Name:', custname , ' , Customer Number:
' , custnum) AS customer_info
FROM customer;
customer_info
Customer Name:Kent , Customer Number: 3
Customer Name:Moore , Customer Number: 4
Customer Name:Felipe , Customer Number: 5
Customer Name:Khoury , Customer Number: 7
Customer Name:Brant , Customer Number: 8
Customer Name:Wells , Customer Number: 9
Customer Name:Ironwood , Customer Number: 13
Customer Name:Joe , Customer Number: 14
Customer Name:Test22 , Customer Number: 15
Customer Name:Joe G. , Customer Number: 16
Your turn
Using vorderline show price calculation in one column:
totalprice + tax = totalwithtax
Your turn – sample solution

Using vorderline show price calculation in one column:


totalprice + tax = totalwithtax

SELECT CONCAT (totalprice, ' + ' ,tax, ' = ', totalwithtax ) AS


price_calculation
FROM vorderline;
DATE FUNCTIONS IN MySQL
SELECT CURDATE(), orderdate, DATEDIFF(CURDATE(), orderdate)
FROM orders;

Curdate() Orderdate DATEDIFF(Curdate(), orderdate)


03/09/2014 05/09/1997 6207
03/09/2014 05/09/1997 6207
03/09/2014 05/09/1997 6207
03/09/2014 01/10/1997 6181
03/09/2014 08/10/1997 6174
03/09/2014 19/10/1997 6163
03/09/2014 15/10/1997 6167
03/09/2014 17/10/1997 6165
03/09/2014 04/11/1997 6147
03/09/2014 06/11/1997 6145
03/09/2014 01/12/1997 6120
03/09/2014 04/12/1997 6117
03/09/2014 08/12/1997 6113
03/09/2014 15/12/1997 6106
03/09/2014 17/12/1997 6104
Your turn
• Using DATEDIFF function in MySQL display orders placed within the
last 30 days
Your turn – sample solution
• Using DATEDIFF function in MySQL display orders placed within the
last 10 days
SELECT *
FROM orders
WHERE DATEDIFF(CURDATE(), orderdate) <=10;
Date functions in MySQL
• Try
SELECT CURDATE(), CURDATE()+ 10, CURDATE() – 10
FROM dual;
Date functions in MySQL
• Now try
SELECT CURDATE(), STR_TO_DATE(CURDATE()+ 10,'%Y%m%d'),
CURDATE() – 10
FROM dual;
DATE PART QUERIES (MySQL)
SELECT YEAR(orderdate), MONTH(orderdate), DAY(orderdate)
FROM orders;
• The above query pulls out the various parts of the orderdate column in the orders table
• Notice each value is separated and listed as its own column in the resultset

YEAR(orderdate) MONTH(orderdate) DAY(orderdate)


1997 9 5
1997 9 5
1997 9 5
1997 10 1
1997 10 8
1997 10 19
1997 10 15
1997 10 17
1997 11 4
1997 11 6
1997 12 1
1997 12 4
1997 12 8
1997 12 15
1997 12 17
DAYNAME (MySQL)
• We might also include
DAYNAME as part of a larger ordernum orderdate DAYNAME(orderdate)

query 1
2
05/09/1997 Friday
05/09/1997 Friday
3 05/09/1997 Friday
SELECT ordernum, orderdate, 4 01/10/1997 Wednesday
DAYNAME(orderdate) 5 08/10/1997 Wednesday
6 19/10/1997 Sunday
FROM orders ; 7 15/10/1997 Wednesday
8 17/10/1997 Friday
9 04/11/1997 Tuesday
10 06/11/1997 Thursday
11 01/12/1997 Monday
12 04/12/1997 Thursday
13 08/12/1997 Monday
14 15/12/1997 Monday
15 17/12/1997 Wednesday
Formatting String Values in ORACLE
• You can combine data from the tables by concatenating them.
• Besides strings and printable characters you can combine special non-
printable character for formatting purposes:
• CHR(10) – new line
• CHR(13) - carriage return (not used in linux).
• CHR(9) is a tab

SELECT first_name || ' ' || last_name || CHR(10) ||


'Email: ' || CHR(9) || email || CHR(10) ||
'Phone: ' || CHR(9) || phone_number
FROM hr.employees;
Scalar Functions in Oracle
•TO_DATE function
In Oracle, TO_DATE function converts a string value to DATE data type value using the specified format. ; SELECT
TO_DATE('2012-06-05', 'YYYY-MM-DD') FROM dual;

•TO_CHAR function
• Converts a DATETIME, number, expression to a TEXT expression in a specified format

•SYSDATE function
• Returns today’s date and time

•ROUND function
SELECT ROUND(123.456, 2) FROM dual;
DATE PART QUERIES in Oracle
SELECT EXTRACT (year FROM orderdate) AS y
FROM s9.tbl_orders;
DATE PART QUERIES in Oracle
SELECT EXTRACT (day FROM orderdate) AS d,
EXTRACT (month FROM orderdate) AS m,
EXTRACT (year FROM orderdate) AS y
FROM s9.tbl_orders;
• The above query pulls out the various parts of the orderdate column in the orders table
• Notice each value is separated and listed as its own column in the resultset
DAYNAME/DATENAME (MySQL/SQL Server)
• If we want to know the day name for a specific date, we can use the
DAYNAME() function in MySQL or DATENAME() in MS SQL Server or
use TO_CHAR(TO_DATE) in Oracle
MySQL:
SELECT DAYNAME('2013-01-23') FROM dual;
MS SQL Server:
SELECT DATENAME(w,'2013-01-23’)
Oracle:
SELECT TO_CHAR(TO_DATE('2013-01-23','yyyy-mm-dd'),'day') AS
dayname FROM dual;
DAYNAME('2013-01-23')
Wednesday
DATENAME (Transact-SQL) – MS SQL Server
• Returns a character string that represents the specified datepart of
the specified date
datepart Abbreviations
Syntax
year yy, yyyy
SELECT DATENAME ( datepart , date ) quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
TZoffset tz
ISO_WEEK ISOWK, ISOWW
DAY NAME (MySQL/SQL Server), Oracle
• We might also include DAYNAME (or DATENAME)
as part of a larger query ordernum orderdate DAYNAME(orderdate)
MySQL: 1 05/09/1997Friday
2 05/09/1997 Friday
SELECT ordernum, orderdate, DAYNAME(orderdate) 3 05/09/1997 Friday
FROM orders ; 4 01/10/1997 Wednesday
5 08/10/1997 Wednesday
6 19/10/1997 Sunday
MS SQL Server: 7 15/10/1997 Wednesday
SELECT ordernum, orderdate, 8 17/10/1997 Friday
DATENAME(w,orderdate) 9 04/11/1997 Tuesday
FROM orders ; 10 06/11/1997Thursday
11 01/12/1997 Monday
12 04/12/1997 Thursday
ORACLE: 13 08/12/1997 Monday
SELECT ordernum, 14 15/12/1997 Monday
orderdate,TO_CHAR(TO_DATE(orderdate,'yyyy-mm- 15 17/12/1997 Wednesday
dd'),'day') AS dayname
FROM s9.tbl_orders ;
Display current system date
Using MySQL: Using MS SQL Server:
• CURDATE() • GETDATE()
Will display current system date Will display current system date
Example: Example:
SELECT CURDATE() FROM dual; SELECT GETDATE();

CURDATE()
03/09/2014 2014-09-03 17:46:39.400

Using Oracle:

SELECT SYSDATE FROM dual;


ORACLE:
SELECT ordernum, orderdate, ROUND(SYSDATE - orderdate) AS
numofdays
FROM s9.tbl_orders ;
Example in Oracle
• Table employee contains the hiring date. Use that
information to find out how many years each employee has
been working for?
SELECT first_name, last_name,
ROUND((SYSDATE - hire_date)/365) AS
"Years"
FROM hr.employees;
Comparison Operators

• Examples:
SELECT * FROM hr.employees WHERE salary > 20000;
Or try:
SELECT * FROM s9.tbl_customer WHERE creditlimit >0;
Other Comparison Operators

• Example:
SELECT *
FROM hr.employees
WHERE salary BETWEEN 15000 AND 20000;
Logical Conditions

• Example:
SELECT *
FROM hr.employees
WHERE salary >10000 AND department_id = 100;
Or try:
SELECT * FROM s9.tbl_customer
WHERE creditlimit >0 AND city LIKE 'Tor%';
Rules of Precedence
Eliminating Duplicate Rows

• Eliminate duplicate rows by using the DISTINCT keyword in the SELECT


clause
SELECT DISTINCT salary FROM hr.employees;
SELECT DISTINCT city FROM s9.tbl_customer;
• To eliminate duplicate rows in the result, use the DISTINCT keyword
in the SELECT statement immediately after the SELECT keyword
ORDER BY Clause
• ORDER BY clause sorts the result set based on specified field and sequence

SELECT * FROM hr.employees


WHERE department_id = 100 AND salary >= 7000
ORDER BY first_name;

Or try:

SELECT *
FROM s9.tbl_customer
WHERE creditlimit >0
AND city LIKE 'Tor%'
ORDER BY custname DESC;

You might also like