[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Total from a table in sql

Posted on 2011-04-26
5
Medium Priority
?
245 Views
Last Modified: 2012-05-11
Hi,

I have a table called T which looks like this

TL     Amt
H        2
N        70
C        10
B        Null
S        50

I need an sql statment to show the total of Amt field for each item in this table

Please help

Thanks,

R8VI
0
Comment
Question by:R8VI
5 Comments
 
LVL 3

Expert Comment

by:SreejithG
ID: 35465808
SELECT SUM(Amt) FROM T
0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 total points
ID: 35465815
select TL , Sum(amt) from T
group by TL

Or
select TL , Sum(isnull(amt,0)) from T
group by TL
0
 
LVL 9

Expert Comment

by:kaminda
ID: 35465818
Code should be this
SELECT TL, SUM(Amt) FROM T GROUP BY TL

Open in new window

0
 
LVL 4

Expert Comment

by:malikirfan28
ID: 35465824
for each item mean for each TL?
And B TL have NULL amount. Does it STRING NULL value or DB Null value? Can you please tell what is the type of Amt column (i.e. either its varchar or decimal)?
0
 
LVL 4

Expert Comment

by:malikirfan28
ID: 35465836
If this NULL is a db null value then you can use the following script.

select TL , Sum(isnull(amt,0)) from T
group by TL

and if it is String NULL then you can use the following script

select TL, sum(Amt) from
(select TL, case when isnumeric(Amt) = 1 then Amt else 0 end as Amt from T) as a
group by TL
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question