1
string str = string.Format(@"select m.pcode, m.fyyear, m.salary, m.ta,
                           m.contigency, m.nrc, m.institcharges, m.others,
                           y.pcode as ypcode,y.fyyear as yfyear,y.yearlyalloc,
                           y.salary as sal1,y.ta as ta1,y.contigency as cont1,
                           y.nrc as nrc1,y.institcharges as inst1,
                           y.others as other1                                      
                           FROM monthly AS m inner join yearly y
                           on m.pcode=y.pcode 
                           where m.pcode=('" + DropDownList1.SelectedItem.ToString() 
                           + "' ) AND 
                             y.fyyear like('" + DropDownList2.SelectedItem.ToString()
                           + "')group by m.pcode,m.fyyear", con); 
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataTable dtNew = new DataTable();
da.Fill(dtNew);
GridView1.DataSource = dtNew;
GridView1.DataBind();

在这段代码中,月表包含我想以这种方式显示的列:

select m.pcode, m.fyyear, sum(m.salary) as msal, sum(m.ta) as mta, 
sum(m.contigency) as m cont, sum(m.nr) as mnrc, sum(m.institcharges) as minstit, 
sum(m.others) as mothers  
FROM monthly AS m 
where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' )
and m.fyyear like('" + DropDownList2.SelectedItem.ToString() + "')
group by m.pcode,m.fyyear

和年度表数据为:

"selecet y.pcode as ypcode,y.fyyear as yfyear,y.yearlyalloc,y.salary as sal1,
 y.ta as ta1,y.contigency as cont1,y.nrc as nrc1,y.institcharges as inst1,
 y.others as other1 
 FROM yearly AS y
 where y.pcode=('" + DropDownList1.SelectedItem.ToString() + "' )
 and y.fyyear like('" + DropDownList2.SelectedItem.ToString() + "') 
 group by y.pcode,y.fyyear

我希望这些将这两个查询结合起来,我使用 join 编写的上述复合查询正在工作,但它不允许我使用 sum 和 group by 函数。请帮忙

4

0 回答 0