SQL select for total values of columns

Hi there,
I have a table that looks like below.

Line, Amount, Type
303, 20, OUT
300, 10, OUT
301, 20, IN
301, 10, OUT
300, 10, IN
300, 30, OUT
300, 20, IN
301, 10, IN
300, 40, IN
I want to generate the following data( Basically for each line find the total number of times it appears
as IN and OUT as well as the total values of the amount for each Line.

Line, NumIN, NumOUT, TotalInAmount, TotalOutAmount
300, 3, 2, 70, 40
301, 2, 1, 30, 10
303, 0, 1, 20

How can I generate this using a single SQL Query.  Thank you.
ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
<total air code>
Create two subqueries, one for the IN and OUT aggregates, then join on a main query of distinct lines

SELECT yt.line, yt_in.NumIN, yt_in.TotalINAmount, yt_out.TotalOUTAmount
FROM (SELECT DISTINCT Line FROM YourTable) yt
   LEFT JOIN (
      SELECT Line, COUNT(Type) as NumIN, SUM(Amount) as TotalINAmount
      FROM YourTable
      WHERE Type='IN'
      GROUP BY Line) yt_in ON yt.line = yt_in.line
   LEFT JOIN (
      SELECT Line, COUNT(Type) as NumOUT, SUM(Amount) as TotalOUTAmount
      FROM YourTable
      WHERE Type='OUT'
      GROUP BY Line) yt_out ON yt.line = yt_out.line
ORDER BY yt.line

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
quick and dirty:

create table #test( Line int, Amount int, Type varchar(10))

insert into #test values (303, 20, 'OUT')
insert into #test values (300, 10, 'OUT')
insert into #test values (301, 20, 'IN')
insert into #test values (301, 10, 'OUT')
insert into #test values (300, 10, 'IN')
insert into #test values (300, 30, 'OUT')
insert into #test values (300, 20, 'IN')
insert into #test values (301, 10, 'IN')
insert into #test values (300, 40, 'IN')

select DISTINCT T.Line, TIn.Num AS NumIn, TOut.Num AS NumOut, TIn.SumAmount AS TotalInAmount, TOut.SumAmount AS TotalOutAmount  
FROM #test AS T
LEFT JOIN (
	SELECT line, COUNT(*) AS Num, SUM(Amount) AS SumAmount FROM #test WHERE Type = 'IN' GROUP BY Line 
) AS TIn
ON TIn.Line = T.Line
LEFT JOIN (
	SELECT line, COUNT(*) AS Num, SUM(Amount) AS SumAmount FROM #test WHERE Type = 'OUT' GROUP BY Line 
) AS TOut
ON TOut.Line = T.Line

DROP TABLE #test

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
or try this:

SELECT Line, 
 sum(case when Type = 'IN' then 1 ELSE 0 end) As NumIN,
 sum(case when Type = 'OUT' then 1 ELSE 0 end) As NumOUT,
 sum(case when Type = 'IN' then Amount ELSE 0 end) As TotalInAmount,
 sum(case when Type = 'OUT' then Amount ELSE 0 end) As TotalOutAmount
 FROM yourTable
 GROUP BY Line
Order By Line

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.