0

我正在使用 SQL Server 2005。

我想在单个结果中获取monthlyTotalAppoinmentmonthlyEmployeewiseTotal从表中获取。appointment

预约表

appoinmentId
appoinmentDate
employeeId

我可以成功获取monthlyTotalAppoinmentemployeewisetotaappoinment从以下查询中获取,但我想要每月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 

我得到以下输出。

在此处输入图像描述

但我想要以下输出

在此处输入图像描述

   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

对于上述数据cuming输出(对于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       
4

1 回答 1

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 

可以在此处找到一个工作示例。

什么不见​​了?

  1. 无法弄清楚为什么几个月有 2 列。如果有这个原因,我会修改代码。
  2. 我只列出了提供详细信息的月份。我看到一月也在这个例子中。如果您希望即使没有可用数据也能显示所有 12 个月,请告诉我,我们会添加它。
  3. 没有使用完全相同的列名。如果需要,我相信您可以更改它们:-)
于 2012-10-18T10:34:21.737 回答