2015-12-05 47 views
1

我試着表現出按季度劃分如下信息:的Oracle 11g SQL組按季度

  • 一些項目開始在季度
  • 在本季度
  • 數這些項目工作的員工數量每個項目的平均花費時間。

繼承人所有香港專業教育學院想出這麼遠:

SELECT CASE 
      WHEN Start_Date >= '01-JAN-14' AND Start_Date <= '31-MAR-14' THEN 'Q1' 
      WHEN Start_Date >= '01-APR-14' AND Start_Date <= '30-JUN-14' THEN 'Q2' 
      WHEN Start_Date >= '01-JUL-14' AND Start_Date <= '30-SEP-14' THEN 'Q3' 
      WHEN Start_Date >= '01-OCT-14' AND Start_Date <= '31-DEC-14' THEN 'Q4' 
     END AS "Quarter", 
     COUNT(Proj_Number) Num, SUM(COUNT(Emp_Num)),AVG(Hours_Used) 
FROM  Project JOIN Assignment USING (Proj_Number) JOIN Employee USING (Emp_Num) 
WHERE Start_Date >= '01-JAN-14' AND Start_Date <= '31-DEC-14' 
GROUP BY Start_Date,Emp_Num; 

回答

1

你應該四分之一的項目和empls這些項目的工作人員數量進行分組:

SELECT CASE 
     WHEN Start_Date >= '01-JAN-14' AND Start_Date <= '31-MAR-14' THEN 'Q1' 
     WHEN Start_Date >= '01-APR-14' AND Start_Date <= '30-JUN-14' THEN 'Q2' 
     WHEN Start_Date >= '01-JUL-14' AND Start_Date <= '30-SEP-14' THEN 'Q3' 
     WHEN Start_Date >= '01-OCT-14' AND Start_Date <= '31-DEC-14' THEN 'Q4' 
    END AS "Quarter", 
    COUNT(Proj_Number) Num, COUNT(Emp_Num) emp_count 
FROM  Project JOIN Assignment USING (Proj_Number) JOIN Employee USING (Emp_Num) 
WHERE Start_Date >= '01-JAN-14' AND Start_Date <= '31-DEC-14' 
GROUP BY "Quarter"; 

,你應該對每個項目花費的時間有不同的詢問

SELECT CASE 
      WHEN Start_Date >= '01-JAN-14' AND Start_Date <= '31-MAR-14' THEN 'Q1' 
      WHEN Start_Date >= '01-APR-14' AND Start_Date <= '30-JUN-14' THEN 'Q2' 
      WHEN Start_Date >= '01-JUL-14' AND Start_Date <= '30-SEP-14' THEN 'Q3' 
      WHEN Start_Date >= '01-OCT-14' AND Start_Date <= '31-DEC-14' THEN 'Q4' 
     END AS "Quarter", 
     Proj_Number ,AVG(Hours_Used) hours_spent 
FROM  Project JOIN Assignment USING (Proj_Number) JOIN Employee USING (Emp_Num) 
WHERE Start_Date >= '01-JAN-14' AND Start_Date <= '31-DEC-14' 
GROUP BY "Quarter" ,Proj_Number; 

你可能想加入他們看到所有的信息在一個查詢

SELECT quarter.Quarter,quarter.Num as proj_count, quarter.emp_count, 
       proj.Proj_Number, proj.hours_spent 
      FROM (SELECT CASE 
      WHEN Start_Date >= '01-JAN-14' AND Start_Date <= '31-MAR-14' THEN 'Q1' 
      WHEN Start_Date >= '01-APR-14' AND Start_Date <= '30-JUN-14' THEN 'Q2' 
      WHEN Start_Date >= '01-JUL-14' AND Start_Date <= '30-SEP-14' THEN 'Q3' 
      WHEN Start_Date >= '01-OCT-14' AND Start_Date <= '31-DEC-14' THEN 'Q4' 
     END AS "Quarter", 
     COUNT(Proj_Number) Num, COUNT(Emp_Num) emp_count 
    FROM  Project JOIN Assignment USING (Proj_Number) JOIN Employee USING (Emp_Num) 
    WHERE Start_Date >= '01-JAN-14' AND Start_Date <= '31-DEC-14' 
    GROUP BY "Quarter") quarter, 
    (SELECT CASE 
       WHEN Start_Date >= '01-JAN-14' AND Start_Date <= '31-MAR-14' THEN 'Q1' 
       WHEN Start_Date >= '01-APR-14' AND Start_Date <= '30-JUN-14' THEN 'Q2' 
       WHEN Start_Date >= '01-JUL-14' AND Start_Date <= '30-SEP-14' THEN 'Q3' 
       WHEN Start_Date >= '01-OCT-14' AND Start_Date <= '31-DEC-14' THEN 'Q4' 
      END AS "Quarter", 
      Proj_Number ,AVG(Hours_Used) hours_spent 
    FROM  Project JOIN Assignment USING (Proj_Number) JOIN Employee USING (Emp_Num) 
    WHERE Start_Date >= '01-JAN-14' AND Start_Date <= '31-DEC-14' 
    GROUP BY "Quarter" ,Proj_Number) proj 
      WHERE quarter.Quarter = proj.Quarter 
1

我認爲這是非常容易使用to_char(xxx, 'Q')得到季:

SELECT TO_CHAR(Start_Date, 'Q') AS "Quarter", 
     COUNT(Proj_Number) as Num, COUNT(Emp_Num), AVG(Hours_Used) 
FROM Project JOIN 
    Assignment 
    USING (Proj_Number) JOIN 
    Employee 
    USING (Emp_Num) 
WHERE Start_Date >= DATE '2014-01-01' AND Start_Date <= DATE '2014-12-31' 
GROUP BY TO_CHAR(Start_Date, 'Q');