How to get sum of attributes from different tables MYSQL - mysql

I am new to MySQL, currently I've been trying to create a column in a table that contains the total of payment an individual has to pay. For references, the first picture is how I want the tables to look like (in the big picture).
What i want is that in the column of total_to_pay, it automatically sums up the value of price from Mobile_Subscription, value of price from TV_Subscription and value of payment_from_past from History. So I want it to basically look something like this:
I want to know if such thing is possible and if not how can i make something similar?
I searched on Google this problem but all I could find was how to sum all values from 2 different columns, when what I wanted is to sum 2 (or 3) specific values from 2 different column from 2 different tables.

Related

SSRS - Sum columns where condition exists

I have an SSRS report, where there are groups (in this case Mills 1-4), each one having a product milled and the tons produced (for example).
I need to add up the 4 groups (ton produced), wherein each group, the product = this.
Like if Mill#1 made product X and Mill#4 also made product X, then I need to add the "tons" produced fields for these two.
If all the Mills made the same product, then the sum would be for "tons produced" for all 4.
Example below:
I would want to add tons produced for the two rows in green where the product = M120
Example of data
"
To make it work the way you want, you'll need to use a Matrix with the columns as your product.
You'll need to add a matrix. Use the MILL for your row column grouping. Use the Product as the Column grouping. Add the TONS as the Data. You can right-click on the TONS and Insert Column -> Inside Group if you also want to add another column - say for the Hours.
Left click on the Tons and you can automatically add the Column Total and Row Total.
For more info, see MS Docs - Create a Matrix.

SUM function needs automatically to adjust for changes to SQL DB entries

I'm using multiple Calc tables in a single document for ingoing and outgoing bills that are stored in a SQL DB. Table A has ingoing bills and Table B has outgoing bills. For example table A has data from B2-B39 and table B has data from B2-B15. I need the sum function to automatically adjust when data entries are added or subtracted.
I can use =SUM(B2:B39) in tableA.B40 and =SUM(B2:B15) in tableB.B16 to get the sums of the current data, at the end of the data entries. However, when the DB changes, for example Table A has data from B2-B44, I end up missing five entries. To further complicate things, I have Table C, where I evaluate the net income. For example in Table C I use tableB.B16-tableA.B40. When the number of entries in tables A and B change the function in Table C no longer works.
I need SUM in multiple tables to automatically adjust its position based on how many rows of data are found in the DB.
Maybe there is something about your layout that you have not mentioned but a solution would seem to be to apply whole column references (eg =SUM(B:B)), or if you must have the total in the same column move it to the top and sum like so:
=SUM(B2:B1048576)
Otherwise try something like LibreOffice Basic and search for the first empty cell in the column to put the formula in.

SSRS column group displays data on unique rows

I have a data set where i'm using a table to display Name, Radio #, and Unit # information in SSRS tablix. As some of the groups have 60+ members, i thought it would be better to expand the tables into 4 columns repeating those detail fields instead of displaying a 3 page long skinny table. In the SQL i used a row count%4 function to assign a "position" number 0-3 for each name. If i create a table with the detail members above and then add a parent column group on position, i get the tables repeated as i want but each name/radio/unit appears on a unique row. I've tried several different ways of grouping rows/columns but always seem to get this staggered table (with only name/radio to make it easier to digest): sample_pic
Sorry if this is a duplicate. I've really searched quite a bit before putting this in but it's probably the case that if i knew what to search, i wouldn't be putting this question in. So if you'd rather tell me what to search i can do that too. :)
SSRS will display a row in the table for each row returned from the dataset, this is normal behaviour for data to display.
One way to get what you want is to create a query which has all the information form your column headings in one row, probably with a pivot or similar.
Or you could just display your columns in separate tables.

how to populate column from the content of other columns after a row is inserted in the same table in mysql

I have a table traffic with 7 columns, namely toll_id, date1, shift, car_single, car_return, car_local and car_total.
How could I populate first 5 columns manually, and then store a value in column car_total, which will be the sum of car_single and car_return?
Here is the image of my table:
Just to add a 3rd and 4th ways of achieving the desired outcome:
If you have at least MySQL v5.7.6, you can use a generated column as car_total.
Alternatively, you can choose not to store car_total at all, but calculate this value on the fly while querying the table.
Having a column to store the results of the calculation is good if you regularly have search based on that field because you can use indexes to speed up the searches. Calculating the results on the fly may be better, if you just need to display the result of the calculation, but there is no need to filter on it.
There are two ways to do this:
Add the logic in the application itself, so that it calculates total before inserting the record. (Recommended)
Write an after insert trigger (http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) which calculates the count when record is inserted.

MySQL: Use aggregate result in further calculation

I'm currently working on writing report generators. For one report I need to do a breakdown by a given characteristic (supplier, logging user, language, etc which for each row includes the name of the characteristic I'm interested in, the number of items that match that characterastic, and the percentage of total items this figure represents. The first two aren't a problem, the third is.
For example, to get a breakdown by language I'd be using a query like this.
SELECT lang_id,
COUNT(IF(open=TRUE,1,NULL)) AS lang_total
FROM table
GROUP BY lang_id;
This gives me the number of items per language.
I can get the total number of items in the table and store it in a variable simply enough with a plain count.
SELECT #totalOpen:=COUNT(*) FROM table WHERE open = TRUE;
Now I want to add a third column, which is the figure in lang_total divided by the value in #totalOpen multiplied by 100 (in other words, the percentage of all items that fit the criteria). Something along the lines of the following:
This is the bit I'm having trouble with, as because as far as I can tell you can't use aggregate columns in calculations.
SELECT lang_id,
COUNT(IF(open=true,1,NULL)) AS lang_total
(lang_total/#totalOpen)*100 as lang_percent
FROM table
GROUP BY lang_id;
I'm sure that there must be a way of doing this in MySQL, but I've not been able to track it down. Can anyone help out with this?
I read this question now for the first time. I know that probably it's too late to be useful for you but I would have solved in this way.
select lang_id,
sum(if(open= true,1,0)) as lang_total,
coalesce(sum(if(open= true,1,null)) / #r,0) as percentage
from table,(select #r:=count(*) from table where open = TRUE) as t
group by lang_id;