Mysql: pivoting calculated data on itself - mysql

So I have this fairly large set of data that I'm reporting out on a predefined set of segments. I calculated the segments creating a series of sum case when statements. How do I present this data pivoted on itself? I'm trying to create a table that looks like this:
I wrote the code that generates the sums across the top based on how each color is defined. I hadn't had trouble presenting other data in column 1 because they were simple case when statements from a single field. Ultimately I want to present it as percents like the picture, but for now I'm trying to understand how to accomplish this. I can't do a select red, orange, etc etc because those fields are only calculated, not actual. How would I achieve something like this?

Related

How to get sum of attributes from different tables 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.

How can I create a table that uses an equation to average data from another table?

I have a table that contains data from repeated experiments (for example, site A has one sample, and the lab processed the sample three times obtaining slightly different values). I need to average these results in a separate table, but what I have read on the Microsoft support site is that a query that pulls data into another table with a calculated field is not possible on Access.
Can I query multiple data points from one table into a single calculated field in another table? Thank you.
UPDATE
I ended up doing a lot of manual adjustments of the file format to create a calculated field in the existing table that averages each sites data, so my problem is, for my current purposes, solved. However I would still like to understand. Following up with you both, I think the problem was that I had repeated non-unique IDs between rows when I probably should have made data columns with unique variable names so that I could query each variable name for an average.
So, instead of putting each site separately on the y axis, I formatted it by putting the sample number for each site on the x-axis:
I was able to at least create a calculated field using this second format in order to create an average value for each site.
Would have there been a way to write a query using the first method? Luckily, my data set was not at all very hefty, so I could handle a reformat manually, but if the case were with thousands of data entries, I couldn't have done that.
Also, here is the link to the site I mentioned originally https://support.office.com/en-ie/article/add-a-calculated-field-to-a-table-14a60733-2580-48c2-b402-6de54fafbde3.
Thanks all.

subtract two textboxes in ssrs from diff tables

I would like to build a report like this
In this report 2015 and 2016 columns are from different tables and I need the difference of both of them as result in another table the data set for 2015 and 2016 are also different. The data is pulled from cube. I tried this expression to get the result
ReportItems!textbox10.Value-ReportItems!textbox5.Value here textbox 10(2016 column) and textbox5(2015 column) but when I do this calculation I'm getting this result set
Technically you can reference a textbox with an expression like this:
=ReportItems!TextBoxList1.Value
However, I would strongly caution against this as it usually goes against best practices. Textboxes in tables are meant to be dynamic based on the grouping you define. It would be best to refer to the actual values from the dataset using the Lookup function. See here for more info on that.
Edit based on feedback:
There are two ways you can go about this.
Remove the filters from the query and apply the filters on the tables. This way, you can use table grouping to separate out the numbers, but still compare them to the total. The difference expression would look something like this:
=Sum(Fields!Amt.Value) - Sum(Fields!Amt.Value, "RowGroupName")
With your existing structure, use a lookup like this:
=Fields!Amt2.Value - Lookup(Fields!ID1.Value, Fields!ID2.Value, Fields!Amt1.Value, "Dataset1Name")

Need help to create a SSRS RDL report in specified format

I have to develop a RDL report in the following format:
I have stored procedure returning first block result set i.e. with Sr.No. but don't know how to return result for second block i.e. for <----Current----> <---Last---> block because here I have to show values next to each Label.
Do we need to create multiple DataSet for this task OR we can achieve this in a single stored procedure?
Anybody suggest me how can we achieve this.
One approach in this case would be to add the Label information to the underlying stored procedure, i.e. the same information repeadted for each Code, then only display this information once for each Code in group footer rows.
This assumes that you can't just calculate the Label values for each Code from the rest of the DataSet.
So, making some guesses about your data and assuming your updated DataSet looks like this:
You can create a report similar to this:
Note that the Label information is displayed only once for each Code since the information is in the group footer rows. Just specify the Label fields without any aggregation; this will just take the first row's values.
Results look to match your requirements:
You could approach this other ways, e.g. using the Lookup function or with Subreports, but this approach only required one table and one DataSet so seems simplest to me.

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;