MySQL Update/Group By Querry

This query gives me a correct total amount........

UPDATE project j,
      (
      SELECT b.project_id, SUM(b.amount) AS samount
      FROM budget b
      GROUP BY b.project_id
      ) z, budget b
SET j.B_AMT = z.samount
WHERE z.project_id = j.project_id;

This is the same basic query but should filter out some data based on the "AND c.ctype = 'CapEx';" part of the query but it gives me the some total as above.

UPDATE project j,
      (
      SELECT b.project_id, SUM(b.amount) AS samount
      FROM budget b
      GROUP BY b.project_id
      ) z, budget b, capital c
SET j.BCAP_AMT = z.samount
WHERE z.project_id = j.project_id
AND b.capital_id = c.capital_id
AND c.ctype = 'CapEx';
myvpAsked:
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.

Pratima PharandeCommented:
Yes it is correct
  SELECT b.project_id, SUM(b.amount) AS samount
      FROM budget b
      GROUP BY b.project_id

this query returning you the sum of amount first
depending on the project_id
and you are filtering on c.ctype = 'CapEx'
If there is no match for this filter the project id itself get dropped the sum will not change
0
Pratima PharandeCommented:
try this

UPDATE project j,
      (
      SELECT b.project_id, SUM(b.amount) AS samount
      FROM budget b, capital c
where  b.capital_id = c.capital_id and AND c.ctype = 'CapEx
      GROUP BY b.project_id
      ) z
SET j.BCAP_AMT = z.samount
WHERE z.project_id = j.project_id;
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
gnoonCommented:
I think ctype filter is not applied to SUM() in the 2nd query. It should be this


UPDATE project j,
      (
      SELECT b.project_id, SUM(b.amount) AS samount
      FROM budget b, capital c
      WHERE b.capital_id = c.capital_id
      AND c.ctype = 'CapEx';
      GROUP BY b.project_id
      ) z
SET j.BCAP_AMT = z.samount
WHERE z.project_id = j.project_id;
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Pratima PharandeCommented:
gnoon , same query I have suggested before :)
0
myvpAuthor Commented:
Got it. Thanks  other than you had two ands together.
0
gnoonCommented:
sorry I miss refresh.
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
Query Syntax

From novice to tech pro — start learning today.