SSRS Case Statement - reporting-services

I have created a table in SSRS of various grades, but I need to look at the value of 2 grades together and assign it an alternate grade. I cannot put this as a CASE in the SELECT as the way the database is designed the values are not stored in one row, but in multiple rows, therefore it cannot combine the data in a new column. For example, this is one students grades represented in the DB
634 Attainment *#1#2#3#4#N/A NULL 1 2
636 Effort A*#A#B#C#N/A NULL A 2
637 Focus EX#ME#WB#N/A NULL EX 1
638 Participation EX#ME#WB#N/A NULL ME 2
639 Groupwork EX#ME#WB#N/A NULL ME 2
640 Rigour EX#ME#WB#N/A NULL ME 2
641 Curiosity EX#ME#WB#N/A NULL ME 2
642 Initiative EX#ME#WB#N/A NULL ME 2
643 Self Organisation EX#ME#WB#N/A NULL ME 2
644 Perseverance EX#ME#WB#N/A NULL ME 2
I have created a table that has grouped the grades based on the pupil ID and it is now represented as one row and column headings for each grade (effort, Focus etc).
I have tried to do a sum using the ReportItems!Textbox1.Value but I can't use this method as it is not an aggregate function. What I wanted to do was
IF (ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 2) THEN 5
Is there a way to do this?
ADDITIONAL:
I have just tried:
=SWITCH(ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 2, 5,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 3, 4,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 4, 3,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 5, 2,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 6, 1,
ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 7, 0,
"NULL"
)
This is returning an Error.

I finally resolved this by using this expression:
=IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 2), 5,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 3), 4,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 4), 3,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 5), 2,
IIF(ReportItems!Textbox104.Value + (ReportItems!Textbox105.Value = 6), 1,
IIF(ReportItems!Textbox104.Value + ReportItems!Textbox105.Value = 7, 0,
"NULL"))))))

Related

how to define dynamic nested loop python function

a = [1]
b = [2,3]
c = [4,5,6]
d = [a,b,c]
for x0 in d[0]:
for x1 in d[1]:
for x2 in d[2]:
print(x0,x1,x2)
Result:
1 2 4
1 2 5
1 2 6
1 3 4
1 3 5
1 3 6
Perfect, now my question is how to define this to function, considering ofcourse there could be more lists with values. The idea is to get function, which would dynamicaly produce same result.
Is there a way to explain to python: "do 8 nested loops for example"?
You can use itertools to calculate the products for you and can use the * operator to convert your list into arguments for the itertools.product() function.
import itertools
a = [1]
b = [2,3]
c = [4,5,6]
args = [a,b,c]
for combination in itertools.product(*args):
print combination
Output is
(1, 2, 4)
(1, 2, 5)
(1, 2, 6)
(1, 3, 4)
(1, 3, 5)
(1, 3, 6)

MySQL: Value of child lines based on proportion of cost?

