Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
declare @Month int='2';
declare @Year nvarchar(500)='2014';

select distinct(Emp_Status)as EMPSTATUS,
       sum(case when Att_Status in ('AB') then 1 else (case when( Att_Status like '%/AB%') then 0.5 else case when( Att_Status like '%AB/%') then 0.5 else 0 end end) end) as ABSENT_DAYS ,
        DATEDIFF(DAY,
            DATEADD(DAY, 0, DATEADD(month, ((@Year - 1900) * 12) + @Month - 1, 0)),
            DATEADD(DAY, 0, DATEADD(month, ((@Year - 1900) * 12) + @Month, 0))
       ) AS [ACTUAL DAYS]
       from Attendence 
       inner join 
        EmployeeMaster  on fk_Att_EmpCode=pk_Emp_Code
where year(Att_Date)=@Year and Month (Att_Date)=@Month
 group by Emp_Status</blockquote>

and output format of that above query is
EMPSTATUS	ABSENTDAYS	ACTUALDAYS
Direct Contract	5.0	28
Permanent	48.5	28

but i want out put total of ABSENTDAYS as total
and I dnt Want Total of ACTUALDAYS

means bellow Format
EMPSTATUS	ABSENTDAYS	ACTUALDAYS
Direct Contract	5.0	28
Permanent	48.5	28
total	53.5	28
Posted
v2

First of all, such formatting and presentation kind of stuff is not a matter of a query. A query gives you the data, the rest is up to a presentation layer (whatever kind of). So it is a bad design decision to take this path. If this is meant to be a serious BI application, you will encounter maintainability issues during it's life-cycle.

Wither way, if you stick to this approach, will have to add an UNION at the end which will need to have the same underlying query, but without a GROUP BY.

I have not tried to recreate your data, so check this explanatory example:
SQL
create table data(D char(1), A int);
insert into data values('A', 10);
insert into data values('A', 20);
insert into data values('B', 30);
insert into data values('B', 40);

select D, sum(A) from data group by D union select 'T', sum(A) from data;

That will look ugly in your case.
But you can use a view or CTE[^] to make it a little bit nicer:

SQL
with myCTE (myD, myA)
as (
  select D as myD, A as myA from data
)
select myD, sum(myA) from myCTE group by myD union select 'T', sum(myA) from myCTE;

And you can put all your calculation inside the AS(...) part, so you don't have to repeat it after the UNION clause.
 
Share this answer
 
Hi

Use the OVER clause to modify a SUM scope in your query. No GROUP BY needed


SQL
SELECT
    MyColumn, OtherColumn,
    SUM(MyColumn) OVER () AS SumTotal
FROM
 
Share this answer
 
may be this will help you... try this

select EMPSTATUS,ABSENTDAYS,ACTUALDAYS from YourTable
union
select 'Total ' as EMPSTATUS,(select sum(ABSENTDAYS) from YourTable) as ABSENTDAYS,(select distinct ACTUALDAYS from YourTable) as 'ACTUALDAYS'
from YourTable
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900