How to roundoff the decimal places in SSRS - reporting-services

I have an expression in my report that computes the general average of a student.
=(((SUM(Round(Fields!FINAL_FS_GRADE.Value,0))+ SUM(Round(Fields!FINAL_SS_GRADE.Value,0)))) / CountRows("GRADES")) / 2
Below is sample of student Grades for Quarter 1 and Quarter 2 and final Grade
Q1 Q2 FINAL
83 83 83
84 83 83.5
79 80 79.5
85 88 86.5
88 88 88
84 77 80.5
83 90 86.5
82 76 79
If we are going to compute the general average of Final Grade, this will be 83.3125
But my requirements is to round off the final grades.
Q1 Q2 FINAL ROUND OFF FINAL
83 83 83 83
84 83 83.5 84
79 80 79.5 80
85 88 86.5 87
88 88 88 88
84 77 80.5 81
83 90 86.5 87
82 76 79 79
So the general average will become 84.375 or 84. but my output is still 83.3
whats wrong with my expression syntax?
Thanky you.

Instead of trying to do all the work on the value, why not just leave the values as they are (no rounding) and set the format property of the field to something like f0. This will display 0 decimal places and round the result in the process.

Related

Add values in a lists which is a string column in hive

I have a set of data where a columns consists of lists which is of string data type.
Column_A|Column_B
AAA |1 23 56 89 74 52
BBB |63 99 44 2 80 87 58 63
CCC |96 45 23 84 62 74
Here, In the above data I need to add the values in column B as below:
Column_A|Column_B |Column_C
AAA |1 23 56 89 74 52 |295
BBB |63 99 44 2 80 87 58 63|496
CCC |96 45 23 84 62 74 |384
I have used cast function and converted the data type from string to int using the below query.
select Column_A,cast (Column_B as INT) as Column_B from Xyz
But summing the values is a great challenge.
Can someone help me out?
I'm learning RegEx too.. Is there any possibility to use RegEx?
Explode your column using split by space and aggregate.
This is demo in Hive:
with your_data as
(
select Column_A,Column_B from
(
select stack(3,
'AAA','1 23 56 89 74 52',
'BBB','63 99 44 2 80 87 58 63',
'CCC','96 45 23 84 62 74'
) as (Column_A,Column_B)
)s
) --Use your table instead of this CTE
select Column_A,Column_B, sum(cast(b.val_b as int)) as Column_C
from your_data a
lateral view outer explode(split(Column_B,' ')) b as val_b
group by Column_A,Column_B;
Result:
OK
AAA 1 23 56 89 74 52 295
BBB 63 99 44 2 80 87 58 63 496
CCC 96 45 23 84 62 74 384
Time taken: 53.228 seconds, Fetched: 3 row(s)
Alternatively, if the maximum number of elements in the list is fixed, you can do the same without explode, it will work much faster:
create temporary macro cast_value(s string) nvl(cast(s as int),0);
with your_data as
(
select Column_A,Column_B from
(
select stack(3,
'AAA','1 23 56 89 74 52',
'BBB','63 99 44 2 80 87 58 63',
'CCC','96 45 23 84 62 74'
) as (Column_A,Column_B)
)s
) --Use your table instead of this CTE
select Column_A,Column_B,
cast_value(col_B_array[0])+
cast_value(col_B_array[1])+
cast_value(col_B_array[2])+
cast_value(col_B_array[3])+
cast_value(col_B_array[4])+
cast_value(col_B_array[5])+
cast_value(col_B_array[6])+
cast_value(col_B_array[7])+
cast_value(col_B_array[8])+
cast_value(col_B_array[9]) as Column_C
from(
select Column_A,Column_B, split(Column_B,' ') col_B_array
from your_data a
)s
Result:
OK
AAA 1 23 56 89 74 52 295
BBB 63 99 44 2 80 87 58 63 496
CCC 96 45 23 84 62 74 384
Time taken: 0.82 seconds, Fetched: 3 row(s)

Ranking Based On Aggregate Sum Derived Table Without #variables

This is been a headache challenge for me in my project, being a php and or say sql novice.
I must first of all indicate that i'm running this code in php with binded params, since I am using pdo.
Now, as the title indicates, how can I rank students in a class, after having calculated their raw scores from Aggregate Sum Derived Table (From Clause), without #variables in sql. Something like, let assume my data pulled from the database is:
studref Name English Maths Gov
Bd1 Ida 66 78 49
Bd2 Iyan 58 80 69
Bd3 Ivan 44 56 80
Bd4 Iven 63 92 68
Bd5 Ike 69 77 59
So using Aggregate Sum in derived table, I then add another column, which is summation of the marks from the various subjects like:
SUM(THE SCORES) AS accum_raw_scores
studref Name English Maths Gov accum_raw_scores
Bd1 Ida 66 78 49 193
Bd2 Iyan 58 80 69 207
Bd3 Ivan 44 56 80 180
Bd4 Iven 63 92 68 223
Bd5 Ike 69 77 59 205
So, what I want to do next is to add another column, which will represent the rank of each student, based on his/her total score from the subjects. Hence, this is where I want the code below to handle that for me:
1+(SELECT COUNT(*)
FROM (SELECT s.studref, SUM(s.subjectscore) AS total_score
FROM studentsreports s
GROUP BY s.studref) AS sub
WHERE sub.total_score > main.accum_raw_scores) AS overall_position,
So that in the end, I will have something like:
studref Name English Maths Gov accum_raw_scores rank
Bd1 Ida 66 78 49 193 4
Bd2 Iyan 58 80 69 207 2
Bd3 Ivan 44 56 80 180 5
Bd4 Iven 63 92 68 223 1
Bd5 Ike 69 77 59 205 3
Unfortunately, I have tried several approaches but to no success. Please help!
Meanwhile, I want to try as much as possible to do without variables.

MySql sort query with multiple fields

I have two tables "activity_stats" & "activity_stats_values"
activity_stats table
# id, activity_id, kyf_id, kyf_sort
618 84 5 1
619 84 6 2
638 84 4 3
activity_stats_values table
# id, activity_id, player_id, kyf_id, value
2563 84 46 5 45
2564 84 46 6 60
2587 84 47 5 10
2588 84 47 6 25
2589 84 49 5 10
2590 84 49 6 40
2591 84 48 5 30
2592 84 48 6 15
2594 84 46 4 NULL
2595 84 47 4 80
2596 84 48 4 NULL
2597 84 49 4 NULL
Requirement
players should be sorted by values in descending order. Meaning the player with highest value of first keyfigure(kyf_id , kyf_id position based on field "kyf_sort" of table "activity_stats") should be on top (then second key figure, then third key figure).
Expected output
# id, activity_id, player_id, kyf_id, value kyf_sort
2563 84 46 5 45 1
2564 84 46 6 60 2
2594 84 46 4 NULL 3
2591 84 48 5 30 1
2592 84 48 6 15 2
2596 84 48 4 NULL 3
2589 84 49 5 10 1
2590 84 49 6 40 2
2597 84 49 4 NULL 3
2587 84 47 5 10 1
2588 84 47 6 25 2
2595 84 47 4 80 3
OR playes ids in order [46,48,49,47]
I tried the following query
SELECT ac_st_v.activity_id,ac_st_v.player_id,ac_st_v.value,ac_st.kyf_id,ac_st.kyf_sort,pl.first_name ,
(select max(value)
from activity_stats_values
where activity_id=ac_st_v.activity_id
and kyf_id=ac_st.kyf_id
group by activity_id) as m_value
FROM teamplayer.activity_stats_values as ac_st_v
JOIN teamplayer.activity_stats as ac_st
ON ac_st_v.activity_id=ac_st.activity_id
AND ac_st_v.kyf_id=ac_st.kyf_id
JOIN teamplayer.players as pl
ON ac_st_v.player_id=pl.id
where ac_st_v.activity_id= 84
order by ac_st_v.player_id,ac_st.kyf_sort,m_value
Is there any way to sort the values like this?

Percentage by Row Group

