获取SQL Server中所有计数的总COUNT

时间:2015-04-29 11:01:57

标签: sql-server stored-procedures

这里我在SQL Server中存储过程,其中它获取每个实体的摘要计数。但后来我想得到它的总体TOTAL,以便我可以从我的桌子上调用它。

继承我的存储过程。

    USE [canteen]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_getTotalByCostCenter] 
    -- Add the parameters for the stored procedure here
    @datefrom datetime, 
    @dateto datetime,
    @entity nvarchar(50),
    @canteen int

AS
IF @entity = 'Aquino'
BEGIN
SELECT 'Aquino' as Plant, d.dept_code, d.dept_name, count(c.id) as Total
    FROM all_chrg c
    LEFT OUTER JOIN emp_mstr e
    ON c.emp_id = e.id
    LEFT OUTER JOIN department d
    ON e.emp_dept = d.id
    LEFT OUTER JOIN entity et
    ON e.emp_entity = et.id
    WHERE et.entity_code IN ('1425')
    AND c.pdate >= @datefrom AND c.pdate <= @dateto
    AND c.plant_tag = @canteen
    GROUP BY d.dept_code,d.dept_name
    ORDER BY d.dept_code
END
ELSE
IF @entity = 'Central Office'
BEGIN
SELECT 'Central Office' as Plant, d.dept_code, d.dept_name, count(c.id) as Total
    FROM all_chrg c
    LEFT OUTER JOIN emp_mstr e
    ON c.emp_id = e.id
    LEFT OUTER JOIN department d
    ON e.emp_dept = d.id
    LEFT OUTER JOIN entity et
    ON e.emp_entity = et.id
    WHERE et.entity_code IN ('1410')
    AND c.pdate >= @datefrom AND c.pdate <= @dateto
    AND c.plant_tag = @canteen
    GROUP BY d.dept_code,d.dept_name
    ORDER BY d.dept_code
END
ELSE
IF @entity = 'Gabriela'
BEGIN
SELECT 'Gabriela' as Plant, d.dept_code, d.dept_name, count(c.id) as Total
    FROM all_chrg c
    LEFT OUTER JOIN emp_mstr e
    ON c.emp_id = e.id
    LEFT OUTER JOIN department d
    ON e.emp_dept = d.id
    LEFT OUTER JOIN entity et
    ON e.emp_entity = et.id
    WHERE et.entity_code IN ('1410','1420','1425','1435','1407')
    AND c.pdate >= @datefrom AND c.pdate <= @dateto
    AND c.plant_tag = @canteen
    GROUP BY d.dept_code,d.dept_name
    ORDER BY d.dept_code
END
ELSE
IF @entity = 'PETC'
BEGIN
SELECT 'PETC' as Plant, d.dept_code, d.dept_name, count(c.id) as Total
    FROM all_chrg c
    LEFT OUTER JOIN emp_mstr e
    ON c.emp_id = e.id
    LEFT OUTER JOIN department d
    ON e.emp_dept = d.id
    LEFT OUTER JOIN entity et
    ON e.emp_entity = et.id
    WHERE et.entity_code IN ('1430')
    AND c.pdate >= @datefrom AND c.pdate <= @dateto
    AND c.plant_tag = @canteen
    GROUP BY d.dept_code,d.dept_name
    ORDER BY d.dept_code
END
ELSE 
IF @entity = 'Lapu-lapu'
BEGIN
SELECT 'Lapu-lapu' as Plant, d.dept_code, d.dept_name, count(c.id) as Total
    FROM all_chrg c
    LEFT OUTER JOIN emp_mstr e
    ON c.emp_id = e.id
    LEFT OUTER JOIN department d
    ON e.emp_dept = d.id
    LEFT OUTER JOIN entity et
    ON e.emp_entity = et.id
    WHERE et.entity_code IN ('1415')
    AND c.pdate >= @datefrom AND c.pdate <= @dateto
    AND c.plant_tag = @canteen
    GROUP BY d.dept_code,d.dept_name
    ORDER BY d.dept_code
END
ELSE
BEGIN
SELECT CASE WHEN et.entity_code = '1410' THEN
            'Gabriela'
            WHEN et.entity_code = '1420' THEN
            'Gabriela'
            WHEN et.entity_code = '1425' THEN
            'Gabriela'
            WHEN et.entity_code = '1435' THEN
            'Gabriela'
            WHEN et.entity_code = '1407' THEN
            'Gabriela'
            WHEN et.entity_code = '1415' THEN
            'Lapu-lapu'
            WHEN et.entity_code = '1430' THEN
            'PETC'
            ELSE
            'Unknown'
            END as Plant,
    d.dept_code, d.dept_name, count(c.id) as Total
    FROM all_chrg c
    LEFT OUTER JOIN emp_mstr e
    ON c.emp_id = e.id
    LEFT OUTER JOIN department d
    ON e.emp_dept = d.id
    LEFT OUTER JOIN entity et
    ON e.emp_entity = et.id
    WHERE c.pdate >= @datefrom AND c.pdate <= @dateto
    AND c.plant_tag = @canteen
    GROUP BY CASE WHEN et.entity_code = '1410' THEN
            'Gabriela'
            WHEN et.entity_code = '1420' THEN
            'Gabriela'
            WHEN et.entity_code = '1425' THEN
            'Gabriela'
            WHEN et.entity_code = '1435' THEN
            'Gabriela'
            WHEN et.entity_code = '1407' THEN
            'Gabriela'
            WHEN et.entity_code = '1415' THEN
            'Lapu-lapu'
            WHEN et.entity_code = '1430' THEN
            'PETC'
            ELSE
            'Unknown'
            END,
            d.dept_code,d.dept_name
    ORDER BY Plant,d.dept_code
END

0 个答案:

没有答案