Query Syntax - Max, Sum

SELECT s.sctype, MAX(o.JTDCOST) Cost_JTD
FROM jcost o
INNER JOIN sccode s ON s.SCCODE = o.SCCODE
INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE o.JOB_ID = 7588            
AND o.DELETED = 'N'
AND o.WEDATE <= '2014-08-03'
GROUP BY o.SCCODE;

The above query returns 5 values that add up to 185,366.60
  13,683.75
  24,891.28
  38,862.48
107,562.82
       366.27

When it is incorporated in the query below the column returns 430,251.28

SELECT SUM(CostJTD)
FROM ccode c
LEFT JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
LEFT JOIN sctype t ON s.SCTYPE = t.SCTYPE_ID
LEFT JOIN job j ON c.JOB_ID = j.JOB_ID
      LEFT JOIN (SELECT s.SCTYPE COST, MAX(o.JTDCOST) CostJTD
      FROM jcost o
      LEFT JOIN sccode s ON s.SCCODE = o.SCCODE
      LEFT JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
      WHERE o.JOB_ID = 7588            
      AND o.DELETED = 'N'
      AND o.WEDATE <= '2014-08-03'
      GROUP BY s.SCTYPE) sub_query2 ON s.SCTYPE = sub_query2.COST
WHERE j.JOB_ID = 7588
AND c.DELETED = 'N'
AND c.POPU > 0
AND s.SCTYPE NOT IN(8,9,14)
GROUP BY s.SCTYPE;

it should be the 185,366.60
hdcowboyazAsked:
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.

dsackerContract ERP Admin/ConsultantCommented:
Without knowing your data, what jumps out immediately is your grouping is different in the two queries. If your second query is supposed to join to the exact same subquery that we see in your first query, you're grouping them differently.

First query is grouped by SCCODE. The second is grouped by SCTYPE. Without knowing your data, anything beyond this is a bit of a guess.
0
hdcowboyazAuthor Commented:
When I modify it to the below query I get 741,466.40

SELECT SUM(CostJTD)
FROM ccode c
LEFT JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
LEFT JOIN sctype t ON s.SCTYPE = t.SCTYPE_ID
LEFT JOIN job j ON c.JOB_ID = j.JOB_ID
      LEFT JOIN (SELECT s.SCTYPE COST, MAX(o.JTDCOST) CostJTD
      FROM jcost o
      LEFT JOIN sccode s ON s.SCCODE = o.SCCODE
      LEFT JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
      WHERE o.JOB_ID = 7588            
      AND o.DELETED = 'N'
      AND o.WEDATE <= '2014-08-03'
      GROUP BY o.SCCODE) sub_query2 ON s.SCTYPE = sub_query2.COST
WHERE j.JOB_ID = 7588
AND c.DELETED = 'N'
AND c.POPU > 0
AND s.SCTYPE NOT IN(8,9,14)
GROUP BY s.SCTYPE;
0
chaauCommented:
Why don't you just use:
SELECT SUM(Cost_JTD) FROM(
SELECT s.sctype, MAX(o.JTDCOST) Cost_JTD
FROM jcost o
INNER JOIN sccode s ON s.SCCODE = o.SCCODE
INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE o.JOB_ID = 7588            
AND o.DELETED = 'N'
AND o.WEDATE <= '2014-08-03'
GROUP BY o.SCCODE)

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

hdcowboyazAuthor Commented:
310,045.40
0
hdcowboyazAuthor Commented:
I just need to SUM this query...

SELECT MAX(o.JTDCOST) CostJTD
FROM jcost o
INNER JOIN sccode s ON s.SCCODE = o.SCCODE
INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE o.JOB_ID = 7588            
AND o.DELETED = 'N'
AND o.WEDATE <= '2014-08-03'
GROUP BY o.SCCODE;
0
hdcowboyazAuthor Commented:
Got it....
0
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.