How to create a query that is able to sort data into 2 groups? - ms-access

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.

Related

MySQL Nested Select Querys using data from main query

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. :)

Report builder 3.0 Using Reportitems!TexboxXX.Value sometimes creates multiple boxes. Why?

I have 6 Datasets each one is the same query but has a different WHERE clause based on employee type. They generate 6 tables. At the top of the report there is a summary table which uses reportitems!textboxXX.value to grab the totals for 2 particular fields from all the tables. I'm also using a StartDate and EndDate parameter. Each reportitems! expression in the table comes from a different dataset in the report if that is relevant.
When I run the report using dates from yesterday back to the 9th of May I get the desired output.
But when I go to the 8th I get multiple rows all the same.
and as I go even further back I get even more rows of the same information. To my knowledge nothing interesting or different happened on the 8th of May and the tables further down the report look exactly the same. Any idea what might be causing this? The design view looks like this if that helps. There's no grouping or filters on the table.
Still not certain about the mechanics behind this but I found a 'solution' so I figured I'd post it. I just made a new dataset for my summary tables where the query was simply SELECT 1. Now I get a single row every time.

Access 2013 Web App - How to select the first record in a table using expression builder?

I am using Access 2013 Web apps to build an application. I have one table containing a list of organisations, and another table containing comments about the organisations. One organisation can have many comments.
I am trying to write a query that will select the organisation and the first comment about that organisation from the comments table. However, this is proving quite difficult with the limited Web App functions (that is, I do not know how to select only the first comment - it is selecting all of the comments associated with the organisation). I have looked high and low how to achieve this. Any help would be much appreciated. Thanks.
The way you can do this is build a query that joins in all the child records but in a SORTED order. Say like this:
Note how in above the sort order is by DESCENDING comment date. In your case you could sort by DESCENDING PK [ID] of the CHILD table. So you can sort by any column, and just sort descending on the column you want the most recent row. As noted, if you don't have a comments date column, then just sort by PK [id] of the CHILD table.
Once you have the above, then you can write a data macro that returns the FIRST row, which will thus be the most recent based on the last row.
So the data macro can look like this:
And in a form, your macro code to “fetch” the most recent comments and stuff results into a un-bound text box can look like this:

SQL Server Report Builder - Show Count of Sub Groups

I have a SQL Server Reporting Services report that shows customer order data, but it's grouped as follows:
Store
Customer
Customer Order Items
So, each report is a grouping of stores, with a subgroup of customers per store, and then the items per customer. I'm trying to show aggregate sale and other information at each header record of the appropriate group in the report. Most of this is working well, but for each store header record, I want to show a count of the customers. I'm trying to use some variation and\or combination of RowCount, CountDistinct and other aggregate functions, but to no avail.
Can anyone help me determine how I essentially can get a "count" of customer groups to show at the Store level header? TIA!
CountDistinct on Customer should work fine - no need to specify scope if it's in the Store group header row.
I put a simple test together.
Data:
Report in designer:
Most important thing to note is the CountDistinct on Customer in the Store header row; this is just the expression used:
=CountDistinct(Fields!customer.Value)
End result, showing correct values:
Please let me know if I'm missing something.
Edit after comment:
Apologies in advance for how long this is getting.
The previous report did have row groups for Store and Customer, but I've modified this to make it more clear, hopefully. Still based on the same DataSet:
You can see there are three row groups, and each row in the report is actually a group header row belonging to one of those groups.
In the Store group header row I've kept that same CountDistinct expression. I've also added a CountRows() expression to show how many actual rows are available in each of the different groups.
Here you can see for Store1, CountRows is returning 4, i.e. there are four rows that we are aggregating in this scope, which is what we expect looking at the DataSet.
Similarly, when we apply =CountDistinct(Fields!customer.Value) in the Store scope we are considering these same 4 rows, and we see two distinct customers for Store1, which seems correct to me.
For Store2 we are considering 6 rows in total, which have three distinct customers. Again, just by applying =CountDistinct(Fields!customer.Value) we get correct value.
Hopefully this rejigged report helps clear things up. If I'm still not getting your requirements, can you please explain what numbers are wrong in my sample report based on my sample DataSet? That way I can adjust things easily on my side.

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;