【问题标题】:Display result in SQL Server在 SQL Server 中显示结果
【发布时间】:2018-11-12 07:01:06
【问题描述】:

我有以下表格结构。我想要总休假天数,特别是月份和年份。我还提到了我的预期输出。

Table : 1
tblAbsent

Id  Code      LeaveDate Leave   LeaveDay
934 002DSK  2018-08-10  EL      1
934 002DSK  2018-08-11  EL      1
934 002DSK  2018-08-12  EL      1
934 002DSK  2018-09-10  EL      1
934 002DSK  2018-10-12  EL      1
934 002DSK  2018-10-13  EL      1
934 002DSK  2018-10-14  EL      1
934 002DSK  2018-10-15  EL      1

Table : 2 tblEmpdetails

Id          Code    tblAbsentID Month   Year
1172        002DSK  934         4       2018
1259        002DSK  934         5       2018
1335        002DSK  934         6       2018
1411        002DSK  934         7       2018
1487        002DSK  934         8       2018
1563        002DSK  934         9       2018
1639        002DSK  934        10       2018
1715        002DSK  934        11       2018
1791        002DSK  934        12       2018
Table : 3 LeaveOpening

Id      tblAbsentId  Leave  LeaveOpen
80      934          EL      24

我尝试过的结果是:

SELECT DISTINCT
    et.EmployeeId
   ,Month AS [Monthnumber]
   ,DATENAME(mm, DATEADD(mm, Month, -1)) + ' - ' + CONVERT(NVARCHAR(20), Year, 103) AS [Calendar Year Of Service]
   ,CONCAT('1-', et.MD) AS [Wages during From...To...]
   ,et.PD AS [No. Of Days Work Performed], 
   --sum(lb.HFDay) as EL,
   lo.LeaveOpening

   ,(SELECT
            SUM(lb.HFDay)
        FROM dbo.tblAbsent lb
        WHERE lb.EmployeeId = 934 and lb.Leave='EL'  --and lb.LeaveDate >='2018-08-01' and lb.LeaveDate <= '2018-08-30'
        )
    AS EL
    ,(SELECT
            SUM(lb.HFDay)
        FROM dbo.tblAbsent lb
        WHERE lb.EmployeeId = 934 and lb.Leave='ML'
        ) as MaternityLeave
   ,et.PD [Total Of Cols 4 To 7]
   ,ed.FName [Employee Name]
   ,ed.FatherName [Father/Spouses Name]
   ,CONVERT(VARCHAR(15), ed.DateOfJoining, 103) [Date Of Joining Service]

FROM dbo.tblEmpdetails et
INNER JOIN dbo.EmployeeDetail ed
    ON et.EmployeeId = ed.Id
INNER JOIN dbo.LeaveOpening lo
ON lo.EmployeeId = et.EmployeeId
inner join dbo.tblAbsent lb on 
lb.EmployeeId = et.EmployeeId
WHERE et.EmployeeId = 934
AND et.CompanyId = 1
AND Category IN (1, 2, 3, 4)
AND et.ProcessDate >= '2018-01-01'
AND et.ProcessDate <= '2018-12-31' and lb.LeaveDate >='2018-08-01' and lb.LeaveDate <= '2018-08-30'
GROUP BY et.EmployeeId
        ,Month
        ,Year
        ,MD
        ,Gross
        ,PD
        ,ed.FName
        ,ed.FatherName
        ,ed.DateOfJoining
        ,LeaveOpen

我无法根据表 2 按月分组。我希望实际输出是 Sum(LeaveDay) 作为 tblAbsent 表中特定月份的 EL。

Expected Output:

tblAbsentId Monthnumber Calendar Year Of Service    LeaveOpen   EL
934           4             April - 2018                 24      0
934           5             May - 2018                   24      0
934           6             June - 2018                  24      0
934           7             July - 2018                  24      0
934           8             August - 2018                24      3
934           9             September - 2018             24      1
934          10             October - 2018               24      4
934          11             November - 2018              24      0
934          12             December - 2018              24      0

【问题讨论】:

    标签: sql .net sql-server sql-server-2008


    【解决方案1】:

    我已经创建了您在上述问题中显示的表格,并且还在表格中插入了相同的数据。我能够生成与以下查询相同的输出:

    SELECT TE.tblAbsentId, TE.Month, TE.Year, LO.LeaveOpen,
        SUM(ISNULL(TA.LeaveDay, 0)) TotalLeave
    FROM tblEmpdetails TE
    INNER JOIN LeaveOpening LO ON TE.tblAbsentId = LO.tblAbsentId
    LEFT JOIN tblAbsent TA ON TE.tblAbsentId = TA.Id AND TE.Month = 
    MONTH(TA.LeaveDate) AND TE.Year = YEAR(TA.LeaveDate)
    GROUP BY TE.tblAbsentId, TE.Month, TE.Year, LO.LeaveOpen
    

    上述查询的输出将如下所示:

    【讨论】:

      【解决方案2】:

      您可以使用如下查询

      See live demo

      select T.tblAbsentId,
      MonthNumber=T.month,
      [Calendar year of service]=DATENAME(mm, DATEADD(mm, T.Month, -1)) + ' - ' + CONVERT(NVARCHAR(20), Year, 103),
      Leaveopen,
      EL= COALESCE(s,0)
      from tblEmpdetails T
      left join 
      (
          select 
              id, 
              m=month(leavedate),
              leave, 
              s=sum(leaveday) 
          from tblAbsent 
          group by id, month(leavedate),leave
       ) A
       on T.tblAbsentId=A.id and T.Month=A.m
       left join
       LeaveOpening L 
           on L.tblAbsentid=T.tblAbsentId
      

      【讨论】:

      • @RamkrishnaJadhav 如果我的回复对您有用,请投票/接受我的回复作为正确答案,以便其他用户可以受益:知道答案有效并将问题标记为已回答。 [stackoverflow.com/help/someone-answers]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多