C#,.net求教,下面这段引用数据库求和,两个表连接,哪里写的有问题 [问题点数:20分]

luckychap 2017-08-01 05:32:19
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyWeb.oa.oa_LeaseContract
{
/// <summary>
/// Add_Main 的摘要说明。
/// </summary>
public partial class FinanceStatMain : BasePage
{
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!this.IsPostBack)
{
Bind();
}
}

private void Bind()
{
//------------------------------------------------------------------
string sql = string.Format(@"

select AddressSimple=isnull(AddressSimple,address),LessorBankName=isnull(LessorBankName,LessorName),a.*,b.*
FROM OA_LeaseContract a
inner join
(
select LTID,sum(Pay_Price) as Pay_Price,sum(Pay_Price2) as Pay_Price2 ,sum(Pay_Price3) as Pay_Price3 ,
sum(Get_Price) as Get_Price, sum(Get_Price2) as Get_Price2, sum(Get_Price3) as Get_Price3
from OA_LeaseContract_Finance b
{0}
group by b.LTID
) b on a.LTID=b.LTID
{1}
order by a.LTID desc"

, sqlPartFinance()
, sqlPartHT());
//绑定数据
DataGrid1.DataSource = WebLibrary.CsDB.GetTable(PubFunc.ConnectionString, sql).DefaultView;
DataGrid1.DataBind();
//-----------------------------------------------------------------------------------------------
}


private string sqlPartFinance()
{
string sWhere = " where 1=1 ";
if (!String.IsNullOrEmpty(Request["DateType"]))
sWhere += PubFunc.GetSqlSelectDate(Request, string.Format("b.{0}", Request["DateType"]), "BeginDate", "FinPlanDate");
//-----------------------------------------------------------------------------------------------
sWhere += PubFunc.GetSql_Part("a.HTID", Request["HTID"], "like")
+ PubFunc.GetSql_Part("a.LessorName", Request["LessorName"], "like")
+ PubFunc.GetSql_Part("a.Lessorphone", Request["Lessorphone"], "like")
+ PubFunc.GetSql_Part("a.ReceiptorName", Request["ReceiptorName"], "like")
+ PubFunc.GetSql_Part("a.LesseeName", Request["LesseeName"], "like")
+ PubFunc.GetSql_Part("b.FinPlanDate", Request["FinPlanDate"], "like")
+ PubFunc.GetSql_Part("a.HT_State", Request["HT_State"]);
//-----------------------------------------------------------------------------------------------
if (!String.IsNullOrEmpty(Request["Address"]))
sWhere += PubFunc.GetSqlSelectSplit("a.address", Request["address"]);
switch (Request["FinState"])
{
case "是":
sWhere += " and a.AuditDate is not null";
break;
case "否":
sWhere += " and a.AuditDate is null";
break;
}
//-----------------------------------------------------------------------------------------------
return sWhere;
}
private string sqlPartHT()
{
string sWhere = " where 1=1 ";
if (!String.IsNullOrEmpty(Request["DateType"]))
sWhere += PubFunc.GetSqlSelectDate(Request, string.Format("b.{0}", Request["DateType"]), "FinPlanDate", "EndDate");
//-----------------------------------------------------------------------------------------------
sWhere += PubFunc.GetSql_Part("a.HTID", Request["HTID"], "like")
+ PubFunc.GetSql_Part("a.LessorName", Request["LessorName"], "like")
+ PubFunc.GetSql_Part("a.Lessorphone", Request["Lessorphone"], "like")
+ PubFunc.GetSql_Part("a.ReceiptorName", Request["ReceiptorName"], "like")
+ PubFunc.GetSql_Part("a.LesseeName", Request["LesseeName"], "like")
+ PubFunc.GetSql_Part("b.FinPlanDate", Request["FinPlanDate"], "like")
+ PubFunc.GetSql_Part("a.HT_State", Request["HT_State"]);
//-----------------------------------------------------------------------------------------------
if (!String.IsNullOrEmpty(Request["Address"]))
sWhere += PubFunc.GetSqlSelectSplit("a.address", Request["address"]);
switch (Request["FinState"])
{
case "是":
sWhere += " and a.AuditDate is not null";
break;
case "否":
sWhere += " and a.AuditDate is null";
break;
}
//-----------------------------------------------------------------------------------------------
return sWhere;
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

}
#endregion
}
}
总是报错下面这个错误,求教应该怎么修改?
...全文
686 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2017-08-02
  • 打赏
  • 举报
回复
引用 6 楼 qq_38287011 的回复:
是的。。但是我不能在group by里加入这个字段,因为我求和。。日期不能求和
别用b.*,改成b.字段,b.字段。 *号会自动带上FinPlanDate,所以导致group by必须包含这个字段
luckychap 2017-08-01
  • 打赏
  • 举报
回复
引用 5 楼 hanjun0612 的回复:
那你怎么可以select AddressSimple=isnull(AddressSimple,address),LessorBankName=isnull(LessorBankName,LessorName),a.*,b.* b.*包含了FinPlanDate,那就必须出现在 group by 里。 除非你一个个字段打上去(不包括FinPlanDate)
是的。。但是我不能在group by里加入这个字段,因为我求和。。日期不能求和
正怒月神 版主 2017-08-01
  • 打赏
  • 举报
回复
那你怎么可以select AddressSimple=isnull(AddressSimple,address),LessorBankName=isnull(LessorBankName,LessorName),a.*,b.* b.*包含了FinPlanDate,那就必须出现在 group by 里。 除非你一个个字段打上去(不包括FinPlanDate)
luckychap 2017-08-01
  • 打赏
  • 举报
回复
引用 2 楼 hanjun0612 的回复:
select AddressSimple=isnull(AddressSimple,address),LessorBankName=isnull(LessorBankName,LessorName),a.*,b.* FROM OA_LeaseContract a inner join ( select LTID,sum(Pay_Price) as Pay_Price,sum(Pay_Price2) as Pay_Price2 ,sum(Pay_Price3) as Pay_Price3 , sum(Get_Price) as Get_Price, sum(Get_Price2) as Get_Price2, sum(Get_Price3) as Get_Price3 from OA_LeaseContract_Finance b {0} group by b.LTID ) b on a.LTID=b.LTID 这句话就没出现过finplandate,哪来的?
FinPlanDate'这列是b表里的一列。。因为他是日期,所以不能在group by里加入。。因为求和不能求日期。。。
luckychap 2017-08-01
  • 打赏
  • 举报
回复
FinPlanDate'这列是b表里的一列。。因为他是日期,所以不能在group by里加入。。因为求和不能求日期。。。
正怒月神 版主 2017-08-01
  • 打赏
  • 举报
回复
select AddressSimple=isnull(AddressSimple,address),LessorBankName=isnull(LessorBankName,LessorName),a.*,b.* FROM OA_LeaseContract a inner join ( select LTID,sum(Pay_Price) as Pay_Price,sum(Pay_Price2) as Pay_Price2 ,sum(Pay_Price3) as Pay_Price3 , sum(Get_Price) as Get_Price, sum(Get_Price2) as Get_Price2, sum(Get_Price3) as Get_Price3 from OA_LeaseContract_Finance b {0} group by b.LTID ) b on a.LTID=b.LTID 这句话就没出现过finplandate,哪来的?
luckychap 2017-08-01
  • 打赏
  • 举报
回复
FinPlanDate这列没提出来。。。怎么把这列提出来呢。。在group by里提出来。。倒是不报错了。。但是程序功能不对了。。这个字段是个日期,不能分组求和

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