question

trail8631 avatar image
trail8631 asked

Select statement which ansrews this question!!

Write a SELECT statement that answers this question: Which invoices have a PamenTotal that's greater than the average PaymentTotal for all paid Invoices? Return the InvoiceNumber and InvoiceTotal for each invoice.
selecthomework
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

trail8631 avatar image trail8631 commented ·
SELECT PaymentTotal > AVG, InvoiceNumber, InvoiceTotal FROM Invoices
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
Is there one row for each invoice, or is it an invoice detail table where you need to sum multiple rows for each invoice?
0 Likes 0 ·
Tim avatar image
Tim answered
Since this appears to be homework, please post what you have written so far and we will help out.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

trail8631 avatar image trail8631 commented ·
SELECT PaymentTotal > AVG, InvoiceNumber, InvoiceTotal FROM Invoices
0 Likes 0 ·
TimothyAWiseman avatar image
TimothyAWiseman answered
I do not think it appropriate to hand you the query, but based on what you have so far you might look at: 1. Moving comparisons out of the column list and into a WHERE clause after the FROM clause 2. Getting your Average payment total for the paid invoices from a subquery (depending on the structure of the table you may need a where clause inside that subquery to ensure you get only the paid ones....)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image
Blackhawk-17 answered
Go to the Microsoft website, search for **SQL Server Books OnLine**, bookmark it, and then look up HAVING.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ozamora avatar image
ozamora answered
SELECT invoice_no, PaymentTotal FROM Invoice WHERE PaymentTotal > (SELECT AVG(PaymentTotal) FROM Invoice WHERE paid = 'Y') AND paid = 'Y'
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image Scot Hauder commented ·
Hi Oscar, it's not the paid='Y' I was alluding to...the query probably will not compile because you cannot have an aggregate in the WHERE clause with the syntax above
0 Likes 0 ·
ozamora avatar image ozamora commented ·
Ok.what's what you are alluding to? Maybe I am misunderstanding the question. You can have where clauses in aggregate queries.
0 Likes 0 ·
Scot Hauder avatar image Scot Hauder commented ·
you're right,my fault in my test db I have an existing invoice table so I used invoice2..when I copied your query I only renamed the first leading to Msg 147, Level 15, State 1, Line 3 An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. I knew aggregates could appear in the where clause but saw the error and thought something might be slightly off with the syntax
0 Likes 0 ·
Scot Hauder avatar image
Scot Hauder answered
;WITH Invoices AS ( SELECT Invoice_no, PaymentTotal, AVG(PaymentTotal) OVER(PARTITION BY (SELECT NULL))[Average] FROM Invoice WHERE Paid = 'Y' ) SELECT Invoice_no, PaymentTotal FROM Invoices WHERE PaymentTotal > Average
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image Scot Hauder commented ·
ozamora has the better, more efficient answer--this is just an alternative
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.