有一个表。字段 id  A费用   B费用 C费用
               1   20      30    40
               1   30      40    30
               2   10      20    30
               2   20      30    40
               2   10      40    20
               3   30      30    20
               4   .........
               ....................我想求出同一id的所有费用值。即id1的费用为(20+30+40)+(30+40+30)
在unix下,用select id,sum(A+B+C) from account group by id;就可以实现但是我现在用ODBC连接数据库。用
CString sql="select id,sum(A+B+C) from account order by id"
CMyRecordset m_pRecordset->Open(AFX_DB_USE_DEFAULT_TYPE,sql);不行啊,一运行就出错。是不是Open中的SQL不支持sum啊?还是说CMyRecordset要进行什么字段的绑定?应该有人做过数据库的行列的求和计算吧。各位大侠帮帮忙啊。等了一天了,很急的。CSDN有时候又连不上来。提供你们的做法也行啊。
谢谢!!!!分不够可以再加,答在http://expert.csdn.net/Expert/topic/1133/1133467.xml?temp=.7003748也行

解决方案 »

  1.   

    好象是
    select id,sum(A+B+C) as result from account order by id
    然后取result的字段值
      

  2.   

    to jiangsheng:什么意思啊?具体点好吗?to Ah:在VC中怎么写啊?
      

  3.   

    CRecordset m_pset;CString sSQL="select id,sum(A+B+C) from account";m_pset->Open(CRecordset::forwardonly,sSQL);m_pset->GetFieldValue((short)0,str1);
    m_pset->GetFieldValue((short)1,str2);str1为id值,str2为sum值
      

  4.   

    有个问题,如果用m_pset->Open(CRecordset::forwardonly,sSQL);就不能m_pset->MoveFirst();和MovePrev()了,但是如果用CRecordset::snapshot;则会提示数据集没有绑定字段。是不是每次移动到最后一条,想要向前移,就必须Open(),然后一条条向后移动啊?