本文介绍了两个不同的表在同一个数据库总和问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 我正在使用c sharp和MSSQL 2008 我有两个表首先是条目和第二个是CustomerTable,表格有2个文本框,用于余额。 1'表:条目 日期CustomerCode客户名称类型Pu 2-10-13 CS001印度G 100 2-10-13 CS001 Indian S 200 2-10-13 CS001印度G 300 2-10-13 CS001印度S 400 2-10-13 CS001印度G 300 2'表:CustomerTable CustomerCode客户名称PBalanceG PBalanceS CS001印度200 400 我想要pu和Pbalance的总和,其中date = input CustomerCode = input and type = input 喜欢 2-10-13 cs001印度g = 100 + 300 + 300 + 200(PBalanceG)= 900 2-10-13 cs001 indian s = 200 + 400 + 400(PBalanceS)= 1000 答案是 txtTextBox1 = 900 txtTextBox2 = 1000 我将此查询用于一个值(g) query = select from(txtPu),lblGBalance from Entry e INNER JOIN CustomerTable c ON e.txtCustomerCode = c.txtCustomerCode Group by e.txtCustomerCode; 它给出错误 列'CustomerTable.lblGBalance'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。 告诉我我的查询或任何其他价值(g,s)的解决方案有什么问题。解决方案 根据您遇到的错误,您还应该包含 CustomerTable .lblGBalance 你的列分组依据条款。 我这样做了: SELECT ( SELECT SUM(PU) FROM 条目 WHERE TYPE = ' G' )+ A.PBalanceG AS BalanceG,( SELECT SUM(PU) FROM 条目 WHERE TYPE = ' S')+ A.PBalanceS AS BalanceS FROM CustomerTable A INNER 加入 ENTRY B ON A.CustomerCode = B.CustomerCode WHERE A.CustomerCode = ' CS001' AND B. 日期 = ' 2-10-13' GROUP BY A.PBalanceG,A.PBalanceS 希望它有所帮助! :) HiI am using c sharp and MSSQL 2008I have two tables first is Entry and second is CustomerTable and form have a 2 textboxes for sum of balance.1'st table: EntryDate CustomerCode CustomerName Type Pu2-10-13 CS001 Indian G 1002-10-13 CS001 Indian S 2002-10-13 CS001 Indian G 3002-10-13 CS001 Indian S 4002-10-13 CS001 Indian G 3002'nd Table: CustomerTableCustomerCode CustomerName PBalanceG PBalanceSCS001 Indian 200 400I want to sum of pu and Pbalance where date= input CustomerCode=input and type=inputlike2-10-13 cs001 indian g=100+300+300+200(PBalanceG)=9002-10-13 cs001 indian s=200+400+400(PBalanceS)=1000Ans istxtTextBox1=900txtTextBox2=1000I am using this query for one value (g)query = "select sum(txtPu), lblGBalance from Entry e INNER JOIN CustomerTable c ON e.txtCustomerCode=c.txtCustomerCode Group By e.txtCustomerCode";it gives a errorColumn 'CustomerTable.lblGBalance' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Tell me what is wrong in my query or any other solution for both of value (g,s). 解决方案 According to your error encountered, you should also include the CustomerTable.lblGBalance column in you Group By Clause.I've done this:SELECT (SELECT SUM(PU) FROM Entry WHERE TYPE = 'G') + A.PBalanceG AS BalanceG , (SELECT SUM(PU) FROM Entry WHERE TYPE = 'S') + A.PBalanceS AS BalanceSFROM CustomerTable AINNER JOIN ENTRY B ON A.CustomerCode = B.CustomerCodeWHERE A.CustomerCode = 'CS001' AND B.Date = '2-10-13'GROUP BY A.PBalanceG, A.PBalanceSHope it helps! :) 这篇关于两个不同的表在同一个数据库总和问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 18:05