Question

In: Computer Science

Below is what I have to do. This is all performed in SQL. I have written...

Below is what I have to do. This is all performed in SQL. I have written a bunch of code, that I have also provided. Any help is appreciated

Exercises

Complete each of the following exercises. If you are unsure how to accomplish the task, please consult the coursework videos where there are explanations and demos.

  1. Use built in SQL functions to write an SQL Select statement on fudgemart_products which derives a product_category column by extracting the last word in the product name. For example
    1. for a product named ‘Leather Jacket’ the product category would be ‘Jacket’
    2. for a product named ‘Straight Claw Hammer’ the category would be ‘Hammer’

Your select statement should include product id, product name, product category and product department.

  1. Write a user defined function called f_total_vendor_sales which calculates the sum of the wholesale price * quantity of all products sold for that vendor. There should be one number associated with each vendor id, which is the input into the function. Demonstrate the function works by executing an SQL select statement over all vendors calling the function.
  2. Write a stored procedure called p_write_vendor which when given a required vendor name, phone and optional website, will look up the vendor by name first. If the vendor exists, it will update the phone and website. If the vendor does not exist, it will add the info to the table. Write code to demonstrate the procedure works by executing the procedure twice so that it adds a new vendor and then updates that vendor’s information.
  3. Create a view based on the logic you completed in question 1 or 2. Your SQL script should be programmed so that the entire script works every time, dropping the view if it exists, and then re-creating it.
  4. Write a table valued function f_employee_timesheets which when provided an employee_id will output the employee id, name, department, payroll date, hourly rate on the timesheet, hours worked, and gross pay (hourly rate times hours worked).

Written Code

use fudgemart_v3
go

--Question 1
--This runs but doesn't supply correct output
select * from fudgemart_products
select right(product_name, charindex(' ', product_name)) as product_category from fudgemart_products
go


---Runs but returns NULL for product_Category
select product_id, product_name, product_department
from fudgemart_products
order by product_id
declare @product_name as varchar(20)
select right(@product_name, charindex(' ',@product_name)) as product_category

print len(@product_name)


---question 2-----

drop function dbo.f_vendor_sales
go

declare @vendor_id int
set @vendor_id = 1
select count(*) from fudgemart_products where product_vendor_id = @vendor_id
go

--Function says it is completed
create function dbo.f_total_vendor_sales(
   @vendor_id int --input
   ) returns int as
begin
   declare @count int
   set @count = (select count(*) from fudgemart_products.dbo.product_wholesale_price where product_vendor_id = @vendor_id)
   return @count --output
end
go

---When i attempt function, I get invalid object name

select product_vendor_id, product_wholesale_price,
   dbo.f_total_vendor_sales(product_vendor_id) as total_vendor_sales
   from fudgemart_products


----For question 3-------
create procedure p_write_vendor
(
   @vendor_name varchar (50),
   @vendor_phone varchar (20),
   @vendor_website varchar (100)
   ) as
if exists ( select 1 from fudgemart_vendors
               where vendor_name = @vendor_name
               or vendor_phone = @vendor_phone
               or vendor_website = @vendor_website)
   begin
       update fudgemart_vendors
       set vendor_name =@vendor_name,
           vendor_phone = @vendor_phone,
           vendor_website = @vendor_website
       where vendor_name = @vendor_name
       or vendor_phone = @vendor_phone
       or vendor_website = @vendor_website
   end
else
   begin
       insert into fudgemart_vendors values (@vendor_name, @vendor_phone, @vendor_website)
   end

Solutions

Expert Solution

--Question 1
select * from fudgemart_products
select right(product_name, charindex(' ', product_name)) as product_category from fudgemart_products
go


select product_id, product_name, product_department
from fudgemart_products
order by product_id
declare @product_name as varchar(20)
select right(@product_name, charindex(' ',@product_name)) as product_category

print len(@product_name)


---question 2-----

drop function dbo.f_vendor_sales
go

declare @vendor_id int
set @vendor_id = 1
select count(*) from fudgemart_products where product_vendor_id = @vendor_id
go

--Function says it is completed
create function dbo.f_total_vendor_sales(
   @vendor_id int --input
   ) returns int as
begin
   declare @count int
   set @count = (select count(*) from fudgemart_products.dbo.product_wholesale_price where product_vendor_id = @vendor_id)
   return @count --output
end

select product_vendor_id, product_wholesale_price,
   dbo.f_total_vendor_sales(product_vendor_id) as total_vendor_sales
   from fudgemart_products


----For question 3-------
create procedure p_write_vendor
(
   @vendor_name varchar (50),
   @vendor_phone varchar (20),
   @vendor_website varchar (100)
   ) as
if exists ( select 1 from fudgemart_vendors
               where vendor_name = @vendor_name
               or vendor_phone = @vendor_phone
               or vendor_website = @vendor_website)
   begin
       update fudgemart_vendors
       set vendor_name =@vendor_name,
           vendor_phone = @vendor_phone,
           vendor_website = @vendor_website
       where vendor_name = @vendor_name
       or vendor_phone = @vendor_phone
       or vendor_website = @vendor_website
   end
else
   begin
       insert into fudgemart_vendors values (@vendor_name, @vendor_phone, @vendor_website)
   end


Related Solutions

SQL- Trigger I have two tables (below) I need to write a trigger that would delete...
SQL- Trigger I have two tables (below) I need to write a trigger that would delete everything for a pid from the Appt table if the pid is deleted from the patient table. Create table Appt(                 pid numeric Foreign Key references patient(pid),                 ptname varchar(50) Foreign Key references patient(name),                 dob date Foreign Key references patient(dob),                 dr varchar(20),                 appdate date,                 apptime time, ); and Create table Patient(                 pid numeric primary key,                 name varchar(50),                ...
Language: Java I have written this code but not all methods are running. First method is...
Language: Java I have written this code but not all methods are running. First method is running fine but when I enter the file path, it is not reading it. Directions The input file must be read into an input array and data validated from the array. Input file format (500 records maximum per file): comma delimited text, should contain 6 fields: any row containing exactly 6 fields is considered to be invalid Purpose of this code is to :...
Language: C++ The program I have written below needs to allow the user to enter in...
Language: C++ The program I have written below needs to allow the user to enter in their own date, however I currently can only get it to output a default date, 10/10/2018. After the user enters the date it needs to go through the exception checks to make sure it is a valid date. Afterword the user needs to be prompted to change their date. Here is the code: #include <iostream> #include <cmath> #include <fstream> #include <cstring> #include <sstream> #include...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
Write SQL queries below for each of the following: List the names and cities of all...
Write SQL queries below for each of the following: List the names and cities of all customers List the different states the vendors come from (unique values only, no duplicates) Find the number of customers in California List product names and category descriptions for all products supplied by vendor Proformance List names of all employees who have sold to customer Rachel Patterson
How do I convert a character column into date in R/Sql? The data is in the...
How do I convert a character column into date in R/Sql? The data is in the format "01 02:52:12" i.e <day> <hr>:<min>:<sec> I want to convert this into Date time format.
What is the importance of reversing entries? Why are they performed and what do they do?...
What is the importance of reversing entries? Why are they performed and what do they do? Are these entries required by all companies following GAAP? Please explain 100 word
I have the below program and I am having issues putting all the output in one...
I have the below program and I am having issues putting all the output in one line instead of two. how can I transform the program for the user enter format ( HH:MM) Like: Enter time using 24 format ( HH:MM) : " and the user enter format " 14:25 then the program convert. instead of doing two lines make all one line. Currently I have the user enter first the Hour and then the minutes but i'd like to...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT