SUM,JOIN,GROUP BY,ORDER BY,PARTITION,OVER。我错过了什么?

时间:2013-11-02 21:00:06

标签: sql oracle join group-by sum

我需要一些帮助。我有两个表,我知道需要加入,而不是问题。我需要显示的是发票中购买的所有产品的发票号,发票日期和总金额。信息需要按发票编号按降序排序,然后按发票日期按升序排序。到目前为止,我所犯的错误是:

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;

在我拔掉头发之前,我们非常感激。

更新

我已将声明更新为:

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;

我正在获取数据而不是错误。我现在遇到的问题是我需要数据读取按发票编号按降序排序和按发票日期按升序排序,所以我假设我需要两个数据输出。我该怎么办?

3 个答案:

答案 0 :(得分:0)

只有那些在group by中使用的列才能在我认为的select子句中被检索。

可能需要包含INV_DATE,LINE_PRICE,然后在select子句中检索它们的聚合。

在java2s.com中举例说明一些有用的网站。

此致 斯里里

答案 1 :(得分:0)

尝试GROUP BY LINE.INV_NUMBER, INVOICE.INV_DATE

根据您使用的DBMS,它可能是也可能不是问题。

答案 2 :(得分:0)

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;