I have a matrix with rows grouped by Dept (Department). I am trying to get the actual hours / required hours percentage in a column for each row group, but I can only get the total %, not the % by group. Ex:
I should get this:
Total Employee Req Hrs Rep Hrs % Billable hrs % NonBill Hrs % Time Off %
Dept A Total 672 680 101 575 85 140 21 8 1
Emp1 168 170 101 150 89 50 29 0 0
Emp2 168 165 98 120 71 20 12 8 4
Emp3 168 175 104 155 92 20 12 0 0
Emp4 168 170 101 150 89 50 29 0 0
Dept B Total 420 428 102 365 87 80 19 4 .1
Emp5 168 170 101 150 89 50 29 0 0
Emp6 84 84 98 60 71 10 12 4 4
Emp7 168 175 104 155 92 20 12 0 0
G Total 1092 1108 101 940 86 190 17 12 1
But I get this:
Total Employee Req Hrs Rep Hrs % Billable hrs % NonBill Hrs % Time Off %
Dept A Total 1684 1675 101 1250 86 225 17 12 1
Emp1 168 170 101 150 89 50 29 0 0
Emp2 168 165 98 120 71 20 12 8 4
Emp3 168 175 104 155 92 20 12 0 0
Emp4 168 170 101 150 89 50 29 0 0
Dept B Total 1092 1108 101 1250 86 225 17 12 1
Emp5 168 170 101 150 89 50 29 0 0
Emp6 84 84 98 60 71 10 12 4 4
Emp7 168 175 104 155 92 20 12 0 0
G Total 1092 1108 101 940 86 190 17 12 1
The totals are correct but the % is wrong.
I have several Datasets because the report only runs the department you are in, except for the VPs who can see all departments.
I Insert the percentage columns into the matrix and have tried several expressions with no results including:
=Fields!ActHrs.Value/Fields!ReqHrs.Value
=Sum(Fields!ActHrs.Value, "Ut_Query")/Sum(Fields!ReqHrs.Value, "Ut_Query")
=Sum(Fields!ActHrs.Value, "Ut_Query","Dept")/Sum(Fields!ReqHrs.Value,
"Ut_Query","Dept")
=Sum(Fields!ActHrs.Value,"Dept", "Ut_Query")/Sum(Fields!ReqHrs.Value,
"Dept","Ut_Query")
Plus more I can't even remember.
I tried creating new groups, and even a new matrix.
There must be a simple way to get the percentage by group but I have not found an answer on any of the interned boards.
OK, I figured this out, but it doesn't make much sense. If I try:
=Textbox29/TextBox28 I get error messages about undefined variables.
If I go the the textbox properties and rename the textboxes to Act and Req and use:
=Act/Req I get the right answer.

SQL query select unique images from multiple tag_id

I have a sql table similar to the one below. And I want to bring back unique results where tag_id matches multiple items.
So if I want to find each image_id that has the tag_id = 106 and 73 I would like it to bring back image_id 12345714 and 12345712.
I've tried doing
SELECT *
FROM tag_relationship
WHERE tag_id
IN ( 106, 73 )
But this brings back
id image_id tag_id
61 12345706 73
70 12345712 73
72 12345712 106
76 12345714 73
77 12345714 106
I've also experimented with GROUP BY, but that doesn't seem to be quite right.
Sample of full table:
image_id can have the same number in different rows, but will have a different tag_id.
id image_id tag_id
1 12345679 63
2 12345679 83
3 12345680 74
4 12345680 108
5 12345680 75
6 12345683 103
7 12345682 87
8 12345682 105
9 12345682 74
10 12345682 81
11 12345683 79
12 12345683 109
13 12345689 111
14 12345689 69
15 12345690 104
16 12345687 110
17 12345687 69
18 12345687 91
19 12345687 93
20 12345687 63
21 12345692 69
22 12345692 104
23 12345692 80
24 12345692 76
25 12345693 74
26 12345693 99
27 12345693 96
28 12345693 94
29 12345691 63
30 12345691 69
31 12345697 92
32 12345697 76
33 12345698 74
34 12345699 97
35 12345698 94
36 12345699 98
37 12345698 81
38 12345699 105
39 12345697 91
40 12345694 100
41 12345694 101
42 12345694 94
43 12345694 74
44 12345696 78
45 12345696 95
46 12345696 112
47 12345701 113
48 12345701 114
49 12345700 94
50 12345700 91
51 12345700 90
52 12345702 87
53 12345702 115
54 12345702 80
55 12345702 74
56 12345704 78
57 12345705 83
58 12345705 84
59 12345704 63
60 12345705 104
61 12345706 73
62 12345706 64
63 12345706 86
64 12345706 88
65 12345706 89
66 12345713 80
67 12345713 115
68 12345713 81
69 12345717 63
70 12345712 73
71 12345717 64
72 12345712 106
73 12345717 79
74 12345712 74
75 12345709 76
76 12345714 73
77 12345714 106
78 12345715 68
79 12345716 116
80 12345715 69
81 12345716 71
SELECT image_id
FROM tag_relationship
WHERE tag_id IN ( 106, 73 )
GROUP BY image_id
HAVING COUNT(*) = 2
SELECT DISTINCT image_id
FROM tag_relationship
WHERE tag_id
IN ( 106, 73 )
It might be really stupid/simple, but I think this will work:
SELECT image_id
FROM tag_relationship
WHERE
tag_id = 106 AND tag_id = 73
Try this
SELECT DISTINCT t1.image_id
FROM tag_relationship t1
JOIN tag_relationship t2 ON t1.image_id=t2.image_id
WHERE t1.tag_id=106
and t2.tag_id=73