Monday, November 28, 2011

Brain teaser series 1...

Run for your life...
Mr. Joy enjoys some risk and likes to explore the nearby area. In the beautiful surrounding there is a train track that goes through a mysterious tunnel. One fine morning Mr. Joy decided to take a walk through the tunnel. While Mr. Joy was 1/4 way on the tunnel, he hears the train whistle. A quick thinker Mr. Joy calculates that if he runs back he would just make it out of the tunnel before train gets in the tunnel and if he runs forward he will just make it out of the tunnel before train hits him. While Mr. Joy saved his life, he left you with a problem, how fast was this train travelling compare to Mr. Joy?

This is old school physics problem with all basic assumptions for this kind of puzzle. Assume Mr. Joy can run with same speed in both direction. He can accelerate to his top speed right away. Train barely misses him if he would run in either direction

Looking forward to see some solution(s)

Sunday, November 27, 2011

Business Intelligence


What is business Intelligence
I am sure a lot of people can define business intelligence but what we need to know is how it applies to us our business, our life and things we do. Over the last few decades, we have collected data and we probably have overwhelming data about a person, from financial records to personal email, phone records etc. While this information is useful for your own records it provides a vast scope for someone in the field of offering services or goods a great opportunity to find out what they can launch or produce. 

While this is a short example and a survey could do that but have you ever thought of magic. While magical tricks (are tricks) and some are really sophisticated, business intelligence is the magic for this data. Data is any piece entered into a system but when we interpret it, it becomes information. When this goes beyond a regular interpretation, business intelligence comes into play. 

Have you ever thought about this if the person entering next in the car dealership will buy the car or not? Well, if you are into sales there, you know the other brain tricks (that I will talk about in another topic) but you wish you knew so you could spend your time appropriately. Well based on the vast data that we have, a simple survey may be a tool that could give you the answer with pretty high confidence. For example, how many car this person already own, is he / she a house owner? how many children? etc.

Business intelligence is not about what we can see but what we cannot see. With that said, here is my introduction to BI. 

Would love to get some feedback and comments...

Saturday, July 16, 2011

How to live life?

Live for today and enjoy what you have or plan for Tomorrow?


Overcome your fear. This is one place where Earth is the limit ;)
If you can do this then you can fly through all problems :)


Ride the ocean but don't be foolish. Enjoy, today is the day and time to enjoy

One day I would like to be in the driver seat there. Believe me you would know that perfection is not achieved but is a practice.


I have ridden in the roughs with my bicycle. It was always thrilling to be on the top

Don't need a pilot :)

Monday, December 27, 2010

Add / Subtracting days from a date to get the next date without counting weekend

Recently had a situation where I had to calculate the next date based on the duration provided to the function. The catch was not to count the weekend (Saturday and Sunday) in the calculation.

I did a lot of google search and landed with  a way to get the next date if the duration was positive (that is the next date will be later than the provided date). I needed a way to go back as well.

After a lot of learning, I came up with this T SQL function for SQL Server that will allow you pass a positive or negative duration parameter and a date. Based on the parameter, it will add or subtract numbers.

