4
Answers

How to print out the summation of a column

Fares Ayyad

Fares Ayyad

8y
307
1

Hello everyone,

I write a query that shows multiple values in multiple columns from multiple:

  1. Select i.cdin_cdindexid as cntdp, coalesce((i.cdin_NoofPackages),0) as sqty,coalesce((i.cdin_WT),0) as swt ,coalesce((i.cdin_volumewt),0) as svwt ,coalesce((i.cdin_MortgageAmount),0) as samt  
  2.                            from cdindex i inner join   company c on i.cdin_CompanyId =c.Comp_CompanyId  inner join Territories t on i.cdin_Secterr =t.Terr_TerritoryID left outer join Performainv p on i.cdin_cdindexid=p.pinv_cdindexid  
  3.                            where(cdin_deleted Is null And c.comp_deleted Is null And t.Terr_Deleted Is null and p.pinv_deleted is null and (p.pinv_InvoiceProperty ='01' or p.pinv_InvoiceProperty is null ) and (p.pinv_Status in('Draft','Posted'or p.pinv_status is null) )  
  4.                            and i.cdin_startunstufdate between '2016-07-01'and '2016-07-11'  
  5.                            group by i.cdin_cdindexid,i.cdin_NoofPackages,i.cdin_WT,i.cdin_volumewt ,i.cdin_MortgageAmount  
And this is the result:
 
 
 
 

my question is how to get the summation of one column an print it in asp label tool.

for Example i want to get the summation of cntdp and represent the result in asp label using C#, if it's possible.

Thank You All.

with my respect.

Answers (4)
2
Midhun T P

Midhun T P

NA 19.7k 281.1k 8y
Hi,
You can get the sum of a datatable column with below code -
object sum = dt.Compute("SUM(cntdp)", "");
Accepted
1
Midhun T P

Midhun T P

NA 19.7k 281.1k 8y
Hi,

Happy to see, it solved your problem.
To get count of distinct values in a row, you can use below code -
int countDepInvo = table.AsDataView().ToTable(true, "depInvo").Rows.Count;

Please mark it as answer, if the solution was helpful to you as it helps others with same problem!
0
Fares Ayyad

Fares Ayyad

NA 36 1.7k 8y
<h4 style="outline: none 0px; color: #333333; font-family: &quot;Open Sans&quot;, sans-serif; font-size: 14px; background-color: #ffffff;"><a data-userid="9088f9" href="http://www.c-sharpcorner.com/members/midhun-tp" style="outline: none 0px; none; cursor: pointer; font-weight: normal; font-family: Roboto, sans-serif; color: #ff6600;">Midhun T P</a></h4><div>Thank you, this solved the problem but</div><div>i want to retrieve count of a column with distinct records how to get that. </div><div>i mean can i do this by c# or by sql statement like.</div><div></div><div>I mean can i get DISTINCT values from this C# statement:</div><div><div class="dp-highlighter"><div class="bar"></div><ol start="1" class="dp-c"><li class="alt"><span><span>countDepInvo=table.Compute(</span><span class="string">"Count(depInvo)"</span><span>,</span><span class="string">""</span><span>);</span></span></li></ol></div></div><div></div>
0
Nitin Sontakke

Nitin Sontakke

NA 11.7k 2.2k 8y
If the above query is part of stored procedure, you can have another query below this and handle multiple result sets in your data access layer in C#.
If you are using SQL Server 2012 and above, you can use advance windowing functions to get the sum in the same row and pick the value only from the last row.
Or make a separate db call just for the sum.