简体   繁体   中英

SUM, JOIN, GROUP BY, ORDER BY, PARTITION, OVER. What am I missing?

I need a bit of help. I have two tables that I know needs to be joined, not a problem. What I need to display is the invoice number, invoice date and the total dollar amount for all products purchased in the invoice. The info needs to be ordered by invoice number in descending order and then by invoice date in ascending order. What I have so far, and is throwing errors, is:

SELECT          LINE.INV_NUMBER, 
                INVOICE.INV_DATE, 
                SUM(LINE.LINE_PRICE) 
           FROM LINE 
FULL OUTER JOIN INVOICE ON LINE.INV_NUMBER=INVOICE.INV_NUMBER 
       GROUP BY LINE.INV_NUMBER 
       ORDER BY LINE.INV_NUMBER DESC, 
                INVOICE.INV_DATE ASC;

Any help is appreciated before I pull all my hair out.

Update:

I have updated my statement to :

SELECT LINE.INV_NUMBER AS "Line Number", 
       INVOICE.INV_DATE AS "Invoice Date", 
       SUM(LINE.LINE_PRICE) AS "Total Sales" 
  FROM INVOICE FULL OUTER JOIN LINE ON INVOICE.INV_NUMBER=LINE.INV_NUMBER 
 GROUP BY LINE.INV_NUMBER, INVOICE.INV_DATE
 ORDER BY LINE.INV_NUMBER DESC, INVOICE.INV_DATE ASC;

and I am getting data instead of errors. What I am now having a problem with is I need the data to read Ordered by the Invoice Number in a descending order and by Invoice Date in ascending order, so I am assuming that I need two data outputs. How would I do that?

Only those columns which are used in group by can be retrieved in the select clause I believe.

May be you need to include INV_DATE, LINE_PRICE and then retrieve their aggregate's in the select clause.

refer to few group by examples in java2s.com that is a useful site.

Regards, Srihari

Try GROUP BY LINE.INV_NUMBER, INVOICE.INV_DATE

Depending on the DBMS you're using, it may or may not be the issue.

SELECT LINE.INV_NUMBER AS "Line Number", 
       INVOICE.INV_DATE AS "Invoice Date", 
       SUM(LINE.LINE_PRICE) AS "Total Sales" 
  FROM INVOICE FULL OUTER JOIN LINE ON INVOICE.INV_NUMBER=LINE.INV_NUMBER 
 GROUP BY LINE.INV_NUMBER, INVOICE.INV_DATE
 ORDER BY INVOICE.INV_DATE ASC, LINE.INV_NUMBER DESC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM