R
R
Red Rain2015-11-01 21:39:01
SQL
Red Rain, 2015-11-01 21:39:01

Why does SQL summarize with the previous group?

Here are the databases that are being sampled:

The database of ships that participated in the Second World War is considered. The relationships are:
Classes (class, type, country, numGuns, bore, displacement)
Ships (name, class, launched)
Battles (name, date)
Outcomes (ship, battle, result)

Here is the query: the query selects each country and performs mathematical operations on the caliber of that country's ship guns - bore , after which it sums the results for each group by country:
SELECT country, SUM((POW(bore, 3) / 2)) FROM Classes GROUP BY country;
And here is the result:
country mw
germany 1687.5
gt.britain 3375
japan 4288
usa 5468

The results are wrong because the query somehow sums the mw of the first group with the mw of each

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aleksey Ratnikov, 2015-11-01
@CodeDes3D

The request does exactly what you asked - counted with grouping by country. If you want to group more by type, then add it to the grouping list:

SELECT country, type, SUM((POW(bore, 3) / 2)) FROM Classes GROUP BY country, type;

N
nozzy, 2015-11-02
@nozzy

SUM((POW(bore, 3) / 2)) OVER (PARTITION BY country)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question