Here is the T SQL function. Have fun and enjoy. Feel free to use this, I would really appreciate acknowledgement and link to my blog (http://dmknol.blogspot.com/)

create function [dbo].[GetEndDate_ByWeekdays]
(
 @pdtStartDate datetime,
 @piDuration int
)
returns datetime
as
begin
 declare @rdt datetime
 -- calculating forward
 if (@piDuration > 0)
 begin
   select @rdt = dateadd (d, case datepart (dw, @pdtStartDate) when 7 then 2 when 1 then 1 else 0 end, @pdtStartDate)
   + (datepart (dw, dateadd (d, case datepart (dw, @pdtStartDate) when 7 then 2 when 1 then 1 else 0 end,
    @pdtStartDate)) - 2 + @piDuration) % 5
   + ((datepart (dw, dateadd (d, case datepart (dw, @pdtStartDate) when 7 then 2 when 1 then 1 else 0 end,
    @pdtStartDate)) - 2 + @piDuration) / 5) * 7
   - (datepart (dw, dateadd (d, case datepart (dw, @pdtStartDate) when 7 then 2 when 1 then 1 else 0 end,
   @pdtStartDate)) - 2)
 end

 -- calculating backward
 else
 begin
  -- local variable
  declare @liDuration int,
    @ldt_orgStart datetime,
    @li_orgDuration int
   
  -- save original value
  set @ldt_orgStart = @pdtStartDate
  set @li_orgDuration = @piDuration
  -- divide the duration into set of fives
  while abs(@piDuration)/5 > 0
   begin
    set @piDuration = @piDuration + 5
    --select [@piduration] = @piDuration
    set @liDuration = 7 - 2*(datepart (dw, @pdtStartDate) /7) + (datepart (dw, @pdtStartDate) -2)/7
    set @liduration = @liDuration *(-1)
    --select [@liduration] = @liDuration
    set @pdtStartDate = dateadd(d, @liDuration, @pdtStartDate)
    --select [@pdtStartDate_inside] = @pdtStartDate
   end
  -- add days that were not covered within the five sets
  if (abs(@li_orgDuration) % 5 > 0)
  begin
   --select @piDuration, @pdtStartDate, datepart (dw, @pdtStartDate)
   set @liDuration = case when datepart (dw, @pdtStartDate) <= abs(@piDuration) + 1 then abs(@piDuration) + 2
         when datepart (dw, @pdtStartDate) = 1 then abs(@piDuration) + 1
         else abs(@piDuration) end
   set @liDuration = @liDuration * (-1)
   --select [@liDuration_last] = @liDuration
   set @rdt = dateadd(d, @liDuration, @pdtStartDate)
  end
  else
   set @rdt = @pdtStartDate
 end

 -- return the date
 return @rdt
end
GO

Saturday, October 30, 2010

TSQL Averages with Rollup

Here is tsql way of getting averages and also rolling up. Below example is using adventureworks database that is available with SQL server 2008. It is getting average pay rate by each department and also overall by the company.

use [AdventureWorks]
select[Department] = case when grouping(d.GroupName) = 1 then 'Company Average'
                              else d.GroupName end,
            [Average Pay Rate] = avg(p.Rate)
from (select [EmployeeID], [Rate] = max([Rate])
            from [HumanResources].[EmployeePayHistory]
            group by [EmployeeID]) p
      inner join [HumanResources].[EmployeeDepartmentHistory] h
                  on h.[EmployeeID] = p.[EmployeeID] and h.[EndDate] is null
      inner join [HumanResources].[Department] d on d.[DepartmentID] = h.[DepartmentID]
group by d.[GroupName]
with rollup
order by 1


Recently one of the buzz word in the technology world was "Lean"

I decided to find it little more about it. A new term has been coined, Lean software development. Well the work and definition below is from various sites and sources on internet. I would like to thank them and not claim this is my work. This is to insitgate discussion and blog the very purpose of having this site.

Lot of you may know about Sixsigma, this is kind of an extension, here is what I found. Looking forward to get some feedback.

Lean software development is a translation of lean manufacturing principles and practices to the software development domain. Adapted from the Toyota Production System, a pro-lean subculture is emerging from within the agile community.
Eliminate waste
  • unnecessary code and functionality
  • delay in the software development process
  • unclear requirements
  • bureaucracy
  • slow internal communication
Amplify learning
Software development is a continuous learning process with the additional challenge of development teams and end product sizes. The best approach for improving a software development environment is to amplify learning. The accumulation of defects should be prevented by running tests as soon as the code is written. Instead of adding more documentation or detailed planning, different ideas could be tried by writing code and building. The process of user requirements gathering could be simplified by presenting screens to the end-users and getting their input.
The learning process is sped up by usage of short iteration cycles – each one coupled with refactoring and integration testing. Increasing feedback via short feedback sessions with Customers helps when determining the current phase of development and adjusting efforts for future improvements. During those short sessions both Customer representatives and the development team learn more about the domain problem and figure out possible solutions for further development
Decide as late as possible
As software development is always associated with some uncertainty, better results should be achieved with an options-based approach, delaying decisions as much as possible until they can be made based on facts and not on uncertain assumptions and predictions. The more complex a system is, the more capacity for change should be built into it, thus enabling the delay of important and crucial commitments.
An agile software development approach can move the building of options earlier for customers, thus delaying certain crucial decisions until Customers have realized their needs better. This also allows later adaptation to changes and the prevention of costly earlier technology-bounded decisions. This does not mean that no planning should be involved – on the contrary, planning activities should be concentrated on the different options and adapting to the current situation, as well as clarifying confusing situations by establishing patterns for rapid action.

Deliver as fast as possible
In the era of rapid technology evolution, it is not the biggest that survives, but the fastest. The sooner the end product is delivered without considerable defect, the sooner feedback can be received, and incorporated into the next iteration. The shorter the iterations, the better the learning and communication within the team. Without speed, decisions cannot be delayed. Speed assures the fulfilling of the Customer's present needs and not what they required yesterday. This gives them the opportunity to delay making up their minds about what they really require until they gain better knowledge. Customers value rapid delivery of a quality product.
The Just-in-Time production ideology could be applied to software development, recognizing its specific requirements and environment. This is achieved by presenting the needed result and letting the team organize itself and divide the tasks for accomplishing the needed result for a specific iteration. At the beginning, the Customer provides the needed input. This could be simply presented in small cards or stories – the developers estimate the time needed for the implementation of each card.
Empower the team
There has been a traditional belief in most businesses about the decision-making in the organization – the managers tell the workers how to do their own job. In a Work-Out technique, the roles are turned – the managers are taught how to listen to the developers, so they can explain better what actions might be taken, as well as provide suggestions for improvements. Most experienced project managers have simply stated the key for a successful project – "Find good people and let them do their own job."
Another mistaken belief has been the consideration of people as resources. People might be resources from the point of view of a statistical data sheet, but in software development, as well as any organizational business, people do need something more than just the list of tasks and the assurance that they will not be disturbed during the completion of the tasks. People need motivation and a higher purpose to work for – purpose within the reachable reality, with the assurance that the team might choose its own commitments. The developers should be given access to Customer; the team leader should provide support and help in difficult situations, as well as make sure that skepticism does not ruin the team’s spirit.
Build integrity in
The Customer needs to have an overall experience of the System – this is the so called perceived integrity: how it is being advertised, delivered, deployed, accessed, how intuitive its use is, price and how well it solves problems.
Conceptual integrity means that the system’s separate components work well together as a whole with balance between flexibility, maintainability, efficiency, and responsiveness. This could be achieved by understanding the problem domain and solving it at the same time, not sequentially. The needed information is received in small batch pieces – not in one vast chunk with preferable face-to-face communication and not any written documentation. The information flow should be constant in both directions – from Customer to developers and back, thus avoiding the large stressful amount of information after long development in isolation.
Automated tests are also considered part of the production process, and therefore if they do not add value they should be considered waste. Automated testing should not be a goal, but rather a means to an end, specifically the reduction of defects.
See the whole
Software systems nowadays are not simply the sum of their parts, but also the product of their interactions. Defects in software tend to accumulate during the development process – by decomposing the big tasks into smaller tasks, and by standardizing different stages of development, the root causes of defects should be found and eliminated. The larger the system, the more organizations that are involved in its development and the more parts are developed by different teams, the greater the importance of having well defined relationships between different vendors, in order to produce a system with smoothly interacting components. During a longer period of development, a stronger sub- contractor network is far more beneficial than short-term profit optimizing, which does not enable win-win relationships.
Lean thinking has to be understood well by all members of a project, before implementing in a concrete, real-life situation. “Think big, act small, fail fast; learn rapidly” – these slogans summarize the importance of understanding the field and the suitability of implementing lean principles along the whole software development process.

SQL server split received string using XML

Several time, we have a need to split the string based on a delimiter, for instance to handle multiple input filter parameters etc.
 
Most of the time we end up using a user defined function that accepts a string and a delimiter and returns a table of split values. Function may be in-efficient at times.
 
Here is an XML way to split the input string and get a result within a table
 
declare @xml as xml,@str as varchar(100),@delimiter as varchar(10)set @str='A,B,C,D,E'set @delimiter =','set @xml = cast(('<X>' + replace(@str, @delimiter, '</X><X>') + '</X>') as xml)select N.value('.', 'varchar(10)') as value from @xml.nodes('X') as T(N)