//ODBC open SQL server database
CDatabase db;
db.OpenEx("DRIVER={SQL Server};Server=Cell;DATABASE=zhg;UID=sa;PWD=;");//change Cell to your computer name
//change zhg to your database name
CRecordset rs(&db);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select type,AVG(price)'average price' From title where type<>'business' group by type");
while(rs.IsEOF()==false)
{
CString str;
CString strtype;
rs.GetFieldValue((short)0,strtype);
CDBVariant varprice;
rs.GetFieldValue(1,varprice);
str.Format("type:%s,avg(price):%f",strtype,varprice.m_dblVal);
AfxMessageBox(str);
rs.MoveNext();
}
rs.Close();
db.Close();