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;
Related
I'm having a problem in SAP BO-s that I'm trying to solve for a while now so I'm hoping to find a help here...
So, I have a list of orders and a number of items on each...
... and a final result should be numbers grouping of numbers of items shown as dimension with number of orders shown as measure...
I've tried with various combinations of foreach and in context operators but none of them seems to work. Does anyone have an idea how to do it? Thanks a lot
The simpliest thing would be to use an object containing the number of items for each order as a dimension rather than a measure. Then you could create groups around the number of items. I am assume you cannot do that.
So start off by creating a variable to convert your Number Of Items Dimension to a string (and dimension) like this...
=FormatNumber([Items_no]; "#")
Next create a variable to count your orders...
=Count([Order_no])
Now adding those two variables to a table will give you the following...
In order to lump anything 6 or greater together you could create a variable with a series of nested if statements. In this situation it is much easier to create a group. To do so, right-click on the Number of Items Dimension column in the table just created, choose "Group > Manage Groups...", and create a group that looks something like this...
Sum the Number of Orders column and there you have it...
I have edited the question to make the problem more simple and added the suggested changes in the coments, Thx a lot
I have a table matches with data and I need:
1- To get all the columns for matches before a date.
http://sqlfiddle.com/#!9/4ff02f/18
SELECT m1.* FROM matches AS m1 WHERE m1.date < '2019-02-23 00:00:00'
2- I need to add two columns with the “streak” for home and away teams in each match (or row). The “streak” for a team is the count of matches before the date of the current match in same competition, until the team lost a game (being home or away indistintly). The result would be kind of this:
results desired
I need to get all the info in only one query in MySql…and I am getting crazy with JOINS, SUBQUERIES,….They don´t get to do what I mean. Really appreciate some help
Thanks!
It sounds like you want to only populate specific fields for your streaks and that you want cumulative data. You may need to use fields that don't show (invisible, or just at the end) for "win/lose" and cumulative data to provide the count.
I don't have time to write a full solution right now, but this will be too long for a "comment" so bear with me:
Add columns for "Win" in home and away. Place a 1 or 0 accordingly.
Add columns for streak and conditionally include a cumulative count of Win IF the Win is 1. But if the win is Zero, set the cumulative back to Zero.
Example of CASE: https://www.w3schools.com/mysql/func_mysql_case.asp
Example of Cumulative: https://popsql.com/learn-sql/mysql/how-to-calculate-cumulative-sum-running-total-in-mysql
Once you have it written, you could likely combine these into a single function/field. But ... baby steps. Like the rest of us. :)
I have a report that in which I have some grouping, I want to limit the maximum amount of rows per group to be 5.
For example: In a group of purchased items I want to show the last five purchased per category.
I've tried with Ceiling(RowNumber("GroupName")/5) as when doing page break after Nth row but I'm not quite getting the results I want. I tried in the filter group property setting the Top N but this doesn't work at all.
Is there a way at all to force RowNumber() function to equal a fix value?
I'm finding this harder than I thought it should be. I cannot modify the dataset, so I can't do this by query.
Any workaround or idea will be much appreciated
You may not be able to filter a tablix using the RowNumber function, but you can hide a row if its row number is (for example) greater than 5.
Use the *Row Visibility" properties to specify an expression like this:
=(RowNumber(Nothing) > 5)
To restart counting for every group, use the grouping name as the scope (instead of Nothing).
so I'm very new to Access - just started learning it this week. I have data comprised of a bunch of policy numbers with corresponding ratings and premium values.
What I'm trying to do is create a query table that aggregates this data by rating (=1 or >1), this is the part I can't figure out. In design mode, I have put criteria =1 or >1, but it's not doing anything; when I switch to datasheet view, it just lists all of the ratings instead of two boxes that say =1 and >1. If anyone could give me insight on how to do this, I'd appreciate it!
From what I understand after reading your question, you want a query that shows two columns, one that counts the records with a rating of 1 and one that counts the records with a rating of greater than 1.
The best way to do this is to set up two expression columns, both wrapped in a count as you can see below:
SELECT Count(IIf([Rating]=1,[Rating],Null)) AS [=1], Count(IIf([Rating]>1,[Rating],Null)) AS [>1]
FROM tblSortRating;
If you have any questions or I've misunderstood your question leave a comment and I'll get back to you.
I have an rdlc report file, and I am trying to make a sum which can only include the last item in each group. I have a table kind of like this:
Place = ? (Group header 1)
User = ? (Group header 2)
Date =Last(Fields!Number.Value) (Group header 3)
Number =Fields!Number.Value (Detail row)
So, in other words, in User there, I want a sum of Date... if that made sense...
The Numberrows contain many numbers per Date. But Date shows only the last number for that day, because the rest doesn't count (but must be displayed) In User I want to sum up those last numbers for all the dates for that user. And same with Place (which would be the sum of every last number for every day for every user).
Could anyone help me with this? I tried the obvious (to me at least) =Sum(Last(Fields!Number.Value)), but (also tried to specify the group in those functions, but didn't make a difference because) I get an error when I try to compile which says:
The Value expression for the textbox 'numberTextbox' contains an aggregate function (or RunningValue or RowNumber functions) in the argument to another aggregate function (or RunningValue). Aggregate functions cannot be nested inside other aggregate functions.
Which I guess kind of makes sense... but how do I do this then?
Update: I have solved the issue by adding another column, and copying those last numbers into that column. This way I can display all the numbers, and do the summing on the column that only contains the ones that is going to be in the sum. I am still very curious to if anyone have a solution to my original problem though... so please post an answer if you do!
Not sure I understand exactly what you're trying to do. Maybe something like =Last(Fields!Number.Value,"Group 1") + Last(Fields!Number.Value,"Group 2") + Last(Fields!Number.Value,"Group 3"), instead using a sum function?
the easiest way to do this would be to modify your dataset to only include the records your are displaying in the date field, that way you could just use a simple sum() instead of trying to do something weird and screwy and might not work.
Modifying the data set may really be the simplest solution, but if you really wanted to do this without complicating the query you could try "custom aggregation".
The exact techniques for this depend on the version of SSRS, and my understanding is it didn't really work that well before SSRS 2008.
The idea is, you write some custom code to maintain an array containing the last value for each date. One function updates the "last value" for a date, and another sums the values in the array. Your header calls the latter function; you conspire to cause the former function to be called once for each detail row before the header is processed.
Here's a blog with a write-up that explains the technique in SSRS 2008.
It also gives some insight into how you can try to make this work in SRSS 2005, but again apparently that's not as reliable.