how to use the Pivoted Column values in Matrix use in another Tablix and write expressions on top of it - sql-server

I have one set of data with fields
StudentId, Name , Address in one dataset and being used in one Tablix.
also another set of data: StudentID Subject Marks in another Dataset and using Matrix to Pivot in the Report.
I am able to fetch the Report in this way
StudentID Name Address MAths Physcis Chemistry Median
1 Mike NJ 85 70 90 2
2 David CA 81 85 90 1
I was calculating Median by counting number of Subject Marks greater than 80.
Now how do I use the value of Median in Tablix instead of in Matrix.
Below should be the expected output format
StudentID Median Name Address MAths Physcis Chemistry
1 2 Mike NJ 85 70 90
2 3 David CA 81 85 90
Note: I am using Matrix to Pivot Subject Column in SSRS Report. I am using Pivot operation in SSRS instead of performing in SP because I get 40 columns after Pivoting in SP and need to physically map 40 columns. Here in example I have only given 3 columns(Maths, Physcis and Chemistry).
Also please do let me know if expected output format is at least possible.
Is there any way that I will be able to Pivot Subject Columns inside the Tablix itself instead of using the another Matrix??
Thank you.

There are two ways to typically go about an aggregation like this. If you stick with the two existing datasets, you'll have to use the Lookup or LookupSet functions to get data from the other dataset. For example, if your table/matrix is using the second dataset as it's source, you would Lookup the Name of each student. Keep in mind that this is not efficient for large reports.
The other approach, which I would recommend, is to join these two datasets in SQL and use that as the data source for the report. This is more efficient and makes the report simpler to maintain.
It's good that you are letting the report do the pivoting for you, it works much better that way.

Related

Excel index and match with multiple conditions with multiple results

I am trying to perform an index and match formula in Excel with a two large datasets which will return multiple unique results.
I have illustrated a simplified version of the data below. The two match conditions are A1 in table 2 = A:A in Table 1 and B2 in table 2 = B:B in table 1. This will result in multiple results and I want a formula I can drag from cells C3 across to D4 in table 2 to show the results of this index and match.
Table 1
First Name
Second Name
Food allergy code
Bob
Johnson
03
Bob
Johnson
04
Table 2
First Name
Second Name
Food allergy code 1
Food allergy code 2
Bob
Johnson
03
04
I have used the formula below which returns the first match, but when I drag this from cell C2 to D2 it returns the same value. I'm not sure how to rewrite this formula so that it provides each unique Food allergy code given both match conditions are met.
=TRANSPOSE(INDEX(Table1!C:C,MATCH(1,(Table1!A:A=A2)*(Table1!B:B=B2),0)))
Any help would be appreciated.
You could use the following, but it is computationally rather inefficient, so if you want to drag this formula over many cells, you should leave a comment to find a computationally more efficient solution.
=TRANSPOSE(FILTER(D:D,(B:B=G4)*(C:C=H4)))
which looks like this in an example:

EXCEL: Create Array Formula out of INDEX/MATCH with multiple results

my aim is to convert a massive excel sheet with different projects, employees and hours worked per month into an overview per employee. It should be possible to display the projects the employee is involved in and how many hours he worked per project per month.
The original sheet looks something like this:
I managed to find the projects Person A worked in by filtering through the INDEX/MATCH function. I applied the formula to the whole row where the employees are listed and receive multiple results of projects. My question is how to transform the formula into something more effective to copy all of the matched results (projects) into a column (see 1).
This is what I have so far, if matches the employee name in a certain area; the output is the first match of the project he is involved in:
=INDEX(B2:J3;1;MATCH("Person A";Sheet1!B3:E3;0))
How can I copy this to the bottom cells to copy all of the matched results? Does it help to create an array formula with this?
You can use he following formula in cell B9:
=IFERROR(INDEX($2:$2,SMALL(IF($3:$3=$B$8,COLUMN($3:$3)-COLUMN(INDEX($3:$3,1,1))+1),ROWS(A$1:A1))),"")
It indexes row 2 and looks for the column number of the first match in row 3 that equals the value in B8 (=Person A). When dragging down it will look for the second match ROWS(A$1:A1) will become ROWS(A$1:A2) = 2.
For Person B you can use this formula in cell B14:
=IFERROR(INDEX($2:$2,SMALL(IF($3:$3=$B$13,COLUMN($3:$3)-COLUMN(INDEX($3:$3,1,1))+1),ROWS(A$1:A1))),"")
I hope this is what you where looking for.
PS
if you paste the following formula in cell C9 you will get the sum result for Person A on Project XY in month 10 2019:
=IF(OR($B9="",C$8=""),"",SUMPRODUCT(($B$2:$K$2=$B9)*($B$3:$K$3=$B$8)*($A$4:$A$6=C$8),B4:K6))
Note: That is provided that the value in cell C8 equals the value in cell A4.

SSRS: Table of group aggregates with grand total

