千家信息网

(一)、根据teacher_no统计星期的上课数

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,-- 解法一SELECTt1.teacher_no,(CASE WEEK WHEN '1' THEN keshu ELSE 0 END ) 礼拜一,(CASE WEEK WHEN '2' THEN k
千家信息网最后更新 2025年01月23日(一)、根据teacher_no统计星期的上课数




  1. -- 解法一
  2. SELECT
  3. t1.teacher_no,
  4. (CASE WEEK WHEN '1' THEN keshu ELSE 0 END ) 礼拜一,
  5. (CASE WEEK WHEN '2' THEN keshu ELSE 0 END ) 礼拜二,
  6. (CASE WEEK WHEN '3' THEN keshu ELSE 0 END ) 礼拜三
  7. FROM
  8. (
  9. SELECT
  10. teacher_no,
  11. WEEK,
  12. COUNT(1) AS keshu
  13. FROM teach
  14. GROUP BY teacher_no,WEEK
  15. )t1
  16. GROUP BY t1.teacher_no;


  17. -- 解法二 就是count()函数作用
  18. SELECT
  19. t1.teacher_no,
  20. (CASE WEEK WHEN '1' THEN keshu ELSE 0 END ) 礼拜一,
  21. (CASE WEEK WHEN '2' THEN keshu ELSE 0 END ) 礼拜二,
  22. (CASE WEEK WHEN '3' THEN keshu ELSE 0 END ) 礼拜三
  23. FROM
  24. (
  25. SELECT
  26. t.teacher_no,
  27. t.WEEK,
  28. COUNT(t.keshu) AS keshu
  29. FROM
  30. (
  31. SELECT
  32. teacher_no,
  33. WEEK,
  34. '1' keshu
  35. FROM teach )t
  36. GROUP BY teacher_no,WEEK
  37. )t1
  38. GROUP BY t1.teacher_no
  39. ;
0