sql数据库同一表 多字段求和 输出
请问一下 数据库字段求和,一个的代码我知道这样写 用GridView输出
SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-39DAEF666\SQL2005;Initial Catalog=XSXJGL;uid=sa;pwd=sa2008");
SqlDataAdapter ad = new SqlDataAdapter("SELECT SUM(CSRQ)FROM XS WHERE XB='建筑一所'", conn);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
还想求同一个表中 别的字段之和,不知道能不能同时用GridView1显示.
sql语句如:SELECT SUM(CSRQ)FROM XS WHERE XB='建筑二所'"
SELECT SUM(CSRQ)FROM XS WHERE XB='建筑三所'"
SELECT SUM(CSRQ)FROM XS WHERE XB='建筑五所'"
[解决办法]
?
SELECT
SUM(case when XB='建筑一所' then CSRQ else 0 end) as [建筑一所],
SUM(case when XB='建筑二所' then CSRQ else 0 end) as [建筑二所],
SUM(case when XB='建筑三所' then CSRQ else 0 end) as [建筑三所],
SUM(case when XB='建筑五所' then CSRQ else 0 end) as [建筑五所]
FROM XS