• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Help With SQL Query Again

So, before I asked a question regarding the below query, and it works great now. It takes the sum of each quarter sales for a each user for each year. Now, I wanted to total the 4 quarters, but it is always null, even though each sepearte quarter has a value...

Here is the query:

SELECT quotas.quotayear,lname,IsNull(q1t.tot,0) as '1st Quarter',IsNull(q2t.tot,0) as '3rd Quarter',IsNull(q3t.tot,0) as '1st Quarter',IsNull(q4t.tot,0) as '4th Quarter', q1t.tot + q2t.tot
FROM eusers
INNER JOIN quotas ON quotas.salesid = eusers.userid
LEFT OUTER JOIN (SELECT sum(tsc) as 'tot',proposal.salesid,quotas.quotayear
FROM priceinfo
INNER JOIN proposal ON proposal.sanum = priceinfo.sanum
INNER JOIN quotas on quotas.salesid = proposal.salesid
INNER JOIN quarters ON quarters.quotayear = quotas.quotayear where dajobfin between quarters.q1 AND quarters.q2
group by quotas.quotayear,proposal.salesid)
q1t ON q1t.salesid = eusers.userid and quotas.quotayear = q1t.quotayear

LEFT OUTER JOIN (SELECT sum(tsc) as 'tot',proposal.salesid,quotas.quotayear
FROM priceinfo
INNER JOIN proposal ON proposal.sanum = priceinfo.sanum
INNER JOIN quotas on quotas.salesid = proposal.salesid
INNER JOIN quarters ON quarters.quotayear = quotas.quotayear where dajobfin between quarters.q2 AND quarters.q3
group by quotas.quotayear,proposal.salesid)
q2t ON q2t.salesid = eusers.userid and quotas.quotayear = q2t.quotayear

LEFT OUTER JOIN (SELECT sum(tsc) as 'tot',proposal.salesid,quotas.quotayear
FROM priceinfo
INNER JOIN proposal ON proposal.sanum = priceinfo.sanum
INNER JOIN quotas on quotas.salesid = proposal.salesid
INNER JOIN quarters ON quarters.quotayear = quotas.quotayear where dajobfin between quarters.q3 AND quarters.q4
group by quotas.quotayear,proposal.salesid)
q3t ON q3t.salesid = eusers.userid and quotas.quotayear = q3t.quotayear

LEFT OUTER JOIN (SELECT sum(tsc) as 'tot',proposal.salesid,quotas.quotayear
FROM priceinfo
INNER JOIN proposal ON proposal.sanum = priceinfo.sanum
INNER JOIN quotas on quotas.salesid = proposal.salesid
INNER JOIN quarters ON quarters.quotayear = quotas.quotayear where dajobfin between quarters.q4 AND '1/1/2010'
group by quotas.quotayear,proposal.salesid)
q4t ON q4t.salesid = eusers.userid and quotas.quotayear = q4t.quotayear

Results look like:

2009      Shepard      0.00      0.00      0.00      971754.53      NULL
2010      Shepard      1051.34      0.00      0.00      0.00      NULL
2009      Austin      0.00      0.00      0.00      37089.40      NULL
2010      Austin      0.00      0.00      0.00      0.00      NULL

argh, why?
0
melegant99
Asked:
melegant99
1 Solution
 
igni7eCommented:
because you're adding nulls together

you sum by using the sum function and grouping data together

i dont have time right now but do this:

select col1, col2, sum(tot)
from
(
your query

) all data
group by some cols
0
 
melegant99Author Commented:
Or, do this to solve the problem:

isnull(q1t.tot,0) + isnull(q2t.tot,0)
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now