Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Hitung total hari kerja bisnis antara dua tanggal

Jika Anda hanya ingin mengecualikan akhir pekan, Anda cukup mengecualikannya menggunakan hitungan bersyarat dengan menambahkan:

count(distinct case when datepart(weekday, getdate()) <= 5 then date end)

Jadi kueri Anda menjadi:

set datefirst 1;

select  count(distinct(dateadd(d, 0, datediff(d, 0,checktime)))) as workingdays,
        count(distinct case when datepart(weekday, getdate()) <= 5 
                            then dateadd(d, 0, datediff(d, 0,checktime)) 
                        end) as weekdays
from departments,
 dbo.USERINFO INNER JOIN dbo.CHECKINOUT ON 
     dbo.USERINFO.USERID = dbo.CHECKINOUT.USERID
where  userinfo.name='Gokul Gopalakrishnan' and deptname='GEN/SUP-TBL' 
and checktime>='2014-05-01' and checktime<='2014-05-30'

NAMUN Saya sangat merekomendasikan menambahkan tabel kalender ke basis data Anda. Itu membuat semuanya begitu mudah, kueri Anda akan menjadi:

SELECT  DaysWorked = COUNT(cio.Date),
        WeekDaysWorked = COUNT(CASE WHEN c.IsWeekDay = 1 THEN cio.Date END),
        WorkingDaysWorked = COUNT(CASE WHEN c.IsWorkingDay = 1 THEN cio.Date END),
        TotalDays = COUNT(*),
        TotalWeekDays = COUNT(CASE WHEN c.IsWeekDay = 1 THEN 1 END),
        TotalWorkingDays = COUNT(CASE WHEN c.IsWorkingDay = 1 THEN 1 END)
FROM    dbo.Calender AS c
        LEFT JOIN
        (   SELECT  DISTINCT
                    Date = CAST(CheckTime AS DATE)
            FROM    dbo.Departments AS d
                    CROSS JOIN dbo.userInfo AS ui
                    INNER JOIN dbo.CheckInOut AS cio
                        ON cio.UserID = ui.UserID
            WHERE   ui.Name = 'Gokul Gopalakrishnan' 
            AND     d.deptname = 'GEN/SUP-TBL' 
        ) AS cio
            ON c.Date = cio.Date
WHERE   d.Date >= '2014-05-01'
AND     d.Date <= '2014-05-30';

Dengan cara ini Anda dapat menentukan hari libur, akhir pekan, dll. Ini jauh lebih fleksibel daripada solusi lainnya.

EDIT

Saya pikir saya salah memahami kriteria asli Anda. Ini akan bekerja untuk Anda tanpa tabel kalender:

SET DATEFIRST 1;

DECLARE @StartDate DATE = '2014-05-01', 
        @EndDate DATE = '2014-05-30';

DECLARE @Workdays INT = 
    (DATEDIFF(DAY, @StartDate, @EndDate) + 1)
    -(DATEDIFF(WEEK, @StartDate, @EndDate) * 2)
    -(CASE WHEN DATEPART(WEEKDAY, @StartDate) = 7 THEN 1 ELSE 0 END)
    -(CASE WHEN DATEPART(WEEKDAY, @EndDate) = 6 THEN 1 ELSE 0 END);


SELECT  WorkingDays = COUNT(DISTINCT CAST(CheckTime AS DATE)),
        BusinessDays = @Workdays
FROM    dbo.Departments AS d
        CROSS JOIN dbo.userInfo AS ui
        INNER JOIN dbo.CheckInOut AS cio
            ON cio.UserID = ui.UserID
WHERE   ui.Name = 'Gokul Gopalakrishnan' 
AND     d.deptname = 'GEN/SUP-TBL' 
AND     cio.CheckTime >= @StartDate
AND     cio.CheckTime <= @EndDate;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Visual Studio:ContextSwitchDeadlock

  2. Buat pesan kesalahan khusus dalam batasan centang di SQL SERVER 2008

  3. Saya perlu mengimpor data dari Excel ke SQL Server menggunakan VBA

  4. Mengatur korban kebuntuan

  5. Bagaimana cara membagi string menjadi kolom untuk tampilan?