Hi, I need two tables for the result. I can do it in three queries in Access, but I really don't know what should I do in SQL Server. I hope someone can guide me since I have so many queries that I need to convert from Access to SQL.
Thank You.
TableA
Name|Subject|Course|Sort|Mark
Row1 YY|MathI|Math010|01|75
Row2 YY|MathI|Math011|02|65
Row3 YY|MathI|Math014|15|50
Row4 YY|MathI|Math015|04|80
Row5 YY|MathI|Math035|20|90
Row6 ZZ|MathII|Math010|01|75
Row7 ZZ|MathII|Math011|02|65
Query 1:
Select
Name,
Subject,
Course,
Sort,
SUM(Mark) As Mark
From
TableA
WHERE Sort >=N'01' and <=N'15'
GOUP BY Name, Subject, Course, Sort
Result:
Name|Subject|Course|Sort|Mark
Row1 YY|MathI|Math010|01|75
Row2 YY|MathI|Math011|02|65
Row3 YY|MathI|Math014|15|50
Row4 YY|MathI|Math015|04|80
Row6 ZZ|MathII|Math010|01|75
Row7 ZZ|MathII|Math011|02|65
Query 2:
Select
Name,
Subject,
SUM(Mark) As Mark
From
TableA
GOUP BY Name, Subject
Result:
Name|Subject||Mark
Row1 YY|MathI|270
Row2 ZZ|MathII|140
Table B
Name|Subject|LastYrMark
Row1 YY|Math1|150
Row2 ZZ|Math2|500
Join Query 2 & Table B
Select
Query2.Name,
Query2.Subject,
SUM(Query2.Mark) As Mark
TableB.LastYrMark
From
Query2
Left Outer Join
Query2.Name = TableB.Name AND
Query2.Subject = TableB.Subject
GOUP BY Query2.Name, Query2.Subject
FinalResult:
Name|Subject||Mark|LastYearMark
Row1 YY|MathI|270|150
Row2 ZZ|MathII|140|500
Thanks for your help.
Thank You.
TableA
Name|Subject|Course|Sort|Mark
Row1 YY|MathI|Math010|01|75
Row2 YY|MathI|Math011|02|65
Row3 YY|MathI|Math014|15|50
Row4 YY|MathI|Math015|04|80
Row5 YY|MathI|Math035|20|90
Row6 ZZ|MathII|Math010|01|75
Row7 ZZ|MathII|Math011|02|65
Query 1:
Select
Name,
Subject,
Course,
Sort,
SUM(Mark) As Mark
From
TableA
WHERE Sort >=N'01' and <=N'15'
GOUP BY Name, Subject, Course, Sort
Result:
Name|Subject|Course|Sort|Mark
Row1 YY|MathI|Math010|01|75
Row2 YY|MathI|Math011|02|65
Row3 YY|MathI|Math014|15|50
Row4 YY|MathI|Math015|04|80
Row6 ZZ|MathII|Math010|01|75
Row7 ZZ|MathII|Math011|02|65
Query 2:
Select
Name,
Subject,
SUM(Mark) As Mark
From
TableA
GOUP BY Name, Subject
Result:
Name|Subject||Mark
Row1 YY|MathI|270
Row2 ZZ|MathII|140
Table B
Name|Subject|LastYrMark
Row1 YY|Math1|150
Row2 ZZ|Math2|500
Join Query 2 & Table B
Select
Query2.Name,
Query2.Subject,
SUM(Query2.Mark) As Mark
TableB.LastYrMark
From
Query2
Left Outer Join
Query2.Name = TableB.Name AND
Query2.Subject = TableB.Subject
GOUP BY Query2.Name, Query2.Subject
FinalResult:
Name|Subject||Mark|LastYearMark
Row1 YY|MathI|270|150
Row2 ZZ|MathII|140|500
Thanks for your help.