Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
[QueryID:37] For each employee with a commission rate of less than 0.04, compute the number of orders taken in January 2004. The result should include the employee number, the employee last name, and the number of orders taken. The following steps may be helpful in formulating the query for the problem: 1. Mapping the data items required to the column names in the DB employee number EmpNo employee last name EmpLastName number of orders taken by employees COUNT(OrdNo) 2. Interpret the row selection condition Orders placed in January 2004 OrdDate is in January 2004 OrdDate >= 1-Jan-2004 and OrdDate <= 31-Jan2004 Alternatively OrdDate between 1Jan-2004 and 31-Jan-2004 the order month is January and order year is 2004 date_part(month, OrdDate) = 1 and date_part(year, OrdDate) = 2004 any dates in January 2004 OrdDate like *-Jan-2004 (not recommended) Employee commission rate is less than 0.04 EmpCommRate < 0.04 3. Identify the table(s) needed, which contain(s) all the data items used in 1) and 2) orderTbl table (M-table, FK) employee table (1-table, PK) 4. How to join these tables Joining condition — orderTbl.EmpNo =employee.EmpNo Joining styles – in FROM, orderTbl o join employee e on o.EmpNo = e.EmpNo or -- in FROM, orderTbl o, employee e and in WHERE, o.EmpNo = e.EmpNo -- in FROM, orderTbl o natural join employee e or 5. Group result or individual result An aggregate function COUNT() is used to calculate the number of orders taken by each employee group result, Group By is needed, the Grouping By column are EmpNo, EmpLastName. Note the column EmpLastName is redundant but required by SQL engine, and must be specified in Group By clasue as it appears in Select clause and is not an aggregate column. All groups are considered, no Having clause 6. Using the template: SELECT <list of columns or expressions > FROM <list of tables or join operations> WHERE <row selection condition > GROUP BY < list of grouping columns > ©H.Y Lu 2008