I am using this SQL:
TRANSFORM Max(table1.[quiz]) AS MaxOfquiz
SELECT table1.[quizdate], table1.[studentname], Max(table1.[quiz]) AS [Total Of
quizscores]
FROM table1
GROUP BY table1.[quizdate], table1.[studentname]
PIVOT table1.[coursename];
To try and pivot a combobox table:
Table1
ID quizdate coursename studentname quiz
1 02-Jan-21 math john 100
2 03-Feb-21 science joe 99
3 04-Mar-21 physics monica 97
4 05-Apr-20 language mike 88
Where coursename is a dropdown (math, science, physics, language) into
Table 2
studentname math science physics language
john 100
joe 99
monica 97
mike 88
I got these results:
quizdate studentname Total Of quizscores 1 2 3 4
4/4/2016 moe 88 88
1/1/2017 john 100 100
2/2/2017 joe 99 99
3/3/2017 monica 97 97
It seems that combox box coursename can't be pivoted as column name, but the
numbers instead. Can anyone explain how I can generate the correct results?
You answer it yourself:
Where coursename is a dropdown (math, science, physics, language)
So, in the query, the value from the field will be the ID of these "dropdowns".
To obtain coursenames, move these to a separate table and join this with Table1 in your query.
Related
I have a query made by joining data from three tables in a MS-Access DB
simsID Forename Surname Class AssessmentName Mark Percentage
1234 Joe Bloggs 13X Test1 20 50
1235 Fred Bloggs 13Y Test1 31 77.5
1234 Joe Bloggs 13X Test2 30 60
1235 Fred Bloggs 13Y Test2 10 20
1235 Fred Bloggs 13Y Test3 20 33.3333333333333
1234 Joe Bloggs 13X Test3 34 56.6666666666667
I would like to display the data as follows
ID Forename Surname Class Test1 Mark Test1 % Test2 Mark Test2 % Test3 Mark Test3 %
1234 Joe Bloggs 13X 20 50 30 60 34 56.6666666666667
1235 Fred Bloggs 13Y 31 77.5 10 20 20 33.3333333333333
The only way I can see is to do two crosstab queries and then join them.
AllStudentData_Marks query
TRANSFORM Avg(AllStudentData.Mark) AS AvgOfMark
SELECT AllStudentData.simsID AS ID, AllStudentData.Forename AS Forename, AllStudentData.Surname AS Surname, AllStudentData.Class AS Class
FROM AllStudentData
GROUP BY AllStudentData.simsID, AllStudentData.Forename, AllStudentData.Surname, AllStudentData.Class
PIVOT AllStudentData.Assessments.AssessmentName & " Mark";
AllStudentData_Precentage query
TRANSFORM Avg(AllStudentData.Percentage) AS AvgOfPercentage
SELECT AllStudentData.simsID AS ID, AllStudentData.Forename AS Forename, AllStudentData.Surname AS Surname, AllStudentData.Class AS Class
FROM AllStudentData
GROUP BY AllStudentData.simsID, AllStudentData.Forename, AllStudentData.Surname, AllStudentData.Class
PIVOT AllStudentData.Assessments.AssessmentName & " %";
Join query
SELECT AllStudentData_Marks.*, AllStudentData_Percentage.*
FROM AllStudentData_Marks INNER JOIN AllStudentData_Percentage ON AllStudentData_Marks.ID = AllStudentData_Percentage.ID;
As expected this gives me a big table:
AllStudentData_Marks.ID AllStudentData_Marks.Forename AllStudentData_Marks.Surname AllStudentData_Marks.Class Test1 Mark Test2 Mark Test3 Mark AllStudentData_Percentage.ID AllStudentData_Percentage.Forename AllStudentData_Percentage.Surname AllStudentData_Percentage.Class Test1 % Test2 % Test3 %
1234 Joe Bloggs 13X 20 30 34 1234 Joe Bloggs 13X 50 60 56.6666666666667
1235 Fred Bloggs 13Y 31 10 20 1235 Fred Bloggs 13Y 77.5 20 33.3333333333333
I would like to limit the amount of columns (ie not repeat the names) and have more sensible column names (Forename, Surname, etc), but the number of assessment columns is not fixed hence why I am using the wildcard in SELECT.
How can I limit this final query to just return my compact table with sensible headings? ie
ID Forename Surname Class Test1 Mark Test1 % Test2 Mark Test2 % Test3 Mark Test3 %
1234 Joe Bloggs 13X 20 50 30 60 34 56.6666666666667
1235 Fred Bloggs 13Y 31 77.5 10 20 20 33.3333333333333
Thanks in advance for reading.
Yes, two joined CROSSTABS is one way to pivot two sets of values. Another method described in http://allenbrowne.com/ser-67.html#MultipleValues.
And yet another approach involves a UNION query which is then used as source for CROSSTAB. Working with the sample dataset you posted, consider:
Query1:
SELECT simsID, Forename, Surname, Class, AssessmentName, Mark AS Data, "M" AS Cat FROM dataset
UNION SELECT simsID, Forename, Surname, Class, AssessmentName, Percentage, "P" AS Cat FROM dataset;
Query2:
TRANSFORM Sum(Query1.Data) AS SumOfData
SELECT Query1.simsID, Query1.Surname, Query1.Class
FROM Query1
GROUP BY Query1.simsID, Query1.Forename, Query1.Surname, Query1.Class
PIVOT [AssessmentName] & [Cat];
Might find this of interest Crosstab Query on multiple data points
id subject points Rank
joe maths 70 1
Mike maths 60 2
Sarah maths 40 3
mike English 80 1
Sarah English 65 2
joe English 55 3
Sarah Chemistry 80 1
Mike Chemistry 60 2
joe Chemistry 43 3
I was able to query this in mysql but i want to store the rank column in the table (grades) using the ALTER command
i tried
ALTER TABLE grades
ADD COLUMN Rank
GENERATE ALWAYS AS
DENSE_RANK() OVER(PARTITION BY subjects ORDER BY points DESC)
ORDER BY id,
Rank
but did not work
Hope i could find some help out there
I am trying to grab a participants rankings in a multi-event tournament.
I can do a ranking for a single event pretty easily. Is there a way to find ALL in one go?
Given input: "Bob"
Data example: Desired output:
Name | Event | Score Name | Event | Score | Rank
-------------------- ----------------------------
Bob 1 100 Bob 1 100 1
Bob 2 75 Bob 2 75 3
Bob 3 80 Bob 3 80 2
Jill 2 90
Jill 3 60
Chris 1 70
Chris 2 50
Chris 3 100
Amy 1 85
Amy 2 95
Amy 3 65
The catch: I do not have access to the Rank()
function with my version of SQL, and updating is not possible in this scenario.
Clearly I could just do the score per event separately in a loop,
but I'd like to try to do it all in one go.
You can emulate a ranking function in MySQL using a self-join to values with a higher score in the same Event, and then counting the number of higher scores for each participant:
SELECT s1.Name, s1.Event, s1.Score, COUNT(s2.Name)+1 AS Rank
FROM scores s1
LEFT JOIN scores s2 ON s2.Event = s1.Event AND s2.Score > s1.Score
WHERE s1.Name = 'Bob'
GROUP BY s1.Name, s1.Event, s1.Score
ORDER BY s1.Name, s1.Event
Output:
Name Event Score Rank
Bob 1 100 1
Bob 2 75 3
Bob 3 80 2
Demo on dbfiddle
I have three tables
Student
studenid stuname
101 john
102 aron
103 mary
104 lucy
Subject
studenid subjid subjname
101 1 maths
102 2 science
103 3 computer
104 4 english
Marks
subjid mark
1 50
2 40
3 55
4 60
1 40
2 55
3 60
I want output like this where studenid (sum of mark as total)
studenid stuname mark
101 john 90
102 aron 95
103 mary 115
104 lucy 60
Thank you in advance for yout help, i want output like this even join query or subquery which is best for timing
This just requires a straight left join across all tables, with an aggregation by student.
SELECT
st.studenid,
st.stuname,
COALESCE(SUM(m.mark), 0) AS mark
FROM Student st
LEFT JOIN Subject su
ON st.studenid = su.studenid
LEFT JOIN Marks m
ON su.subjid = m.subjid
GROUP BY
st.studenid,
st.stuname;
Demo
Note that if studenid be a primary key in the Student table, then strictly we would only need to aggregate by this column alone.
I am having a table student. I need top 3 highest records from each group. The query should work for dynamically added groups. Without setting default value of groups in the query.
student table:
id name mark subject
--------------------------------------
1 kannan 60 French
2 balan 77 French
3 raja 88 French
4 sheik 78 French
5 satheesh 98 French
6 Ravi 90 French
7 Vishnu 90 English
8 siva 100 English
9 suresh 50 English
10 ramesh 59 English
11 ganesh 97 English
12 david 58 English
Expected Result:
name mark subject
---------------------------
Ravi 90 French
Satheesh 98 French
raja 88 French
siva 100 English
ganesh 97 English
ramesh 59 English
Query i tried:
SELECT name, mark, subject from (SELECT name, mark, subject order by mark
desc LIMIT 3) as ss group by subject, mark;
But I am not getting the correct values.
(SELECT NAME ,mark, SUBJECT FROM `student ` WHERE SUBJECT = 'French' ORDER BY mark DESC LIMIT 3)
UNION
(SELECT NAME ,mark, SUBJECT FROM `student ` WHERE SUBJECT = 'English' ORDER BY mark DESC LIMIT 3)