Wednesday, 4 November 2015

Features of C# 6.0 

There are following features.

1. Use of Static Members with Namespace
2. Auto Property Initialize


1. Use of Static Members with Namespace


We can use static classes in namespaces.

Example: 

a) Use of static class of system namespace.


using static System.Console;
using static System.Convert;

namespace CSharp6._0NewFeatures
{
    class Program
    {
        static void UsingStaticMember()
        {
            WriteLine("Use of System.Console\n\n");

            string age = "10";
            WriteLine("System.Convert: " + ToInt32(age));
        }

        static void Main(string[] args)
        {
            UsingStaticMember();
            ReadKey();
        }
    }
}

Output:
using static System.ClassName

Thursday, 22 October 2015

Dynamic Pivot, Passing A Table

You can create dynamic pivot, passing a table. As per following step.

1. Create a table.

Create Table Employee
(
EmpId int Identity(1,1)Primary Key,
EmpName varchar(500),
Salary decimal(18,2),
Createdon datetime default getdate()
)

2. Insert records.

a) Current month  to current date
insert into Employee values('Arun',10000,GETDATE())
insert into Employee values('Barun',20000, GETDATE())
insert into Employee values('Tarun',30000, GETDATE())
insert into Employee values('Ajay',40000, GETDATE())
insert into Employee values('Vijay',50000, GETDATE())
insert into Employee values('Anil',60000, GETDATE())

b) before 1 month to current date
insert into Employee values('Arun',15000,DATEADD(mm,-1, GETDATE()))
insert into Employee values('Barun',25000, DATEADD(mm,-1, GETDATE()))
insert into Employee values('Tarun',35000, DATEADD(mm,-1, GETDATE()))
insert into Employee values('Ajay',45000, DATEADD(mm,-1, GETDATE()))
insert into Employee values('Vijay',55000, DATEADD(mm,-1, GETDATE()))
insert into Employee values('Anil',65000, DATEADD(mm,-1, GETDATE()))

c) before 2 month to current date
insert into Employee values('Arun',20000,DATEADD(mm,-2, GETDATE()))
insert into Employee values('Barun',30000, DATEADD(mm,-2, GETDATE()))
insert into Employee values('Tarun',40000, DATEADD(mm,-2, GETDATE()))
insert into Employee values('Ajay',50000, DATEADD(mm,-2, GETDATE()))
insert into Employee values('Vijay',60000, DATEADD(mm,-2, GETDATE()))
insert into Employee values('Anil',70000, DATEADD(mm,-2, GETDATE()))

d) before 3 month to current date
insert into Employee values('Arun',20000,DATEADD(mm,-3, GETDATE()))
insert into Employee values('Barun',30000, DATEADD(mm,-3, GETDATE()))
insert into Employee values('Tarun',40000, DATEADD(mm,-3, GETDATE()))
insert into Employee values('Ajay',50000, DATEADD(mm,-3, GETDATE()))
insert into Employee values('Vijay',60000, DATEADD(mm,-3, GETDATE()))
insert into Employee values('Anil',70000, DATEADD(mm,-3, GETDATE()))

3. You can create a table  to current date


Declare @Columns varchar(max)
Declare @SQL varchar(max)

select @Columns = coalesce(@Columns+',','')+ QUOTENAME(Mth) from
(
select distinct  MONTH(CreatedOn)Mth from Employee
) as tblColumns
print @columns
set @SQL  = '
;with cte as
(
select EmpName,Month(CreatedON)Mth,Salary from Employee
) select EmpName,'+@Columns+' from cte pivot
(
   sum(salary) for Mth in ('+@Columns+')
) as pivotTable'
print @sql

execute(@SQL)

OUTPUT :
EmpName 7 8 9 10
Ajay 50000.00 50000.00 45000.00 40000.00
Anil 70000.00 70000.00 65000.00 60000.00
Ajay 20000.00 20000.00 15000.00 10000.00
Barun 30000.00 30000.00 25000.00 20000.00
Tarun 40000.00 40000.00 35000.00 30000.00
Vijay 60000.00 60000.00 55000.00 50000.00

Now insert again :

e) before 4 month to current date

insert into Employee values('Arun',15000,DATEADD(mm,-4, GETDATE()))
insert into Employee values('Barun',25000, DATEADD(mm,-4, GETDATE()))
insert into Employee values('Tarun',35000, DATEADD(mm,-4, GETDATE()))
insert into Employee values('Ajay',45000, DATEADD(mm,-4, GETDATE()))
insert into Employee values('Vijay',55000, DATEADD(mm,-4, GETDATE()))
insert into Employee values('Anil',65000, DATEADD(mm,-4, GETDATE()))

OUTPUT :


EmpName 6 7 8 9 10
Ajay 45000.00 50000.00 50000.00 45000.00 40000.00
Anil 65000.00 70000.00 70000.00 65000.00 60000.00
Arun 15000.00 20000.00 20000.00 15000.00 10000.00
Barun 25000.00 30000.00 30000.00 25000.00 20000.00
Tarun 35000.00 40000.00 40000.00 35000.00 30000.00
Vijay 55000.00 60000.00 60000.00 55000.00 50000.00
Now, currently 6 month add without any change in query.

So You can able to add dynamic Pivot, passing a table.


Thanks
Arun Kumar Porwal