We have a packslip lines table with the following structure (simplified):
line_id (unique id for packslip line)
sku (item #)
name
weight
value (value/price of item)
cost
is_kit (is this a kit/parent item?)
parent_line_id (if it's a child item, will contain line_id of parent)
A packslip line can represent an individual product, a parent kit, or kit components. For this exercise, use the following data set:
1, 'ITEM1', 'Item # 1', 0.3, 9.99, 4.79, 0, null
2, 'KIT1', 'Kit # 1', 1.3, 29.99, 0, 1, null
3, 'KITITEM1', 'Kit Item # 1', 0.7, 0, 10.0, 0, 2
4, 'KITITEM2', 'Kit Item # 2', 0.3, 0, 2.49, 0, 2
5, 'KITITEM3', 'Kit Item # 3', 0.3, 0, 4.29, 0, 2
As you can hopefully see, ITEM1 is a regular/individual product, KIT1 is a parent kit, and the last 3 items are child components for KIT1.
Notice that the kit lacks a cost and that the kit items lack a value. I need to create a query that will calculate the kit item values based on the proportion of the items' costs to the overall cost of the kit.
So in this example:
KITITEM1 Value = 10 / (10.0 + 2.49 + 4.29) * 29.99 = $17.87
KITITEM2 Value = 2.49 / (10.0 + 2.49 + 4.29) * 29.99 = $4.45
KITITEM3 Value = 4.29 / (10.0 + 2.49 + 4.29) * 29.99 = $7.67
Can I accomplish this in a single query (can have nested queries)? How?
try this query, sqlFiddle
SELECT T1.line_id,
T1.sku,
T1.name,
T1.weight,
IF (T1.parent_line_id IS NULL,T1.value,
ROUND(T1.cost * T2.value_divided_by_total_cost,2))as value,
T1.cost,
T1.is_kit,
T1.parent_line_id
FROM packslip T1
LEFT JOIN
(SELECT parent_line_id,(SELECT value FROM
packslip p2 WHERE p1.parent_line_id = p2.line_id)
/SUM(cost)
as value_divided_by_total_cost
FROM packslip p1
WHERE parent_line_id IS NOT NULL
GROUP BY parent_line_id
)T2
ON T1.parent_line_id = T2.parent_line_id
The query LEFT JOIN with a derived table that gets the (value of parent) divided by SUM(cost) GROUP BY that parent.
Then the outer query checks to see if parent_line_id IS NOT NULL and multiplies the cost by value from the derived table.

Does mysql query cache the dynamically calculated columns

I have a mysql query:
SELECT my_table.* WHERE SOUNDEX(my_col)='whatever' OR SUBSTR(SOUNDEX(my_col),4)='whatever' ORDER BY SUBSTR(SOUNDEX(my_col),4)='whatever',SOUNDEX(my_col)='whatever'
How many times will the substring function and soundex functions will actually be called? I mean for exactly same inputs will mysql cache the results over the span of one query?
If not, how can I make the change in the query so that each function is called minimum times possible.
MySQL would call this function four times for every returned row, to avoid this you can use a subquery, so instead of
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)
You can do
SELECT * from (select *, Soundex("aaaa") as current_soundex from song)
ORDER BY
Substr(pre_calculated_soundex, 1, 1) = Substr(current_soundex , 1, 1)
+ Substr(pre_calculated_soundex, 2, 1) = Substr(current_soundex , 2, 1)
+ Substr(pre_calculated_soundex, 3, 1) = Substr(current_soundex , 3, 1)
+ Substr(pre_calculated_soundex, 4, 1) = Substr(current_soundex , 4, 1)

SQL update multiple rows with same value

I use this to update (add points) rows which mgroup is 15
UPDATE ibf_members SET points = points + 500 WHERE mgroup = 15
What can I use to update (add points + 500) for rows which has its id as 5, 7, 10, 11, 16, 25 and also has mgroup as 15?
You can use the IN clause for this, which is easier to read (and possibly more efficient?) than building a giant OR list. Try something like:
UPDATE ibf_members
SET points = points + 500
WHERE mgroup = 15
AND id IN (5, 7, 10, 11, 16, 25);
Just add another condition to your WHERE clause:
UPDATE ibf_members SET points = points + 500 WHERE mgroup = 15 AND id IN (5, 7, 10, 11, 16, 25)

Finding matches between 30,000+ data sets

For simplicity sake, lets use a case example of 3 colors with corresponding numbers (there is actually 30,000+ different 'colors' and 254 different 'numbers' in real life though)
Red - 0, 1, 2, 3, 10, 15
Green - 0, 2, 3, 20
Blue - 2, 10, 11, 12
I want to find the matches between them (rgb, rg, rb), as well as keep a tally of the number of #s shared between the set:
rgb = 1
rg = 2
rb = 2
Finally it'll need to determine a ratio of the number of #s shared compared to the number of distinct #s in the set.
rgb = 1/9 (since it has a distinct: 0, 1, 2, 3, 10, 11, 12, 15, 20)
rg = 2/7 (0, 1, 2, 3, 10, 15, 20)
rb = 2/8 (0, 1, 2, 3, 10, 11, 12, 15)
So the total output would be
match | # of matches | % |
rgb | 1 | 1/9
rg | 2 | 2/7
rb | 2 | 2/8
The algorithm I was able to come up w/is you have each color in a table and map the numbers associated with it (aka red (table Name), 0, 1, 2, 3, 10, 15 (data)). Then take the color with the most 'numbers' and compare it to every other color's hours, find matches. Once done with that color you can 100% ignore it and move onto the next color and do comparisons with n-1.
Take the example:
1) Select red
2) Does any other color share 0
3) Does any other color share 1
....etc
4) Select blue
5) Does any other color minus red share .....
I know that there has to be a more efficient way to do this, any suggestions?
Thanks for the help.
As there are only 254 (or 255 if your 0-254 comment is correct), then you can express the sets of 'numbers' for each 'colour' as a 256-bit integer. Then the number of shared numbers for r and g is just the bitcount of (r and b) and the number of distinct numbers is the bitcount of (r or b), so using your example,
if R is the bit-set for red, G is the bitset for B etc:
match | # of matches | % |
rgb | bitcount(R and G and B) | bitcount(R and G and B)/bitcount(R or G or B) |
rg | bitcount(R and G) | bitcount(R and G)/bitcount(R or G) |
rb | bitcount(R and B) | bitcount(R and B)/bitcount(R or B) |