Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am using c sharp and MSSQL 2008

I have two tables first is Entry and second is CustomerTable and form have a 2 textboxes for sum of balance.


1'st table: Entry

Date CustomerCode CustomerName Type Pu
2-10-13 CS001 Indian G 100
2-10-13 CS001 Indian S 200
2-10-13 CS001 Indian G 300
2-10-13 CS001 Indian S 400
2-10-13 CS001 Indian G 300

2'nd Table: CustomerTable

CustomerCode CustomerName PBalanceG PBalanceS

CS001 Indian 200 400

I want to sum of pu and Pbalance where date= input CustomerCode=input and type=input

like

2-10-13 cs001 indian g=100+300+300+200(PBalanceG)=900
2-10-13 cs001 indian s=200+400+400(PBalanceS)=1000

Ans is

txtTextBox1=900
txtTextBox2=1000

I 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 error

Column '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).
Posted

Hey there,

This is a sample query can get you the sums that you need,

SQL
SELECT 
(Select SUM(E1.PU) FROM Entry E1 WHERE E1.[TYPE] = 'G' AND E1.[Date] = '2-10-13' AND E1.CustomerCode='CS001') + C.PBalanceG AS BalanceG,
(Select SUM(E2.PU) FROM Entry E2 WHERE E2.[TYPE] = 'S' AND E2.[Date] = '2-10-13' AND E2.CustomerCode='CS001') + C.PBalanceS AS BalanceS
FROM CustomerTable C
WHERE C.CustomerCode = 'CS001'


Let me know if it helps.

Azee...
 
Share this answer
 
Comments
indiancodinglove 11-Oct-13 4:34am    
Sir its working thanks a lot.
Quote:
Column 'CustomerTable.lblGBalance' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
According to your error encountered, you should also include the CustomerTable.lblGBalance column in you Group By Clause.
I've done this:
SQL
SELECT
    (SELECT SUM(PU) FROM Entry WHERE TYPE = 'G') + A.PBalanceG AS BalanceG  ,
    (SELECT SUM(PU) FROM Entry WHERE TYPE = 'S') + A.PBalanceS AS BalanceS
FROM CustomerTable A
INNER JOIN ENTRY B ON A.CustomerCode = B.CustomerCode
WHERE A.CustomerCode = 'CS001'
    AND B.Date = '2-10-13'
GROUP BY
 A.PBalanceG, A.PBalanceS

Hope it helps! :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900