Hi I have a query to retrive students score in this format
Id subject class_score total_marks rank subject_type
1 MATLAB 33.80 73.30 11 Core
1 SCIENCE 39.50 83.00 4 Core
1 ENGLISH 37.60 77.60 8 Elective
1 WQP 43.90 77.40 9 Core
1 BDT 42.00 71.50 12 Elective
1 ART 47.10 84.60 2 Elective
1 COMPUTING 26.00 65.50 13 Elective
2 MATLAB 0.00 0.00 14 Core
2 SCIENCE 38.60 73.60 10 Core
2 ENGLISH 43.80 83.30 3 Elective
2 WQP 45.00 89.00 1 Core
2 BDT 41.00 79.50 6 Elective
2 ART 38.00 78.00 7 Elective
2 COMPUTING 40.80 80.80 5 Elective
I wish to calculate the overall score of each student by add the total_marks of all (Core subject + the top 3 score of the student's elective subjects) and rank them from first to last.Please can anyone assist ?Thanks
My current
use ods_sms;
SELECT student_id,sum(t.total_marks)score
FROM
(select
student_id,total_marks from tab_exam_tracker
where (subject_type='Core')
union all select t.student_id,t.total_marks from
(select student_id,total_marks from tab_exam_tracker
where subject_type = 'Elective' order by total_marks desc
limit 3) t
) t
GROUP BY student_id
But am getting wrong results
SELECT
t.id,
SUM(total_marks) AS Core_Marks,
Elective_Marks
FROM tab_exam_tracker t
INNER JOIN (
SELECT id, SUM(total_marks) as Elective_Marks
FROM (
SELECT
id,
total_marks,
IF(#id <> id, #rank:= 1, #rank:= #rank + 1) as rank,
#id:= id
FROM tab_exam_tracker,
(SELECT #id:= 0 r_id, #rank:= 0 AS r) AS a
WHERE subject_type = 'Elective'
ORDER BY id, total_marks DESC
) as b
WHERE rank <= 3
GROUP BY id
) AS e_marks ON t.id = e_marks.id
GROUP BY t.id, Elective_Marks;
Related
I'm using the GridDB Node js Connector and was wondering how I could add multiple tables in a single container while retaining data for all of the tables?
P.S. I went through the SQL documentation for GridDB, so using the JOIN command is not confusing; it is simply the cluster intiliazation. The code from the documentation is attached below:
○Inner join
SELECT * FROM employees e INNER JOIN departments d ON e.department_id=d.department_id;
id first_name department_id department_id department
------+-----------+--------------+--------------+-----------
0 John 0 0 Sales
1 William 1 1 Development
2 Richard 0 0 Sales
4 Lisa 3 3 Marketing
5 James 1 1 Development
○Left outer join
SELECT * FROM employees e LEFT JOIN departments d ON e.department_id=d.department_id;
id first_name department_id department_id department
------+-----------+--------------+--------------+-----------
0 John 0 0 Sales
1 William 1 1 Development
2 Richard 0 0 Sales
3 Mary 4 (NULL) (NULL)
4 Lisa 3 3 Marketing
5 James 1 1 Development
I am working on an algorithm, which requires grouping by two columns. Pandas supports grouping by two columns by using:
df.groupby([col1, col2])
But the resulting dataframe is not the required dataframe
Work Setup:
Python : v3.5
Pandas : v0.18.1
Pandas Dataframe - Input Data:
Type Segment
id
1 Domestic 1
2 Salary 3
3 NRI 1
4 Salary 4
5 Salary 3
6 NRI 4
7 Salary 4
8 Salary 3
9 Salary 4
10 NRI 4
Required Dataframe:
Count of [Domestic, Salary, NRI] in each Segment
Domestic Salary NRI
Segment
1 1 3 1
3 0 0 0
4 0 3 2
Experiments:
group = df.groupby(['Segment', 'Type'])
group.size()
Segment Type Count
1 Domestic 1
NRI 1
3 Salary 3
4 Salary 3
NRI 2
I am able to achieve the required dataframe using MS Excel Pivot Table feature. Is there any way, where I can achieve similar results using pandas?
After the Groupby.size operation, a multi-index(2 level index) series object gets created that needs to be converted into a dataframe, which could be done by unstacking the 2nd level index and optionally filling NaNs obtained with 0.
df.groupby(['Segment', 'Type']).size().unstack(level=1, fill_value=0)
I'm trying to create a calculated column that assigns a rank to each Product based on its sales in each combination of Country, Store, and Quarter.
However, the data in the table is monthly.
I was thinking of using SUMMARIZE() to create the quarterly view and then use RANKX() on that, but the value for the sum of sales cannot be evaluated for each column if I use summarize.
In order to get the rank for the monthly view I have been using the following formula:
=
RANKX (
FILTER (
Table,
Table[Country] = EARLIER ( Table[Country])
&& Table[StoreType] = EARLIER ( Table[StoreType])
),
[Sales]
)
Simplified the data looks like this with the calculated column for monthly rank and the quarterly one I'm trying to figure out
Country StoreType Month Product Sales MonthlyRank QuarterlyRank
USA A Jan-15 P1 10 1 2
USA A Jan-15 P2 15 2 1
USA B Jan-15 P1 5 1 2
USA A Feb-15 P1 5 3 2
USA A Feb-15 P2 20 1 1
USA A Feb-15 P3 10 2 3
USA A Mar-15 P1 10 2 2
USA A Mar-15 P2 25 1 1
USA B Mar-15 P3 15 1 1
How could I be able to achieve this?
You need to add a column to your date dimension that uniquely identifies each quarter. Why don't we call this unique column something like QuarterKey or QuarterIndex.
Then you can do the following in your SUMMARIZE():
ADDCOLUMNS(
SUMMARIZE(
FactSales
,DimGeography[Country]
,DimStore[Store]
,DimDate[QuarterKey]
)
,"SumSales"
,CALCULATE( SUM( FactSales[Net Amount] ) )
)
I am stuck on a query.
I have data like
Uid, resp-date,camp-type
1 201403 A
1 201406 A
1 201406 B
1 201406 B
1 201407 A
1 201407 B
2 201402 A
2 201406 A
2 201406 B
And want to create some metrics like #of prod offered in last month,
The counting logic is:
If the product is A count the number of products offered in the last n month
If the product is B then count the number of products offered in last n months + number of product type A offered in the current month
Uid, resp-date,prod-type,#offered-last1month, #offered-last2month
1 201403 A 0 0
1 201406 A 0 0
1 201406 B 1 1
1 201406 B 1 1
1 201407 A 3 3
1 201407 B 4 4
2 201402 A 0 0
2 201406 A 0 0
2 201406 B 1 1
query:
Select m.uid, m.respdate,m.prodtype,
case when m.prodtype ='A' then ca.num-mails-1month
when m.prodtype ='B' then cb.num-mails-1month
end as mails-last1month
from m
Left outer Join
( select uid, count(* ) as num-mails-1month from
(
What is a way around to write a sub query that refers m.resp-date
)
) ca
on ca.uid =m.uid
Left outer Join
( select uid, count(* ) as num-mails-1month from
(
)
) cb
on cb.uid =m.uid
I have the following table "GroupPriority":
Id Group Priority
1 1 0
2 2 0
3 3 0
4 2 1
5 1 1
6 2 2
7 3 1
I would like to group these on "Group", order them by "Priority" and then get the one in each "Group" with the highest Priority with the use of linq and subsonic 3.
In this case the result would be:
Id Group Priority
5 1 1
6 2 2
7 3 1
The sql would look like this:
SELECT *
FROM GroupPriority
WHERE (Priority =
(SELECT MAX(Priority)
FROM GroupPriority
WHERE (Group = GroupPriority.Group)))
Thanks
Got the solution:
var group_query = new Query<GroupPriority>(provider);
var items = from gp in group_query
where gp.Priority ==
(from gp_sub in group_query
where gp_sub.Group == gp.Group
select gp_sub.Priority).Max()
select gp;