2014-02-26 15 views
0

我在JSP中使用SQL查詢,我正在尋找將我的數據庫的行與唯一字段分組。請看下面的例子。JSP-使用唯一字段名稱對數據庫行進行分組?

My current query output: 
Custid Value Quantity 
10001  200  5 
10001  300  1 
10002  200  3 
10002  100  4 
10002  300  2 

What I'm trying to achieve: 
Custid Value Quantity 
10001  200  5 
      300  1 
10002  200  3 
      100  4 
      300  2 

我已經使用使用下面的代碼,但只設法對字段,我可以得到它有一個字段(客戶ID)的工作,但是當我添加在另外兩個(價值,數量)。我收到錯誤。嘗試使用GROUP BY,ORDER BY和DISTINCT,無法使其工作。 請參閱代碼:

<sql:query dataSource="${bookdB}" var="result"> 
    SELECT * FROM Invoice 
    ORDER BY cust_id AND 
    GROUP BY cust_id; 
</sql:query> 


    <c:forEach var="row" items="${result.rows}"> 
    <tr> 
    <td><i><c:out value="${row.cust_id}"/></i></td> 
    <td><font color="black"><c:out value="${row.value}"/></font></td> 
    <td><font color="black"><c:out value="${row.quantity}"/></font></td> 
    </tr> 
    </c:forEach> 

任何幫助將不勝感激。謝謝。

回答

1

更新與MS Access語法:

select iif((value = grp_value and quantity = grp_quantity),cust_id,'') as cust_id 
     value, 
     quantity 
    from (select x.*, grp.value as grp_value, grp.quantity as grp_quantity 
      from invoice x 
      join invoice grp 
      on x.cust_id = grp.cust_id 
     where grp.value = (select min(y.value) 
           from invoice y 
          where y.cust_id = x.cust_id) 
      and grp.quantity = (select min(y.quantity) 
           from invoice y 
           where y.cust_id = x.cust_id 
            and y.value = grp.value) 
     order by x.cust_id, x.value, x.quantity) x 
+0

感謝您的答覆。我添加了你的代碼,但得到了以下錯誤。 「:[Microsoft] [ODBC Microsoft Access驅動程序]語法錯誤(缺少運算符)在查詢表達式的情況下,當值= grp_value和數量= grp_quantity,然後cust_id else null結束'。」。有任何想法嗎??? –

+0

@ConorCaslin該語法與大多數數據庫(mysql,Oracle,sql server等)兼容,但不兼容Microsoft Access。在Access中,您將使用IIF語句而不是case語句,但我對語法不是100%熟悉。當我查閱它時,我會在一秒內編輯它。 –

+0

謝謝,如果你不介意的話。 –

相關問題