Tuesday, October 27, 2015

Get top 2 rows per group by

Given a Commissions table with a Commission (decimal) and an employeeID primary key columns,  list the top 2 commissions of each employee:

Best is to use a CTE with either the ROW_NUMBER() or RANK() partitions:

 ;With CTE_MyRank as (
SELECT [Commission]
      ,[EmployeeId]
      ,[CommissionID]
 , ROW_NUMBER() over (partition by employeeID order by commission desc) as Rankx
  FROM [Training].[dbo].[Commissions] 
  )

  select  EmployeeID, commission, Rankx from CTE_MyRank where rankx < 3 order  by [EmployeeId] desc, commission desc



OR the less favorite way is to use MAX , Group By and a UNION:

  Select  Max([Commission]) xMax, [EmployeeId] from [Training].[dbo].[Commissions]
   Group by [EmployeeId] 
  UNION
 Select Max(Commission) xMax, [EmployeeId] from [Training].[dbo].[Commissions] 
 where (Commission) not in (select Max([Commission]) from [Training].[dbo].[Commissions] 
 Group by [EmployeeId] )

Group by [EmployeeId] 
   order by [EmployeeId] desc, xMAx desc

Monday, October 19, 2015

Get the values of all fields/properties of a C# object using reflection

using System.Reflection;     


 private List GetPropertyList(object obj)
        {
            List propertyList = new List();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance |
                                                            BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }
            return propertyList;
        }

Link to one of the best bootstrap validators

http://1000hz.github.io/bootstrap-validator/

Also angular validations:

https://scotch.io/tutorials/angularjs-form-validation-with-ngmessages