CIS276DB
Module 6 Assignment
1. Write a select statement based on the InvoiceTotal column of the Invoices table:
Use the CAST function to return the first column as an integer value.
Name it IntTotal. Name it IntTotal.
Use the CAST function to return the second column as datatype decimal
with one digit to the right. Name it DecimalTotal.
Use the CONVERT function to return the third column as a datatype that
outputs 2 digits to the right of the decimal point and all comma’s to the
left (i.e. 3, 106.34). Name it FormatTotal.
2. Write a select statement that returns 4 columns based on the Vendors table:
(Column name- Name): this column should be formatted in the following
way; VendorContactFName followed by the last initial and a period
(example: “John S.”).
(Column name- StateInitial): the VendorState first initial in lowercase.
(Column name- Phone): VendorPhone without the area code
(Column name- TodaysDate): the current date formatted like- Apr 18,
2008
Filter the results to only return rows where the VendorPhone prefix is equal to
‘(800)’. Sort the results by VendorState and LastName.
3. Business Case: The current date is 12/1/2008; the accounting department
would like to know which invoices with a balance due are still outstanding and
the current age in days their invoice is beyond the invoice date.
Write a select statement that returns 4 columns: VendorName, InvoiceTotal,
InvoiceDate and InvoiceAge (use the appropriate function that will return the
number of days between the InvoiceDate and ‘12/1/2008’).
Filter the results to only return rows where there is a balance due and the
InvoiceAge is greater than 132. Sort the results by VendorName.
4. Write a select statement that returns 7 columns:
InvoiceDate
(Column name- WrittenDate): use the function that will convert
InvoiceDate to this format; Apr 18, 2008
(Column name- NewDate): use the function that will add 45 days to
InvoiceDate and convert it to this format; Apr 18, 2008
(Column name- DayOfWeek): Use the function that will return the name
of the day of NewDate (i.e. Saturday)
(Column name- MonthPart): Use the function that will return the name
of the month of NewDate (i.e March)
(Column name- DatePart): Use the function that will return the day date
of NewDate (i.e. 18 {of Apr 18, 2008})
Column name- YearPart): Use the function that will return the year from
NewDate (i.e. 2008)
Sort the results by InvoiceDate.
5. Business Case: The executive committee is implementing a purchase discount
program based on the invoice total for a vendor. As such, they need to gauge how
many invoices might qualify for a discount. Invoices that are below $100 will NOT
qualify for a discount. Invoices between 101 and $500 are a low consideration,
invoices between 501 and $1000 are a higher consideration and invoices above
$1000 are the highest consideration.
Write a sel ...
CIS276DB Module 6 Assignment 1. Write a select sta.docx
1. CIS276DB
Module 6 Assignment
1. Write a select statement based on the InvoiceTotal column of
the Invoices table:
integer value.
Name it IntTotal. Name it IntTotal.
datatype decimal
with one digit to the right. Name it DecimalTotal.
datatype that
outputs 2 digits to the right of the decimal point and all
comma’s to the
left (i.e. 3, 106.34). Name it FormatTotal.
2. Write a select statement that returns 4 columns based on the
Vendors table:
- Name): this column should be formatted in
2. the following
way; VendorContactFName followed by the last initial and a
period
(example: “John S.”).
- StateInitial): the VendorState first initial in
lowercase.
- Phone): VendorPhone without the area code
- TodaysDate): the current date formatted like-
Apr 18,
2008
Filter the results to only return rows where the VendorPhone
prefix is equal to
‘(800)’. Sort the results by VendorState and LastName.
3. Business Case: The current date is 12/1/2008; the accounting
department
would like to know which invoices with a balance due are still
outstanding and
the current age in days their invoice is beyond the invoice date.
Write a select statement that returns 4 columns: VendorName,
InvoiceTotal,
InvoiceDate and InvoiceAge (use the appropriate function that
will return the
number of days between the InvoiceDate and ‘12/1/2008’).
Filter the results to only return rows where there is a balance
due and the
3. InvoiceAge is greater than 132. Sort the results by
VendorName.
4. Write a select statement that returns 7 columns:
- WrittenDate): use the function that will
convert
InvoiceDate to this format; Apr 18, 2008
- NewDate): use the function that will add 45
days to
InvoiceDate and convert it to this format; Apr 18, 2008
- DayOfWeek): Use the function that will
return the name
of the day of NewDate (i.e. Saturday)
- MonthPart): Use the function that will return
the name
of the month of NewDate (i.e March)
- DatePart): Use the function that will return
the day date
of NewDate (i.e. 18 {of Apr 18, 2008})
- YearPart): Use the function that will return
the year from
4. NewDate (i.e. 2008)
Sort the results by InvoiceDate.
5. Business Case: The executive committee is implementing a
purchase discount
program based on the invoice total for a vendor. As such, they
need to gauge how
many invoices might qualify for a discount. Invoices that are
below $100 will NOT
qualify for a discount. Invoices between 101 and $500 are a low
consideration,
invoices between 501 and $1000 are a higher consideration and
invoices above
$1000 are the highest consideration.
Write a select statement that returns 4 columns: VendorName,
InvoiceNumber,
InvoiceTotal, and PotentialDiscount.
PotentialDiscount is a column that will contain the result
expression from a
CASE statement that contains 4 conditionals based on the
InvoiceTotal column;
Conditionals Result expression
InvoiceTotal < 100 ‘No discount consideration’
InvoiceTotal 101-500 ‘Discount potential 3’
InvoiceTotal 501-1000 ‘Discount potential 2’
InvoiceTotal > 1000 ‘Discount potential 1’
Sort the results by InvoiceTotal.
5. 6. Business Case: The accounting department would like to
know the current
balances for vendors that owe money on their accounts. They
would like to
categorize vendors who owe over $11,000 as having a Very
High debt level, those
who owe between $11,000 and over $500 as having a High debt
level, those who
owe between $500 and over $200 as having a Medium debt level
and anyone
else as a Low debt level.
Write a select statement that returns 3 columns:
function
o Sum of Balances greater than $11,000 = ‘Very High’
o Sum of Balances between $11,000 and greater than $500 =
‘High’
o Sum of Balances between $500 and greater than $200 =
‘Medium’
o Sum of Balances equal to $200 or less = ‘Low’
Filter the results to only include vendors where a balance is due
and sort the
results from the sum of largest balance to smallest.
6. To display a column where the data type cannot be implicitly
converted, use a CAST function
USE AP;
SELECT InvoiceTotal, InvoiceDate, CAST(InvoiceTotal AS
varchar) AS ITVc,
CAST(InvoiceDate AS money) AS TimeIsMoney
FROM Invoices
CONVERT allows you to format certain datatypes (dates
primarily)
SELECT CONVERT(varchar, InvoiceDate) AS S1,
CONVERT(varchar, InvoiceDate,1) AS S2,
CONVERT(varchar, InvoiceDate,109) AS S3,
CONVERT(varchar, InvoiceDate, 8) AS S4,
CONVERT(varchar, InvoiceDate, 10) AS S5,
CONVERT(varchar, InvoiceTotal, 1) AS M1,
CONVERT(varchar, InvoiceTotal, 101) AS M2,
CONVERT(varchar, InvoiceTotal, 53) AS M3
FROM Invoices
Using carriage returns and line feeds (CHAR(13), CHAR(10))
you can output columns as text (example here- address label)
USE AP
SELECT VendorContactFName + ' ' + VendorContactLName
+ CHAR(13) + CHAR(10)
+ VendorAddress1 + CHAR(13) + CHAR(10)
+ VendorCity + ', ' + VendorState + ' '
+ VendorZipCode
+ CHAR(13)
FROM Vendors
7. As it turns out, you don’t need the line feed!
USE AP
SELECT VendorContactFName + ' ' + VendorContactLName
+ CHAR(13)
+ VendorAddress1 + CHAR(13)
+ VendorCity + ', ' + VendorState + ' '
+ VendorZipCode
+ CHAR(13)
FROM Vendors
String functions
USE AP
SELECT VendorName, LEN(vendorName) AS L,
UPPER(VendorName) AS U, LOWER(VendorName) AS
Lw,
RIGHT(VendorName, 6) AS Rn,
LEFT(VendorName, 6) AS Ln
FROM Vendors
String functions
USE AP
SELECT VendorAddress1, CHARINDEX('po', VendorAddress1)
AS wiPO,
PATINDEX('%in%', VendorAddress1) AS wiIN,
REVERSE(VendorAddress1) AS scramble
FROM Vendors
String functions
USE AP
SELECT VendorPhone,
REPLACE(RIGHT(VendorPhone, 13), ') ', '-') AS
Noparenthesis,
SUBSTRING(VendorPhone, 7,8) AS NoAreaCode,
SUBSTRING(VendorPhone, 2,3) AS AreaCode
8. FROM Vendors
String functions
USE Examples
SELECT Name, LEFT(Name, CHARINDEX(' ', Name)-1) AS
FirstName,
RIGHT(Name, LEN(Name) - CHARINDEX(' ', Name)) AS
LastName
FROM StringSample
Date functions using only GETDATE()
SELECT GETDATE() AS TodaysDate, DAY(GETDATE()) AS
CurrDay,
MONTH(GETDATE()) AS CurrMonth,
YEAR(GETDATE()) AS CurrYear,
DATENAME(month, GETDATE()) AS MonthName
Date functions
USE AP
SELECT InvoiceDate, InvoiceDate + 14 AS IDpTW,
DATEDIFF(month, InvoiceDate, GETDATE()) AS
DateDif,
CONVERT(varchar, DATEADD(year, 9, InvoiceDate),107)
AS DateAdd
FROM Invoices
Ranking functions
USE AP
SELECT NTILE(4) OVER (ORDER BY InvoiceTotal DESC) AS
Rank,
InvoiceTotal, InvoiceNumber
FROM Invoices
Ranking functions
USE AP
SELECT NTILE(10) OVER (ORDER BY InvoiceTotal DESC)
9. AS Rank,
InvoiceTotal, InvoiceNumber
FROM Invoices
Ranking functions
USE AP
SELECT RANK() OVER (ORDER BY InvoiceTotal) AS Rank,
DENSE_RANK() OVER (ORDER BY InvoiceTotal) AS
DenseRank,
InvoiceTotal
FROM Invoices
Ranking functions
USE AP
SELECT ROW_NUMBER() OVER (PARTITION BY
VendorState, VendorCity ORDER BY VendorName)
AS RowNum, VendorName, VendorState, VendorCity
FROM Vendors
Case Function
USE AP
SELECT VendorName, InvoiceTotal,
CASE
WHEN InvoiceTotal > 5000 THEN 'We owe our
souls'
WHEN InvoiceTotal BETWEEN 1000 AND 5000
THEN 'Its ok'
WHEN InvoiceTotal BETWEEN 100 AND 999 THEN
'much better'
WHEN InvoiceTotal < 100 THEN 'awesome'
END AS DebtType
FROM Vendors v JOIN Invoices i
ON V.VendorID = i.VendorID
10. ORDER BY InvoiceTotal DESC
Case Function
USE AP
SELECT InvoiceNumber, TermsID,
CASE TermsID
WHEN 1 THEN 'Net due 10 days'
WHEN 2 THEN 'Net due 20 days'
WHEN 3 THEN 'Net due 30 days'
WHEN 4 THEN 'Net due 40 days'
WHEN 5 THEN 'Net due 50 days'
END AS Terms
FROM Invoices
ORDER BY Terms
Case Function using GROUPING (this removes the NULL value
that’s present in a summary query that uses a ROLLUP or
CUBE operator)
USE AP
SELECT
CASE
WHEN GROUPING(VendorState) = 1 THEN 'All'
ELSE VendorState
END AS VendorState,
CASE
WHEN GROUPING(VendorCity) = 1 THEN 'All'
ELSE VendorCity
11. END AS VendorCity,
COUNT(*) AS QtyVend
FROM Vendors
WHERE VendorState IN('IA', 'NJ')
GROUP BY VendorState, VendorCity WITH ROLLUP
ORDER BY VendorState DESC, VendorCity
COALESCE removes null values that are present in columns-
here using the left join between Vendors and Invoices- vendors
that don’t have an Invoice (therefore don’t have an
InvoiceTotal) will display NULL in the InvoiceTotal column.
Using COALESCE, we can substitute a value for NULL (since
InvoiceTotal is a money data type, we have to use a CAST
function to substitute a varchar data type- ‘No Invoice Total’).
USE AP
SELECT VendorName, COALESCE(CAST(InvoiceTotal AS
Varchar), 'No Invoice Total')
AS InvoiceTotal
FROM Vendors v LEFT JOIN Invoices i
ON v.Vendorid = i.VendorID
ORDER BY VendorName
ISNULL works the same as COALESCE
USE AP
SELECT VendorName, ISNULL(CAST(InvoiceTotal AS
Varchar), 'No Invoice Total')
AS InvoiceTotal
FROM Vendors v LEFT JOIN Invoices i
ON v.Vendorid = i.VendorID
ORDER BY VendorName
For this ISNULL example, we substitute in a money value
(0.00) so CAST isn’t necessary
USE AP
SELECT VendorName, ISNULL(InvoiceTotal, 0.00)
AS InvoiceTotal
12. FROM Vendors v LEFT JOIN Invoices i
ON v.Vendorid = i.VendorID
ORDER BY VendorName
IIF Function
USE AP
SELECT InvoiceTotal, VendorState,
IIF(InvoiceTotal>1500, 'High', 'Low') AS InvoiceValue,
IIF(VendorState = 'CA', 'Cali Vendor', 'Not Cali') AS
StateVendor
FROM Vendors v JOIN Invoices i
ON v.VendorID = i.vendorID
Creating test tables for adding, modifying and deleting data
USE AP
SELECT *
INTO Invoice1
FROM Invoices
Creating test tables for adding, modifying and deleting data
USE AP
SELECT *
INTO Vendor1
FROM Vendors
Creating test tables for adding, modifying and deleting data
(partial table)
USE AP
SELECT *
INTO Invoice2
FROM Invoices
WHERE InvoiceTotal-PaymentTotal-CreditTotal = 0
INSERT statement that only uses a VALUE list
13. USE AP
INSERT INTO Invoice2
VALUES(1, '123456', '1/1/2016', 100.00, 0, 0, 1, '1/10/2016',
NULL)
INSERT statement that uses a COLUMN list along with several
VALUE lists
USE AP
INSERT INTO Invoice2
(InvoiceNumber, VendorID, InvoiceDate,
InvoiceTotal,PaymentTotal,
CreditTotal, InvoiceDueDate, TermsID)
VALUES('234567', 6, '2/1/2016', 500.00, 0, 0, '2/10/2016', 1),
('234567', 6, '2/1/2016', 500.00, 0, 0, '2/10/2016', 1),
('234567', 6, '2/1/2016', 500.00, 0, 0, '2/10/2016', 1),
('234567', 6, '2/1/2016', 500.00, 0, 0, '2/10/2016', 1),
('234567', 6, '2/1/2016', 500.00, 0, 0, '2/10/2016', 1)
INSERT statement that uses a subquery
USE AP
INSERT INTO Invoice1
(VendorID, InvoiceTotal, InvoiceNumber, CreditTotal,
TermsID,
PaymentTotal, InvoiceDate, InvoiceDueDate)
SELECT VendorID, InvoiceTotal, InvoiceNumber, CreditTotal,
TermsID,
PaymentTotal, InvoiceDate, InvoiceDueDate
FROM Invoices
WHERE InvoiceTotal > 100
UPDATE statement (UPDATES REQUIRE A WHERE!)
14. USE AP
UPDATE Invoice2
SET PaymentDate = '1/14/2016',
PaymentTotal = PaymentTotal + 100
WHERE InvoiceNumber = '123456'
UPDATE statement (UPDATES REQUIRE A WHERE!)
USE AP
UPDATE Invoice1
SET CreditTotal = CreditTotal + 4.50
WHERE VendorID =
(SELECT VendorID
FROM Vendor1
WHERE VendorName = 'Blue Cross')
DELETE statement (DELETES REQUIRE A WHERE!)
USE AP
DELETE Invoice1
WHERE InvoiceID = 4
DROP Table syntax (forgot to do this in class
DROP TABLE Invoice1
DROP TABLE Invoice2
DROP TABLE vendor1
Databases/AP.mdf
Databases/AP_AllObjects.mdf
Databases/AP_AllObjects_log.LDF
Databases/AP_log.LDF
Databases/Examples.mdf