Using SQL SUM with multiple conditions

Hi there,

I have the following table-1 and I want to query it to generate data as given in table-2.  I want to tabulate items in table-1 to give a summary of total types in per day, total types of out per day.  Please note when a Type is X, the duration of it should be added to the In Duration for the number with the same ID.
For example, on 09/12/2015 there were three entries with Type=in for the number 200.
But, there are also two type X for the same IDs(ID123, ID 240)
Therefore the TotalInPerDay should be 3 and TotalInDurationPerDay should include the Type X as well giving Duration = 17.

I am using something similar to the following query which does not include the X type calculation.
select
      Number, Date,
      sum(case when type='in' then 1 else 0 end) TotalInPerDay,
      sum(case when type='in' then Duration else 0 end) TotalInDurationPerDay,
      sum(case when type='out' then 1 else 0 end) TotalOutPerDay,
      sum(case when type='out' then Duration else 0 end) TotalDurationPerDay
from Data
group by Number, Date

Can you please help me with this query syntax.  
Thank you.

Table-1
ID          |  Number            | Date             | Time    | Type      | Duration |
123       | 200          |09/12/2015 | 11:20    |        in     |     2           |
123       |      200          |09/12/2015 | 11:20    |         X     |     2           |
234       |      300          |09/12/2015| 10:15      |        in    |      3          |
235       |      200          |09/12/2015 | 10:00     |        in     |     2          |      
236       |      200          |09/12/2015 | 13:20    |        out   |      1        |
237       |      200          |09/11/2015 | 10:00     |        in     |      2        |      
238       |      200          |09/11/2015 | 14:20    |        out   |     1         |
239       |      200          |09/11/2015 | 15:20    |        out   |    1          |
240       |      200          |09/12/2015 | 09:00     |        in     |     5         |    
240       |      200          |09/12/2015 | 09:00     |        X      |     6         |      
241       |      300          |09/11/2015| 10:15      |        out   |    3        |
242       |      300          |09/12/2015| 10:15      |        in    |      3        |
243       |      300          |09/12/2015| 10:15      |        in    |     3        |
245       |      300          |09/12/2015| 10:15      |        in    |      4       |
245       |      300          |09/12/2015| 10:15      |       X|       22       |


Number |Date| TotalInPerDay |TotalInDurarionPerDay|TotalOutPerDay|TotalOutDurarionPerDay|
200           |09/11/2015 | 1 | 2 | 2|2
200           | 09/12/2015| 3 | 17| 1|1
300           | 09/11/2015|0| 0| 1| 3
300           | 09/12/2015|4| 35| 0| 0
ambuliAsked:
Who is Participating?
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.

ambuliAuthor Commented:
I tried the following but, it does not include the Duration from X.

select
      Number, Date,
      sum(case when type='in' then 1 else 0 end) TotalInPerDay,
      sum(case when type IN('in', 'X') then Duration else 0 end) TotalInDurationPerDay,
      sum(case when type='out' then 1 else 0 end) TotalOutPerDay,
      sum(case when type='out' then Duration else 0 end) TotalDurationPerDay
from Data
group by Number, Date
0
chaauCommented:
Is it possible that the 'X' is not exactly 'X'? Is it possible that it has leading/trailing spaces? Try this to see if it makes any difference
select
      Number, Date,
      sum(case when type='in' then 1 else 0 end) TotalInPerDay,
      sum(case when TRIM(type) IN('in', 'X') then Duration else 0 end) TotalInDurationPerDay,
      sum(case when type='out' then 1 else 0 end) TotalOutPerDay,
      sum(case when type='out' then Duration else 0 end) TotalDurationPerDay
from Data
group by Number, Date 

Open in new window

0

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
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.