SQL SERVER
Write a SELECT statement that return the vendor name and the total number of accounts that apply to that vendor’s invoices. Filter the result set to include only the vendor who is being paid more than twice. (HINT: use Vendors table, Invoices table and InvoiceLineItems table).
TABLES:
Vendor
Columns: VendorID, VendorName, DefaultTermsID, DefaultAccountNo
Invoices
Columns: InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal, PaymentTotal, CreditTotal, TermsID
InvoiceLineItems
Columns: InvoiceID, InvoiceSequence, AccountNo, InvoiceLineItemAmount, InvoiceLineItemDescription
Please use below query:
SELECT v.VendorName,
COUNT(DISTINCT l.AccountNo) AS [Total Number Of Accounts]
FROM Vendor v JOIN Invoices i
ON v.VendorID = i.VendorID
JOIN InvoiceLineItems l
ON i.InvoiceID = l.InvoiceID
GROUP BY v.VendorName
HAVING COUNT(DISTINCT l.AccountNo) > 2
ORDER BY v.VendorName;
Explanation: We have to display VendorName and total number of Accounts, filtered by the vendor who has been paid more than twice
Alias for Vendor table is v, Invoices is i and InvoicesLineItems is l.
First line is used to give you Vendor Name and Second line is used to give you total number of accounts. You have to use inner join between Vendor and Invoices table o VendorId column to find the Invoices for Vendors and then result set would be inner joined with InvoicelineItems on InvoiceId using Group by clause on Vendor name to aggregate the Vendor Name based on Account No and then apply the filter as greater than 2. It should give you the desired output.
Screen sot of query for your reference:
Data in InvoiceLineItems table is as follows:
Data in Invoices is as follows:
Data in Vendor is as follows:
Output:
In case of any query, do comment.
Please rate the answer. Thanks.
Write a SELECT statement that return the vendor name and the total number of accounts that apply to that vendor’s invoices.
We have the following keys in the tables given:
Vendor: VendorID (Primary Key)
Invoices: InvoiceID, VendorID (Foreign Key - references VendorID from Vendor table)
InvoiceLineItems: InvoiceID (Foreign Key - references InvoiceID from Invoices table)
We can select VendorName directly from the Vendor table but in order to find the number of accounts which are related to a Vendor, we need to use the AccountNo field in the InvoiceLineItems table, which is indirectly related to Vendor through the Invoices table. The number of accounts can be found out be counting the distinct number of AccountNo that are related to a Vendor. The relationship is as follows:
(the left side of the period denotes the tablename, and the right side denotes column name)
InvoiceLineItems.InvoiceID => Invoices.InvoiceID => Invoices.VendorID => Vendor.VendorID
Such a relationship leads use to use joins to get a result.
The query is:
SELECT
VendorName,
COUNT(DISTINCT InvoiceLineItems.AccountNo) AS
Accounts
FROM Vendors
JOIN Invoices
ON Vendors.VendorID = Invoices.VendorID
JOIN InvoiceLineItems
ON Invoices.InvoiceID =
InvoiceLineItems.InvoiceID
GROUP BY Vendor.VendorID;
Filter the result set to include only the vendor who is being paid more than twice.
The above query can be modified to filter those Vendors which are being paid more than twice (more than two accounts), We use the HAVING keyword because results of aggregate functions (COUNT in this case) can only be filtered using the HAVING clause.
The query is:
SELECT
VendorName,
COUNT(DISTINCT InvoiceLineItems.AccountNo) AS
Accounts
FROM Vendors
JOIN Invoices
ON Vendors.VendorID = Invoices.VendorID
JOIN InvoiceLineItems
ON Invoices.InvoiceID =
InvoiceLineItems.InvoiceID
GROUP BY Vendor.VendorID
HAVING COUNT(DISTINCT InvoiceLineItems.AccountNo) > 2;
The above query is the final query.
Feel free to ask any doubts in the comments :)
SQL SERVER Write a SELECT statement that return the vendor name and the total number of...
SQL - write a select statement that returns four columns: vendorName, invoiceNumber, invoiceDate, and invoiceTotal. Return ONE ROW per vendor, representing the vendors invoice with the earliest date.
Write a select statement that returns the vendorname and paymentsum of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number of invoices is >5. CREATE TABLE InvoiceArchive InvoiceID int NOT NULL, VendorID int NOT NULL InvoiceNumber varchar(50) NOT NULL, InvoiceDate smalldatetime NOT NULL, Invoice Total money NOT NULL, Payment Total money NOT NULL, CreditTotal money NOT NULL, TermsID int NOT NULL, InvoiceDueDate smalldatetime...
Someone Please Help Me modify this in PHP I'm in desperate need I cant figure this out ... Make the following modifications: For the vendors table: Comment out the table-level primary key Change the VendorIDcolumn to be a column-level primary key Add a VendorEmail column to the bottom of the table definition (define the data type for the column as variable character and set it to not exceed 45 characters) After the lineItems table, add code to create a table...
WRITE A SQL QUERY SQL SERVER Write a SELECT statement that returns one column from the Customers table named FullName that joins the LastName and FirstName columns. -- Format this column with the last name, a comma, a space, and the first name like this: -- Doe, John -- Sort the result set by last name in ascending sequence. -- Return only the contacts whose last name begins with a letter from M to Z. -- ...
#6 Write a SELECT statement that returns these columns: The count of the number of orders in the Orders table The sum of the tax_amount columns in the Orders table Write a SELECT statement that returns one row for each category that has products with these columns: The category_name column from the Categories table The count of the products in the Products table The list price of the most expensive product in the Products table Sort the result set so...
QUESTION: (Answer in SQL) Write an SQL query to find all pairs of users (A,B) such that both A and B have blurted on a common topic but A is not following B. Your query should print the names of A and B in that order. BACKGROUND INFO: Users can post their thoughts in form of short messages that we call “blurts”. When signing up, users need to provide their email and a password of their choice. In addition, they...
Prepare and execute each of the queries listed using the "Adventureworks2012" database in SQL: Server: http://msftdbprodsamples.codeplex.com/releases/view/93587 When all of your queries are complete, cut and paste the SQL Syntax into a word document. In order to see what the column names are, you need to click on the table and then Columns to see the field names. Make sure to include column headings that make sense in the queries (use the as “Field Name” after the field selected). Multi-table Queries...
Question 1.Write a SQL statement for each of the following questions based on the above tables (50 Points). 1) Create “Enrollment” table. 2) Change the credits of “Database” course from 2 to 3. 3) List the course titles (not Course_No) and grades John Doe had taken. 4) Calculate average of Café Balance, and name new field as Average Balance. 5) Create a roster for “Database” course (list student ID, last name, first name, major, and status for each student enrolled...
Question : Write MS-SQL Server Script for : (check for null at all times) 1) Select all ManagerIDas SupervisorNumber from table EmpJob (if managerID is null, do not include in the output) Table name : EmpJob Available Data : ManagerID 553434 NULL 2134323 324343 Expected output : Supervisor Number 553434 2134323 324343 2) Select name (attribute) the as branch(new col name) from table FoLocation (check for null. If null no data in the output) table name : FoLocation Available Data...
If possible please just send the SQL statement. Thank you Each question within deliverable 3 must begin on a new page and be sure to document the question as the title of each item at the top of each page. Also, using a 12-point font, include the SQL statement and then provide a screen shot of each query. The screen shots must include both the SQL statement and the results for each item below based on the data entered in...