I want to count a range of cells if they contain multiple criteria then return the amount of "yes" to the corresponding criteria - countif

So I have a Column which has multiple texts types in (B1, B2, B3) this corresponds to the group customers are allocated.
Additionally there is another 3 columns which contain yes or blanc cells which tells me if they have received coaching.
I want to filter customers by group and then count how many of them have had coaching.
Im using the following sum to count the group by =SUM(COUNTIFS(E20:E998,{"B1","B2","B3"})) this works and counts the amount of customers i have which are from the B1, B2 or B3 group. But I also want it to count which of these groups have attended coaching which is representative of the text yes in a range of 4 columns.
At the moment I am filtering my tracker to set groups and then using COUNTA acorss the range of 4 columns to get the sum but is there a way to combine COUNTSIFS and COUNTAs so that i can filter customer by group and then count those that have attended coaching ?

Related

Calculate Min and Max for aggregation in SSRS report

My SSRS report have a dataset that will return the following result. The result set for some reason may not be changed. Note that Category - SubCategory pair might not be distinct.
Category Sub-Category Value
-----------------------------
A A1 100
A A2 120
A A2 60
B B1 80
B B2 90
B B2 70
I want to show the max value and main value for each of the SUM(category, subCategory) in report matrix, as exactly the format as follows (except the comment in bracket):
Max | 180 (two A-A2 rows)
Min | 80 (B-B1)
How can I define the matrix and write the expression? If make a group on these two columns, The matrix will show four rows regardless of what expression I set.
I tried to run your use case on my local SSRS.
On Left hand side I have original Data and on Right hand side I have the desired result you expect.
What you need is grouping as below
Expression for sum as below
Put a tablix into your report. Then at Row Groups (bottom) click on the (Details) and chose Add Group > Parent Group. Click the Add group header and chose your Sub-Category. Do the same with your Category. Your Row Grouping hierarchy should be now Category > Sub-Category > Details.
Now you see the brackets on the left in your tablix, they indicate the level. If you use now the following expression with their group name on the specific level, you will get what you want.
'At the Category group level header
=Sum(Fields!Value, "CategoryGroupName")
'At the Sub-Category group level header
=Sum(Fields!Value, "SubCategoryGroupName")
I got the way to make it. The solution is as follows:
Make a row parent group called row. Let the group group by a constant.
Make a child group category under the row group which is grouped by Category.
In matrix cell which is inside the group, Add this expression: =Min(Sum(Fields!Value.value), "Category"), "row"). that's the reason why I make a constant group, because I want to make the nested aggregate function legal.
This expression will return all values identical within the Category group. Now add another row outside of these row groups. Pick a cell and enter =ReportItems!ThatTextBox.Value.
Hide the row which consists your groups.
Do the same for MAX value (Start from adding an adjacent group, grouping by constant)

Group on page count

I have a report which is grouped on an ID field. Each grouping is roughly 1-3 pages depending on how much data Is pulled from the source.
Is there a way to group the report based on the page count of each section?
Example:
All Groups that are 3 pages
All Groups that are 2 pages
All Groups that are 1 page
AFAIK you can not access the number of pages since the grouping is done before the render of the page.
But, assuming you have a table T with a column ID being of a set of values v, which you group by, why not do a count of how many rows there are per value v and join that to you report source (or just use the HAVING clause)? You can maybe derive the number of pages by the number of members of each ID.
Like
ID value count
1 a 2
2 a 2
3 b 1
...

Count number of row results for a row group

How do I group/filter rows and then get total rows for each column. I am going to diagram what the result should be. I don't want to show the actual data. Just the count per column
Out Put should look like this
Column A Column B Column C
Row A - 235 records 300 records 15 records
Row B - 1 record 80 records 900 records
Each column represent a count on the same field but filtered.
So ..
Column A is really Count(MyColumn) WHERE = A
Column B is really Count(MyColumn) WHERE = B
To summarize each row is a grouping + filter and each column is a count based on the number of rows contained in that grouping. No row data needs to be displayed.
You can do this in a table in the group by using the following formula:
=SUM(IIF(Fields!MyColumn.Value = "A", 1, 0))
However, this type of summary report is what a matrix is designed to do. Use the Row field as the row group, the column field as the column group and a Count expression in the intersection and it will do it all for you.

how to get average of rows that have a certain relationship

