enter image description here
As image above
I need to sum the total with conditions
-Student has more then subjects
-every subject has 2 or 3 quizzes and final and re- test ( re-test :if the student get less then 60 from The final
the total final if the Subject has 2 quizzes = 15% from Q1 + 15% from Q1 +70% from final or from re-test
the total final if the Subject has 3 quizzes = 10% from Q1 + 10% from Q1 + 10% from Q3 +70% from final or from re-test
Notice: only one subject has 3 quizzes
Related
I'm developing a new Website where there's some "entities" to vote.
Every vote could be a number between 1 and 5 where 1 is the worst vote and 5 is the best vote.
Now, in the same website I have a "Popular entities chart" where I list the most popular "entities" based on their vote.
Now, I can't do a simply arithmetic average because an "entity" with one vote of 5 could have the same ranking as an "entity" with 100 votes of 5.
I thought about storing for every "entity" not ony the arithmetic average but also the numbers of votes and doing an SQL Query where I order by number of votes and arithmetic average but seems that after this, an entity with many votes of 1 could get popularity (when it isn't popular).
What algorithm could I use?
For a basic solution try order by [average vote] desc, [vote count] desc this way out of two entities with the same average, the one with 100 votes will go above the one with 1 vote, but one with average of 4.5 will never go above one with average of 5.
Edit 1
If you want 100 vote average of 4.5 to win against 10 vote average of 5, why not count votes ignoring 1, 2 and 3, or [count of votes 4 and 5] - [count of votes 1 and 2]? This way count of positive votes would pull entities up in ranking.
Edit 2
You might want to give extra importance to recent votes. Something might have changed about an entity that changed user opinion of it. Could build another average of votes made last month and adjust final ranks based on it.
Edit 3
What about calculate a [popularityScore] column and just order by it?
-- sum instead of average
-- square root of sum will reduce importance of vote count a bit
select
entity,
sqrt(sum(vote - 3)) as popularityScore
from Votes
group by entity
order by rank desc
-- 50 votes of 5 -> popularityScore = 12.25
-- 100 votes of 4 -> popularityScore = 10
-- 200 votes of 4 -> popularityScore = 14.14
-- 2000 votes of 4 -> popularityScore = 44.72
-- 2000 votes of 5 -> popularityScore = 63.25
-- 100000000 votes of 3 -> popularityScore = 0
Could calculate same score for last month and add it to this value.
I've got a puzzle for you... I have database table that stores scores for a game. It records the name of the player, the name of their computer opponent, and the final scores for each:
player_name | bot_name | player_score | bot_score
---------------------------------------------------
Alan bot1 2 1
Bill bot1 3 0
Casey bot2 5 0
Alan bot2 0 3
Bill bot3 1 2
Casey bot3 4 0
Alan bot4 0 3
For each bot, the player with the largest margin of victory is the one with the highest (player_score - bot_score) value. I want to write a SQL query that finds the player who has the most largest margins of victory.
Example: In the table above, Bill has the largest margin of victory over bot1, Casey has the largest margins of victory over bot2 and bot3, and no player has a largest margin of victory over bot4 since no player won against that bot. So, Bill has one largest margin of victory, and Casey has two. In this scenario, the query should return "Casey" and "2" since that's the player with the most largest margins of victory.
Is it possible to write a single SQL query to retrieve this information?
This is rather complicated. For each bot, you can the player (or players) with the largest margin by doing:
select bot_name, max(player_score - bot_score) as max_diff
from t
where player_score > bot_score
group by bot_name;
To get the players with the maximum score is a bit trickier:
select t.*
from t
where (t.player_score - t.bot_score) =
(select max(player_score - bot_score) as max_diff
from t t2
where t2.player_score > t2.bot_score and
t2.bot_name = t.bot_name
);
But, that is still not what you want. You want the players with the largest such wins. Here is how you get the list in order:
select t.player_name, count(*) as num_wins
from t
where (t.player_score - t.bot_score) =
(select max(player_score - bot_score) as max_diff
from t t2
where t2.player_score > t2.bot_score and
t2.bot_name = t.bot_name
)
group by t.player_name
order by num_wins desc;
I'm trying to make a cross tabulation in R, and having its output resemble as much as possible what I'd get in an Excel pivot table. The objective is to replace a report made manually with Excel and Word with one automated with R Markdown; data wrangling and charts have been already taken care of but some tables are missing. So, given this code:
set.seed(2)
df<-data.frame("ministry"=paste("ministry ",sample(1:3,20,replace=T)),"department"=paste("department ",sample(1:3,20,replace=T)),"program"=paste("program ",sample(letters[1:20],20,replace=F)),"budget"=runif(20)*1e6)
library(tables)
library(dplyr)
arrange(df,ministry,department,program)
tabular(ministry*department~((Count=budget)+(Avg=(mean*budget))+(Total=(sum*budget))),data=df)
which yields (actual data is much more complicated):
Avg Total
ministry department Count budget budget
ministry 1 department 1 5 479871 2399356
department 2 1 770028 770028
department 3 1 184673 184673
ministry 2 department 1 2 170818 341637
department 2 1 183373 183373
department 3 3 415480 1246440
ministry 3 department 1 0 NaN 0
department 2 5 680102 3400509
department 3 2 165118 330235
This is as close as I could get to Excel results. I need to display subtotals, like this (generated in Excel using the exact same data):
Is it possible at all to get something like this in R (without manually coding the table cell-by-cell)?
Thanks!
Replace the left hand side with:
ministry * (department + 1) + 1
That is, try this:
tabular(ministry * (department + 1) + 1 ~
((Count = budget) + (Avg = (mean * budget)) + (Total = (sum * budget))),
data = df)
giving:
Avg Total
ministry department Count budget budget
ministry 1 department 1 5 479871 2399356
department 2 1 770028 770028
department 3 1 184673 184673
All 7 479151 3354057
ministry 2 department 1 2 170818 341637
department 2 1 183373 183373
department 3 3 415480 1246440
All 6 295242 1771449
ministry 3 department 1 0 NaN 0
department 2 5 680102 3400509
department 3 2 165118 330235
All 7 532963 3730744
All 20 442813 8856250
Update: correction.
Table 1:Domain Link Result
======================================================================
||Column1(words) ||Column2(links) ||Column3(frequency) ||
======================================================================
1 1 Any Number
2 1 Any Number
3 1 Any Number
4 1 Any Number
1 2 Any Number
2 2 Any Number
3 2 Any Number
4 2 Any Number
Table 2:Sub Link Result
======================================================================
||Column1(words) ||Column2(sublinks) ||Column3(frequency) ||
======================================================================
1 a Any Number
2 b Any Number
3 c Any Number
4 d Any Number
1 e Any Number
2 f Any Number
3 g Any Number
4 h Any Number
And so on.
In the above scenario user entered 4 words and 2 domain links. Now the frequency of 4 keywords is calculated on domain links as well sublinks and stored in separate tables as shown above. I want an aggregate result like below:
Table 3:Final Result
==================================================================================
||Column1(words) ||Column2(Domain links) ||Column3(Total frequency) ||
==================================================================================
Row1: 1 1 Total of frequency in both tables
2 for word "1"
----------------------------------------------------------------------------------
Row2: 2 1 Total of frequency in both tables
2 for word "2"
----------------------------------------------------------------------------------
Row3: 3 1 Total of frequency in both tables
2 for word "3"
----------------------------------------------------------------------------------
Row4: 4 1 Total of frequency in both tables
2 for word "4"
----------------------------------------------------------------------------------
I tried the following query in MySQL:
SELECT t.`keyword`, t.`link` SUM( t.`frequency` ) AS total
FROM (
SELECT `frequency`
FROM `domain_link_result`
WHERE `keyword` = 'national'
UNION ALL
SELECT `frequency`
FROM `sub_link_result`
WHERE `keyword` = 'national'
)t GROUP BY `keyword`
But in Column 2 of the final result I get only first link instead of two links for row 1. How can I get both links or any number of links entered by user in a single row ?
Words and Links have VARCHAR as type and frequency has INT type.
If you want to collapse several rows into one and still be able to see the information, you have to use GROUP_CONCAT
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
This outputs the collapsed values separated by commas, i.e.: a string. In your programming language you can split this string if you need individual values.
Your query would look somehow like this
SELECT keyword, GROUP_CONCAT(links), SUM(frequency)
FROM (subquery)
GROUP BY keyword
Which would output something like this:
==================================================================================
||Column1(words) ||Column2(Domain links) ||Column3(Total frequency) ||
==================================================================================
Row1: 1 1,2 sum of freq.
----------------------------------------------------------------------------------
Row2: 2 1,2 sum of freq.
----------------------------------------------------------------------------------
Row3: 3 1,2 sum of freq.
----------------------------------------------------------------------------------
Row4: 4 1,2 sum of freq.
EDIT: Extra help for your query
Your query looks a little bit confusing to me. Try with a JOIN approach:
SELECT domain_link_results.word AS word,
GROUP_CONCAT(domain_link_results.links) AS domain_links,
domain_link_results.frequency + sub_link_results.frequency AS total_frequency
FROM domain_link_results
INNER JOIN sub_link_results
ON domain_link_results.word = sub_link_results.word
WHERE domain_link_results.word = "national"
GROUP BY domain_link_results.word
On the other hand, it might be better to have all the links in the same table, and an extra field to determine if it's a domain link or a sublink. Without knowing more about your system it is hard to say.
With this dataset:
Category | Amount
A | 5
A | 3
B | 6
B | 2
B | 1
C | 7
I want to create a tablix grouping on category, displaying the percentage of the total amount:
Category | Percentage
A | 33%
B | 38%
C | 29%
Which should be a simple calculation:
Category | Percentage
A | ((Sum of Amount within group / Sum of Amount across groups) * 100)%
B | ((Sum of Amount within group / Sum of Amount across groups) * 100)%
C | ((Sum of Amount within group / Sum of Amount across groups) * 100)%
But I can't figure out how to do that within Report Designer (SSRS) - whenever I create a row grouping on Category, I can get the sum within the group with =Sum(Fields!Amount.Value). But how to get the sum across groups from a cell within the group?
I'll answer my own question.
From within any expression, it's possible to perform lookups in all datasets. Through this way we'll get the data:
LookupSet(SourceFieldToCompare, TargetFieldToCompare, ResultField, DataSet)
Now, let's raise the bar for the question and say the data is grouped in yet another dimension, months - like this:
Category | January | February | March
A | 33% | 37% | 35%
B | 38% | 36% | 37%
C | 29% | 27% | 28%
Say the dataset mentioned in the question is named 'categories'. Now, call on the LookupSet function (Reference):
LookupSet(Fields!Month.Value, Fields!Month.Value, Fields!Amount.Value, "categories")
(keep in mind that the first "Month" is linked to the dataset inside the tablix, and the second "Month" in the second argument is "Month" from the "categories" dataset!)
There remains one problem: LookupSet returns Object types, which Sum won't eat. You need to use a custom aggregate, (custom code is added in "Report Properties"): (Source)
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
suma = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
Next
Return suma
End Function
Now, by calling Code.SumLookup on the LookupSet function the sum is calculated of all fields.
You could have used Scope descriptors to identify the groups that the SUM is to be run against:
Category group is defined in tablix Row Groups.
DataSet1 is the name of the dataset.
Grouped Amount: [Sum(Amount)]
Dataset Total: SUM(Fields!Amount.Value, "DataSet1")
Percentage: SUM(Fields!Amount.Value, "Category") /SUM(Fields!Amount.Value, "DataSet1")