230 likes | 329 Views
Access Patterns. we have seen. We review some of the common patterns we have used. IMPORTANT NOTE: SQL is given for informational purpose only. We have not covered SQL but used the Query Builder instead. Table. ID A B C 1 a1 b1 5 2 a1 b1 8 3 a1 b2 7 4 a1 b2 2 5 a1 b2 1 6 a2 b1 7
E N D
Access Patterns we have seen
We review some of the common patterns we have used. • IMPORTANT NOTE: SQL is given for informational purpose only. We have not covered SQL but used the Query Builder instead.
Table • ID A B C • 1 a1 b1 5 • 2 a1 b1 8 • 3 a1 b2 7 • 4 a1 b2 2 • 5 a1 b2 1 • 6 a2 b1 7 • 7 a2 b1 8 • 8 a3 b2 0
What we want: summing for distinguished column of another column • A SumOfC • a1 23 • a2 15 • a3 0
SQL query SELECT Table1.A, Sum(Table1.C) AS SumOfC FROM Table1 GROUP BY Table1.A;
Query Builder Manipulation • Create Query • Choose Table1 • Select column A • Select column C • Totals (GroupBy default) • Sum for column C
Table • ID A B C • 1 a1 b1 5 • 2 a1 b1 8 • 3 a1 b2 7 • 4 a1 b2 2 • 5 a1 b2 1 • 6 a2 b1 7 • 7 a2 b1 8 • 8 a3 b2 0
What we want: Counting for distinguished column • B CountOfB • b1 4 • b2 4
SQL SELECT Table1.B, Count(Table1.B) AS CountOfB FROM Table1 GROUP BY Table1.B;
Query Builder Manipulation • Create Query • Select Table1 • Select column B • Select column B • Totals (GroupBy) • For second column B choose count
Table2 (DebateResults) • ID winner loser forced • 1 1 2 1 • 2 2 1 2 • 3 1 3 3 • 4 3 2 2 • 5 4 1 4 • 6 1 4 4 • 7 1 5 0
What we want: count wins for each debater • winner CountOfwinner • 1 4 • 2 1 • 3 1 • 4 1
SQL • SELECT Table2.winner, Count(Table2.winner) AS CountOfwinner • FROM Table2 • GROUP BY Table2.winner;
Query Builder Manipulation • Create Query • Choose Table2 • Select winner column twice • Totals (GroupBy) • For second winner column: Count
Table2 (DebateResults) • ID winner loser forced • 1 1 2 1 • 2 2 1 2 • 3 1 3 3 • 4 3 2 2 • 5 4 1 4 • 6 1 4 4 • 7 1 5 0
What we want: count faults • FaulterCountOfFaulter • 1 2 • 2 1 • 5 1
Add calculated field for Faulters: subquery • What do we want for the subquery?
Create Faulter column from Loser column • Faulter forced • 2 1 • 1 2 • 1 4 • 5 0
SQL • SELECT Table2.loser AS Faulter, Table2.forced • FROM Table2 • WHERE (((Table2.loser)<>[forced]));
Query Builder Manipulation • Create Query • select loser column; rename to Faulter; condition <>[forced] • select forced column (for checking result) • name subquery: Faults
Reminder: What we want • FaulterCountOfFaulter • 1 2 • 2 1 • 5 1
SQL • SELECT Faults.Faulter, Count(Faults.Faulter) AS CountOfFaulter • FROM Faults • GROUP BY Faults.Faulter;
Query Builder Manipulation • Create Query • Choose subquery Faults • Select Faulter column twice • Totals (GroupBy) • select Count for second