I have a bunch of data that is stored pertaining to county demographics in a database. I need to be able to access the average of data within in the state of a certain county.
For example, I need to be able to get the average of all counties who's state_id matches the state_id of the county with a county_id of 1. Essentially, if a county was in Virginia, I would need the average of all of the counties in Virginia. I'm having trouble setting up this query, and I was hoping that you guys could give me some help. Here's what I have written, but it only returns one row from the database because of it linking the county_id of the two tables together.
SELECT AVG(demographic_data.percent_white) as avg_percent_white
FROM demographic_data,counties, states
WHERE counties.county_id = demographic_data.county_id AND counties.state_id = states.state_id
Here's my basic database layout:
counties
------------------------
county_id | county_name
states
---------------------
state_id | state_name
demographic_data
-----------------------------------------
percent_white | percent_black | county_id
Your query is returning one row, because there's an aggregate and no GROUP BY. If you want an average of all counties within a state, we'd expect only one row.
To get a "statewide" average, of all counties within a state, here's one way to do it:
SELECT AVG(d.percent_white) AS avg_percent_white
FROM demographic_data d
JOIN counties a
ON a.county_id = d.county_id
JOIN counties o
ON o.state_id = a.state_id
WHERE o.county_id = 42
Note that there's no need to join to the state table. You just need all counties that have a matching state_id. The query above is using two references to the counties table. The reference aliased as "a" is for all the counties within a state, the reference aliased as "o" is to get the state_id for a particular county.
If you already had the state_id, you wouldn't need a second reference:
SELECT AVG(d.percent_white) AS avg_percent_white
FROM demographic_data d
JOIN counties a
ON a.county_id = d.county_id
WHERE a.state_id = 11
FOLLOWUP
Q What if I wanted to bring in another table.. Let's call it demographic_data_2 that was also linked via the county_id
A I made the assumption that the demographic_data table had one row per county_id. If the same holds true for the second table, then a simple JOIN operation.
JOIN demographic_data_2 c
ON c.county_id = d.county_id
With that table joined in, you could add an appropriate aggregate expression in the SELECT list (e.g. SUM, MIN, MAX, AVG).
The trouble spots are typically "missing" and "duplicate" data... when there isn't a row for every county_id in that second table, or there's more than one row for a particular county_id, that leads to rows not included in the aggregate, or getting double counted in the aggregate.
We note that the aggregate returned in the original query is an "average of averages". It's an average of the values for each county.
Consider:
bucket count_red count_blue count_total percent_red
------ --------- ---------- ----------- -----------
1 480 4 1000 48
2 60 1 200 30
Note that there's a difference between an "average of averages", and calculating an average using totals.
SELECT AVG(percent_red) AS avg_percent_red
, SUM(count_red)/SUM(count_total) AS tot_percent_red
avg_percent_red tot_percent_red
--------------- ---------------
39 45
Both values are valid, we just don't want to misinterpret or misrepresent either the value.

N or more continuous year range

I have to create a report using MySql DB where more than 4 tables are involved. I have one table (S1) with S1_ID and S1_Year_Range (strings like 2001-2002) and another table (S2) with S2_ID(PK), S2_Customer_ID, S1_ID (FK) and other fields for other conditions that can appear in Where clause of my query. There can be more than one row in S2 with the same S2_Customer_ID but different S1_ID. My query is to create a report using VB.net and ask users to enter two values; one number for how many continuous years or bigger (like >= 5 years), and a year range value (like 2011-2012) which is the highest value in the list for all customers.
My report lists customer names (by joining the above query with another table), customer rank and all year range values (highest at the bottom) for that customer in one column for each customer. Any help for this query would be appreciated.
Data and results could be like the following:
S1:
(S1_ID....S1_Year_Range)
(1......2000-2001)
(2......2001-2002)
(3......2002-2003)
(4......2003-2004)
(5......2004-2005)
etc
S2:
(S2_ID.....S2_Customer_ID.....S1_ID)
(1....1....1)
(2....1....2)
(3....1....3)
(4....2....2)
(5....2....3)
(6....2....5)
(7....3....2)
(8....3....3)
(9....3....4)
(10...3....5)
(11...4....3)
(12...4....4)
(13...4....5)
etc
when number 2 and year range (2003-2004) is entered by the user, the result should be the following:
customer 3 with 3 year range values (2003-2004, 2002-2003, and 2001-2002) and customer 4 with 2 year range values (2003-2004 and 2002-2003):
cname3
2001-2002
2002-2003
2003-2004
cname4
2002-2003
2003-2004
I hope you can see the columns of the report correctly.
I finally created a complex query to solve my problem. In the following query, I encoded the user year range value as '2010-2011' and number of continuous years as 14. Also a tiny difference with the question is the table names; table CSP here is the same as table S2 in my question but field names are the same as those in my question.
SELECT CSYWFY.S2_Customer_ID, COUNT(CSYWFY.S2_Customer_ID)
FROM (SELECT S1F.S1_Year_Range, S2.S2_Customer_ID , COUNT(S1F.S1_Year_Range) FROM CSP as S2 INNER JOIN S1 as S1F ON S2.S1_ID = S1F.S1_ID WHERE '2010-2011' IN (SELECT S1N.S1_Year_Range FROM CSP as S2N INNER JOIN S1 as S1N ON S2N.S1_ID = S1N.S1_ID WHERE S2N.S2_Customer_ID = S2.S2_Customer_ID ) GROUP BY S2.S2_Customer_ID ASC , S1F.S1_Year_Range DESC ) CSYWFY
GROUP BY CSYWFY.S2_Customer_ID
HAVING COUNT(CSYWFY.S2_Customer_ID) > 14
HTH