2010-11-27 37 views
0

相似的問題我發現在mySQL query - show most popular item 但我需要一點變化,併爲VB6 ADO。vb6 ADO查詢 - 顯示最熱門項目

我想創建一個流行(最賣)的物品清單,沒有日期範圍。

表的一個例子:

+------------+---------------------+-------+ 
| date  | item    | qty | 
+------------+---------------------+-------+ 
| 2009-08-02 | Apple    |  5 | 
| 2009-08-03 | Pear    |  2 | 
| 2009-08-04 | Peach    |  4 | 
| 2009-08-05 | Apple    |  1 | 
| 2009-08-06 | Apple    |  3 | 
+------------+---------------------+-------+ 

結果我需要:

+------------+------------+-------+ 
| sl| item |   | total | 
+------------+------------+-------+ 
| 1 | Apple |   |  9 | 
| 2 | Peach |   |  4 | 
| 3 | Pear |   |  2 | 
+------------+------------+-------+ 
+1

是什麼類型的數據庫?前端技術並不重要,在這種情況下,重要的是它所基於的數據庫和sql方言。通過在VB6中使用ODBC,您可以從任何數據庫檢索數據。 – ArBR 2010-11-27 05:16:48

回答

1

假設你已經有一個ADODB.Connection:

Dim oRS as New ADODB.Recordset 
dim sSql as string 

sSql = "SELECT item, SUM(qty) FROM myTable GROUP BY item ORDER BY Item" 
oRS.Open sSql, oDBConn, adOpenForwardOnly, adLockReadOnly 
Do While Not oRS.EOF 
    '// Do something with the data' 
    oRS.MoveNext 
Loop 
oRS.Close