在SQL Server 2005中按单个查询的总数和详细信息获取每月记录

时间:2012-10-18 09:56:49

标签: sql-server-2005 subquery

我正在使用SQL Server 2005。

我想在单个结果中从monthlyTotalAppoinment表中提取monthlyEmployeewiseTotalappointment

预约表

appoinmentId
appoinmentDate
employeeId

我可以成功获取monthlyTotalAppoinment并从后续查询获得employeewisetotaappoinment,但我想要每月employeewiseappoinment

SELECT * 
  FROM (SELECT Datename(M, Dateadd(M, NUMBER - 1, 0)) AS month 
          FROM MASTER..SPT_VALUES 
         WHERE TYPE = 'p' 
               AND NUMBER BETWEEN 1 AND 12) months 
       LEFT JOIN (SELECT Datename(MM, APPOINMENTDATE) month, 
                         Count(APPOINMENTID)          AS TotalAppointment 
                    FROM APPOINTMENT 
                   GROUP BY Datename(MM, APPOINMENTDATE)) appoinment 
              ON months.MONTH = appoinment.MONTH 

我得到了以下输出。

enter image description here

但我想要关注输出

enter image description here

   appoinementId   employeeId   appoinemntDate  
   -------------  -----------   ---------------
    1                 4         8/25/2012 12:00:00 AM
    2                 4         8/25/2012 12:00:00 AM
    3                 4         8/25/2012 12:00:00 AM
    4                 4         8/25/2012 12:00:00 AM
    5                 4         8/25/2012 12:00:00 AM
    6                 4         9/25/2012 12:00:00 AM
    7                 2         9/25/2012 12:00:00 AM
    8                 2         9/25/2012 12:00:00 AM
    9                 2         9/25/2012 12:00:00 AM
    10                4         9/25/2012 12:00:00 AM
    11                4         10/25/2012 12:00:00 AM
    12                2         10/25/2012 12:00:00 AM
    13                4         10/25/2012 12:00:00 AM

用于上述数据的输出(For EmployeeId 4)

   Month           MonthData   Totalappoinemnt TotalEmployeewiseAppointmemt  
   -------------  -----------   -------------- ------------------------------
     January..      NULL..       NULL..          NULL..
     Augest         Augest         5             9
     September      September      5             9
     October        October        3             9      

但我想要关注

     Month           MonthData   Totalappoinemnt TotalEmployeewiseAppointmemt  
   -------------  -----------   -------------- ------------------------------
    January..      NULL..       NULL..          NULL..
     Augest         Augest         5             5
     September      September      5             2
     October        October        3             2       

1 个答案:

答案 0 :(得分:0)

我在你的问题中遗漏了一些小问题,但是在这个问题中处理了大问题:

SELECT t1.*, 
       t2.EMP_COUNT 
  FROM (SELECT Datename(MONTH, APPOINEMNTDATE) Month_Name, 
               Count(*)                        app_count 
          FROM APPOINTMENTTABLE 
         GROUP BY Datename(MONTH, APPOINEMNTDATE))T1 
       LEFT JOIN (SELECT Count(*)                        emp_count, 
                         Datename(MONTH, APPOINEMNTDATE) Month_Name 
                    FROM APPOINTMENTTABLE 
                   WHERE EMPLOYEEID = 4 
                   GROUP BY Datename(MONTH, APPOINEMNTDATE))T2 
              ON t1.MONTH_NAME = t2.MONTH_NAME 

可以找到一个工作示例here

缺少什么?

  1. 无法弄清楚为什么你有几个月的2列。如果有这个原因,我会修改代码。
  2. 我只列出了几个月的详情。我看到1月也是这个例子。如果您希望所有12个月都显示即使没有可用的数据,请告知我们并将其添加。
  3. 未使用完全相同的列名称。如果你需要,我相信你可以改变它们: - )