I need some help with the design of a query.
I’ve got one table with alot of rows(it needs to be in one table).
Table name: 201201
columns = code, batch, value, volume
rows = 301, 2920, 100, 2000
301, 3192, 40, 800
302, 6479, 230, 3100
303, 4930, 20, 50
303, 3920, 60, 95
How could I pull the results so it’s presented in this way:
code, batches, value, volume
301, 2, 140, 2800
302, 1, 230, 3100
303, 2, 80, 145
I have done this when I use multiplie tables but can’t get it to work when all rows are in the same table.
I manage to get the batch count and mrdr with this query
SELECT DISTINCT(`201201`.code), count(DISTINCT(z.batch)) as batches
FROM `201201`
LEFT JOIN `201201` AS z ON `201201`.code = z.code
GROUP BY `201201`.code
But then I’m stuck.
Thanks in advance.
New issue!
Okay, so in the same table I have a risk column that contains a float number for each batch:
Table name: 201201
columns = code, batch, value, volume, risk
rows = 301, 2920, 100, 2000, 0.3
301, 3192, 40, 800, 0.2
302, 6479, 230, 3100, 0.8
303, 4930, 20, 50, 0.8
303, 3920, 60, 95, 1
What I would like is to multiply the risk with the value for each row and then sum it per code, like this.
code, batches, value, volume, risk
301, 2, 140, 2800, 38
Where 38 comes from, 0.3*100 + 0.2*40.
The same for code 302 and 303.
Why do you need to join when you can do this without join? Hope this helps:
if the above does not work, try Self Join: