日期:2014-05-17 浏览次数:20637 次
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[UserStatisticsByDepartmentID]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT Department
, isnull(sum(CASE WHEN Sex IN (0,1) AND Onjob=1 THEN 1 ELSE 0 END),0)AS 'Total'
, isnull(sum(CASE WHEN Sex = 0 AND Onjob=1 THEN 1 ELSE 0 END),0)AS 'Woman'
, isnull(sum(CASE WHEN Sex = 1 AND Onjob=1 THEN 1 ELSE 0 END),0)AS 'Man'
, isnull(sum(CASE WHEN datediff(day,EntryDate,getdate())=0 THEN 1 ELSE 0 END),0)AS 'TodayIn'
, isnull(sum(CASE WHEN datediff(day,Departure,getdate())=0 THEN 1 ELSE 0 END),0)AS 'TodayOut'
, isnull(sum(CASE WHEN datediff(month,EntryDate,getdate())=0 THEN 1 ELSE 0 END),0)AS 'MonthIn'
, isnull(sum(CASE WHEN datediff(month,Departure,getdate())=0 THEN 1 ELSE 0 END),0)AS 'MonthOut'
, isnull(sum(CASE WHEN datediff(year,EntryDate,getdate())=0 THEN 1 ELSE 0 END),0)AS 'YearIn'
, isnull(sum(CASE WHEN datediff(year,Departure,getdate())=0 THEN 1 ELSE 0 END),0)AS 'YearOut'
FROM DB_User
GROUP BY Department
END
exec ('SELECT Department
, isnull(sum(CASE WHEN Sex IN (0,1) AND Onjob=1 THEN 1 ELSE 0 END),0)AS Total
, isnull(sum(CASE WHEN Sex = 0 AND Onjob=1 THEN 1 ELSE 0 END),0)AS Woman
, isnull(sum(CASE WHEN Sex = 1 AND Onjob=1 THEN 1 ELSE 0 END),0)AS Man
, isnull(sum(CASE WHEN datediff(day,EntryDate,getdate())=0 THEN 1 ELSE 0 END),0)AS TodayIn
, isnull(sum(CASE WHEN datediff(day,Departure,getdate())=0 THEN 1 ELSE 0 END),0)AS TodayOut
, isnull(sum(CASE WHEN datediff(month,EntryDate,getdate())=0 THEN 1 ELSE 0 END),0)AS MonthIn
, isnull(sum(CASE WHEN datediff(month,Departure,getdate())=0 THEN 1 ELSE 0 END),0)AS MonthOut
, isnull(sum(CASE WHEN datediff(year,EntryDate,getdate())=0 THEN 1 ELSE 0 END),0)AS