SQL SUM columns only if they have different IDs

Hi there,

I am having some issues with my SQL query which is slightly complicated with many columns.
Below, I am providing a strip down version to show the exact problem.

I have the following table:

10052|IN  |13.0    |300|
10052|IN  |13.0    |300|
10052|IN  |11.0    |300|
10053|EX  |2.0     |300|
10053|IN  |6.0     |300|
10053|IN  |16.0    |300|
10053|OB  |13.0    |300|

I am doing the following query to find the total IN and Duration.

SELECT Line, SUM(CASE WHEN Type='IN'  THEN 1 ELSE 0 END) AS TotalInbound, SUM(CASE WHEN Type='IN' THEN Duration ELSE 0 END) AS TotalInboundDuration FROM TABLE WHERE Line <> '0000' AND Line <> '' GROUP BY Line;

This results in 300| 5| 59

However, I only wanted to count the IN's if they have different LogID.  So, here I want to get

300 | 2 | 59 as the result.

How can I fix it.  Thank you.
Who is Participating?

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

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.

Kyle AbrahamsSenior .Net DeveloperCommented:
This would be my attempt:

SELECT Line, SUM(CASE WHEN t1.Type='IN'  THEN 1 ELSE 0 END) AS TotalInbound, SUM(CASE WHEN t1.Type='IN' THEN t1.Duration ELSE 0 END) AS TotalInboundDuration FROM 
table t1
WHERE t1.Line <> '0000' AND t1.Line <> '' 
and exists(select logid from table where line = t1.line and logid != t2.logid)

Open in new window

The other way to do it would be to fine all of the lines with differing log ids first, and then join on that:

select distinct t1.line 
into #lines
table t1 join 
table t2 on t1.line = t2.line and t1.logId != t2.LogID

Open in new window

and then your query would add in the where:

and t1.line in (select line from #lines)

Open in new window

Jose TorresCertified Database AdministratorCommented:
You can also try this
SELECT Line, COUNT(1) TotalInbound, SUM(Duration) TotalInboundDuration
	SELECT LogID, Line, SUM(Duration) Duration
	WHERE Type = 'IN'
) A

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
awking00Information Technology SpecialistCommented:
select line, count(distinct logid), sum(duration)
from yourtable
where type = 'IN'
group by line;
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
Query Syntax

From novice to tech pro — start learning today.