I am using SSRS 2014. My end goal is to get a table that looks like the following:
Project Status # of Projects Oldest Project Avg Project Age
Status 1 27 82 16
Status 2 9 29 6
Status 3 13 112 25
Status 4 33 68 16
Status 5 1 63 63
Status 6 1 27 27
Grand Total 84 112 17
A table like this, including the grand totals, can be done in Excel using pivot tables (this is how the data was originally retrieved...manually...)
If I have the same source data in a table where my columns include projectStatus and projectAge, I could generate a query to get all but the grand totals in SQL:
SELECT projectStatus, COUNT(projectStatus), MAX(projectAge), AVG(projectAge)
FROM projects
GROUP BY projectStatus
If I wanted to add grand totals to that, I could do a UNION with the following query:
SELECT 'Grand Total', COUNT(projectStatus), MAX(projectAge), AVG(projectAge)
FROM projects
Note that the grand totals are calculated off of the entire dataset and not just the grouped data - this mostly applies to the average, because it just happens to work out for the COUNT and MAX columns.
Now, if I am trying to generate this above table in SSRS, it is easy to generate the above table sans grand totals using the former query. However, now how do I add grand totals?
I could instead try a different approach where I use a more generic dataset query like:
SELECT projectStatus AS statusCategory, projectAge AS daysInCurrentStatus FROM projects
And then use aggregate functions in my table and have SSRS do the aggregation itself and provide grand totals on the raw data. I get a new problem now. What is the correct way to set my table up? I am calling grouping by [statusCategory] as "Group1", using scoped aggregate functions like so:
And it correctly generates the grand totals, but now I have a new problem: it is showing the average for each row instead of just one cell per group/scope. Is this something SSRS is meant to do, and if so, how can I do it?
...
You need to mess around a little to get right layout, but you basically need a group header or footer to show the Group aggregates.
if you right click on the lines next to [statusCategory] and select Insert Row > Outside Group - Above it will create a group header for you. Then just add your aggregate expressions in the correct columns like =Count(Fields!projectStatus.Value) then you can just set Row Visibilty on the details row to hide if you dont want to see it.
you should end up with a layout similar to this.
with the middle row (detail) visibility set to false
You're on the right track. You have two Row Groups right now. All you need to do is remove the inner one because it's not grouped by anything. In other words, it's repeating for every row of raw data.
So the Row Group named "Details" - either remove it or in the properties click "Add" and group it by 1. That will collapse those rows and do what you were expecting.

TSQL Count appearance Group by values in other table

I need to group some data to show in a graph but... it is too difficult for me :-(
In one table I have customers info, among that, Name, Kgs and yearly turnover
CustomerA 8 415.86
CustomerB 145846 6815.80
..............
CustomerZC 25160 25690.30
and I need to COUNT customers that has bought less than 50 Kgs, how many bought from 51 to 100, from 100 to 1.000, from 1000 to 30.000 and so on
but since groups limit are not similar, the boundaries of each range are stored in another table and looks like
Group0 0-50
Group1 51-100
Group2 101-1000
.....
Group15 1000001-5000000
Group16 5000001-9999999999
but I can modify it if it can helps
My Target is to have result like this:
0-50 14217
51-100 6425
101-1000 841
....
1000001-5000000 43
Now I achieve this result making 15 different queries but I would like to make an global algorithm that can adapt to a variable number of groups
Thanks
This one is similar, take a look at the second option that joins to a range table.
In your case, it would look something like this:
select r.boundary_name, coalesce(count(*), 0) as cnt
from ranges r
left join customers c
on c.kgs between r.low_range and r.high_range
group by r.boundary_name;
Naturally you'd need to tweak the join if you're looking for exclusive ranges vs. inclusive, and the ranges table will need a low and high bound column.

Crystal Reports - Select records via range of text

I have a report that is querying a transaction table, I have simplified the table below:
CREATE TABLE [dbo].[NL_Trans](
[NT_PRIMARY] [int] NOT NULL,
[NT_CODE] [varchar][20] NOT NULL,
[NT_CAT1] [varchar](6) NOT NULL,
[NT_CAT2] [varchar](6) NOT NULL,
)
Data in the table is like:
NT_PRIMARY -- NT_CODE --- NT_CAT1 --- NT_CAT2
---------------------------------------------
1 AB100 123 234
2 AB200 124 234
3 AB300 125 235
4 AB400 126 235
The table has several years data and has 8 NT_CAT fields, these are numbers stored as varchar's.
I have a large list of categories to limit the query to but I can't find a way to limit this to ranges of text.
So far I have tried:
{NL_Trans.NT_CAT1} IN "124" to "125"
AND
(CDbl ({NL_Trans.NT_CAT1}) IN 124 to 125))
Neither of which are returning any results.
I can't use a large list of OR statements as the ranges are more than 100 items in some cases meaning I'd need thousands of lines.
Can anyone give an alternative that will work (and leverage the Catagories index if possible) or tell me why either of the statements above are not working?
First of all, you cannot trust Crystal to evaluate a statement like "5" in "1" to "10". I don't know the basis for these comparisons, but I would guess that it is based on ascii values of the leading digits or something like that.
With that being said, converting to a double should work... Is it possible you have non-numeric characters in your NT_CAT fields? You should try something like this in your record selection formula and see if you have better luck:
if not(isnull({NL_TRANS.NT_CAT1})) and isnumeric({NL_TRANS.NT_CAT1}) then cdbl({NL_TRANS.NT_CAT1}) in 124 to 125
The downside to this is that Crystal won't add this to the WHERE-clause to be handled on the SQL Server, it will do the selection on the local machine instead. To get around that performance hit, you can create a SQL Expression in Crystal that will do the string-to-numeric conversion for you. I don't use TSQL so I'm not going to try and convert my Crystal code snippet to it, but if you created the SQL Expression {%ConvertCAT1ToNumeric} you could just use this in your selection formula:
not(isnull({%ConvertCAT1ToNumeric})) and {%ConvertCAT1ToNumeric} in 124 to 125
Good luck.
try this.
remove thoose lines ** that is the one of criteria type.. you might be looking for a range
**{NL_Trans.NT_CAT1} IN "124" to "125"
**AND
and only put this to the selection criteria but first off, try select a different field for a number range formula, copy the whole formula and create a new criteria. rename the field to the approriate one and run the report. (dont forget to delete the 1st dummy criteria you did)
original :
(CDbl ({NL_Trans.NT_CAT1}) IN 124 to 125))
i think this is the formula to range numbers
(CDbl ({NL_Trans.NT_CAT1}) between 124 to 125))

Resources