Skip to content
Advertisement

To retrieve data from SQL table fields with different IDs into corresponding different variables

The sql database table named ‘data’ has the fields ‘date’, ‘project_id’ and ‘amount’. I am able to retrieve the total amount spent on a single project with the code below:

But what I want to do is to retrieve the total amount of all projects and store them in different variables like:

How can I code for that? Find arrays and loops a bit confusing. Thanks.

Advertisement

Answer

If you know what the projects are, you can use conditional aggregation:

You could use GROUP BY, but then you would need to cycle through the result set to assign the appropriate values in your application code.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement