stored procedures - GET total COUNT of all the count in SQL Server -
here have stored procedure in sql server wherein gets summary count of each entity. wanted overall total of can call table.
heres stored procedure.
use [canteen] go set ansi_nulls on go set quoted_identifier on go alter procedure [dbo].[sp_gettotalbycostcenter] -- add parameters stored procedure here @datefrom datetime, @dateto datetime, @entity nvarchar(50), @canteen int if @entity = 'aquino' begin select 'aquino' plant, d.dept_code, d.dept_name, count(c.id) total 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 et.entity_code in ('1425') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'central office' begin select 'central office' plant, d.dept_code, d.dept_name, count(c.id) total 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 et.entity_code in ('1410') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'gabriela' begin select 'gabriela' plant, d.dept_code, d.dept_name, count(c.id) total 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 et.entity_code in ('1410','1420','1425','1435','1407') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'petc' begin select 'petc' plant, d.dept_code, d.dept_name, count(c.id) total 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 et.entity_code in ('1430') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'lapu-lapu' begin select 'lapu-lapu' plant, d.dept_code, d.dept_name, count(c.id) total 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 et.entity_code in ('1415') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else begin select case when et.entity_code = '1410' 'gabriela' when et.entity_code = '1420' 'gabriela' when et.entity_code = '1425' 'gabriela' when et.entity_code = '1435' 'gabriela' when et.entity_code = '1407' 'gabriela' when et.entity_code = '1415' 'lapu-lapu' when et.entity_code = '1430' 'petc' else 'unknown' end plant, d.dept_code, d.dept_name, count(c.id) total 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 c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group case when et.entity_code = '1410' 'gabriela' when et.entity_code = '1420' 'gabriela' when et.entity_code = '1425' 'gabriela' when et.entity_code = '1435' 'gabriela' when et.entity_code = '1407' 'gabriela' when et.entity_code = '1415' 'lapu-lapu' when et.entity_code = '1430' 'petc' else 'unknown' end, d.dept_code,d.dept_name order plant,d.dept_code end
try
use [canteen] go set ansi_nulls on go set quoted_identifier on go alter procedure [dbo].[sp_gettotalbycostcenter] -- add parameters stored procedure here @datefrom datetime, @dateto datetime, @entity nvarchar(50), @canteen int if @entity = 'aquino' begin declare @cnt int set @cnt=0 select 'aquino' plant, d.dept_code, d.dept_name, @cnt=@cnt+count(c.id) total 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 et.entity_code in ('1425') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'central office' begin select 'central office' plant, d.dept_code, d.dept_name, @cnt=@cnt+count(c.id) total 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 et.entity_code in ('1410') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'gabriela' begin select 'gabriela' plant, d.dept_code, d.dept_name, @cnt=@cnt+count(c.id) total 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 et.entity_code in ('1410','1420','1425','1435','1407') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'petc' begin select 'petc' plant, d.dept_code, d.dept_name, @cnt=@cnt+count(c.id) total 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 et.entity_code in ('1430') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else if @entity = 'lapu-lapu' begin select 'lapu-lapu' plant, d.dept_code, d.dept_name, @cnt=@cnt+count(c.id) total 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 et.entity_code in ('1415') , c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group d.dept_code,d.dept_name order d.dept_code end else begin select case when et.entity_code = '1410' 'gabriela' when et.entity_code = '1420' 'gabriela' when et.entity_code = '1425' 'gabriela' when et.entity_code = '1435' 'gabriela' when et.entity_code = '1407' 'gabriela' when et.entity_code = '1415' 'lapu-lapu' when et.entity_code = '1430' 'petc' else 'unknown' end plant, d.dept_code, d.dept_name, @cnt=cnt+count(c.id) total 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 c.pdate >= @datefrom , c.pdate <= @dateto , c.plant_tag = @canteen group case when et.entity_code = '1410' 'gabriela' when et.entity_code = '1420' 'gabriela' when et.entity_code = '1425' 'gabriela' when et.entity_code = '1435' 'gabriela' when et.entity_code = '1407' 'gabriela' when et.entity_code = '1415' 'lapu-lapu' when et.entity_code = '1430' 'petc' else 'unknown' end, d.dept_code,d.dept_name order plant,d.dept_code end
Comments
Post a Comment