Wednesday, July 30, 2008

Two cool functions of SQL

There are two really cool functions in Oracle SQL that are really very helpful when we are trying to do manipulations with date. The two functions are LEAD and LAG. These are called analytical functions in oracle.

The LEAD and LAG functions are used to give access to multiple rows in a table without the need of a self join. By using the LEAD and LAG functions one can fetch the current and previous row or current and next row values in a single row. Lets go by an example

Assuming we have data in the following format

SELECT empno,
ename,
job,
sal
FROM Job
ORDER BY sal;

EMPNO ENAME JOB SAL
---------- ---------- --------- ----------
7369 SMITH CLERK 800
7900 JAMES CLERK 950
7876 ADAMS CLERK 1100
7521 WARD SALESMAN 1250
7654 MARTIN SALESMAN 1250
7934 MILLER CLERK 1300
7844 TURNER SALESMAN 1500
7499 ALLEN SALESMAN 1600
7782 CLARK MANAGER 2450
7698 BLAKE MANAGER 2850
7566 JONES MANAGER 2975
7788 SCOTT ANALYST 3000
7902 FORD ANALYST 3000
7839 KING PRESIDENT 5000

Now using the LAG function we can get the current employees salary as well as the previous employees salary. This is done by using the following query

The general syntax of LAG function is
LAG  (value_expression [,offset] [,default]) OVER ([query_partition_clase] order_by_clause)

SELECT empno,
ename,
job,
sal,
LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev,
FROM job;

EMPNO ENAME JOB SAL SAL_PREV
---------- ---------- --------- ---------- ----------
7369 SMITH CLERK 800 0
7900 JAMES CLERK 950 800
7876 ADAMS CLERK 1100 950
7521 WARD SALESMAN 1250 1100
7654 MARTIN SALESMAN 1250 1250
7934 MILLER CLERK 1300 1250
7844 TURNER SALESMAN 1500 1300
7499 ALLEN SALESMAN 1600 1500
7782 CLARK MANAGER 2450 1600
7698 BLAKE MANAGER 2850 2450
7566 JONES MANAGER 2975 2850
7788 SCOTT ANALYST 3000 2975
7902 FORD ANALYST 3000 3000
7839 KING PRESIDENT 5000 3000

The Sal_prev field will hold the salary of the previous row employee.

Now lets see how LEAD function will work for the same given data. The general syntax of the LEAD function is
LEAD  (value_expression [,offset] [,default]) OVER ([query_partition_clase] order_by_clause)

SELECT empno,
       ename,
job,
sal,
LEAD(sal, 1, 0) OVER (ORDER BY sal) AS sal_next,
FROM job;

EMPNO ENAME JOB SAL SAL_NEXT
---------- ---------- --------- ---------- ----------
7369 SMITH CLERK 800 950
7900 JAMES CLERK 950 1100
7876 ADAMS CLERK 1100 1250
7521 WARD SALESMAN 1250 1250
7654 MARTIN SALESMAN 1250 1300
7934 MILLER CLERK 1300 1500
7844 TURNER SALESMAN 1500 1600
7499 ALLEN SALESMAN 1600 2450
7782 CLARK MANAGER 2450 2850
7698 BLAKE MANAGER 2850 2975
7566 JONES MANAGER 2975 3000
7788 SCOTT ANALYST 3000 3000
7902 FORD ANALYST 3000 5000
7839 KING PRESIDENT 5000 0

The SAL_NEXT field will hold the value of the next row salary.

This feature can be used in numerous occasions, one occasion where we find it to be useful is in peoplesoft where we deal with the effdt, If we want the job_end_dt of a particular person and you know only his job_begin_dt, then using the LEAD and LAG functions we can determine the job_end_dt, by doing LEAD function to the job_begin_dt and have -1 of date

for example if we have data

Emplid JOB_BEGIN_DT JOBCODE
---------- ----------------------- ----------------
0001 01/01/2006 001
0001 02/01/2006 ABC
0001 04/01/2006 CBA

Now If we need to determine the job_end_dt in each and every jobcode or how long a person has stayed in a particular job code, then the query goes this way.

select EMPLID,
JOB_BEGIN_DT,
JOBCODE
LEAD(JOB_BEGIN_DT,1,0) OVER (ORDER BY JOB_BEGIN_DT) -1 AS JOB_END_DT

The above query will determine the next job_begin_dt in the table and subtract one day from it which will become the job_end_dt for the specified. The resultant data will look like

Emplid JOB_BEGIN_DT JOBCODE JOB_END_DT
---------- ----------------------- ---------------- --------------------
0001 01/01/2006 001 01/31/2006
0001 02/01/2006 ABC 03/31/2006
0001 04/01/2006 CBA


Hope the explanation was useful !!!

Tuesday, July 29, 2008

Cuil vs Google

So there is yet another start up which is into the search market, but this time its a challenging start up and guess what its again the ex-googlers who are getting into the search market. The core team of cuil has been a part of the indexing and ranking features of the google(which means they know search better and what user expects). Cuil claims to have indexed 120 billion pages which is far more than google. The teragoogle team (Russell and Anna) members are now the co-founders of Cuil.Cuil seems to have the best of breed in the management side

Here is what cuil looks like, it has tried to have a similar interface like google black.



I liked the feature explored by category which gives you more information on other related and interested things that you would like to search.

Search is moving in a new dimension and in a more effecient and effective way. I wish cuil also becomes open source which will pave way to the information and index across multiple search engines, also need to know the cheats of cuil to get the most relevant and related results. On th whole a great attempt by a great team of technology enthusiast, Kudos to you guys !!!

Monday, July 28, 2008

Google Knol !!!

Google comes up with yet another spectacular product in the form of Google Knol, as the name suggest google knol is a knowledge sharingsite. In short I would call the wikipedia of Google. But after a couple of views of the google knol, I would say that its more like squidoo. Knol is more author centric and has more articles in different areas of interest i.e it is not a encylopedia !!!

Some of the features of Knol is that Google uses a rich-text editor borrowed from Page Creator, so it's much easier to edit knols than Wikipedia articles. Users can rate the articles, add comments and write reviews, much like for scholarly works.knol provides open collaburation,moderate collaburation and closed collaburation . Google also provides the access to adsense to monetize your knol articles.

Though Google has a lot of products which is used for sharing knowledge and information (blogger,Google docs,Google sites). Knol targets the experts in different domains to shared their knowledege and make a hub for information which will
shared to the billions and trillions of users across the globe.


Happy Googling !!!

Sunday, July 27, 2008

A Tribute to Randy Pausch

Randy Pausch a professor of computer science in CMU delivered an inspiring speech on "Really Achieving your Childhood dreams" was one of the best lectures of all time. It is really inspiring and thought provoking. Randy was diagonised of cancer at a late stage and passed away in 2008. Google also plays tribute to this great professor by adding a line in its homepage as "In Memoriam:Randy Pausch (1960-2008)" and here is the video for you. Randy Pausch- Really Achieving your Childhood dreams

Thursday, July 24, 2008

IGoogle - Limited Edition !!!

iGoogle - Limited Edition

IGoogle is a feature rather a service I would call it from google which provides an integration of all the products of Google under a single web page. You log in to IGoogle and you are ready to check your gmail,google reader,orkut, orkut birthday links.IGoogle is found in the main page of Google




You can add a lot of useful gadgets that are inhouse or you can develop your own gadgets using the google api and make them available to the billions and billions of google users online. These gadgets range from world clock to stock tickers to photo sharing, games and quiz . The gadgets are spread across a wide range of areas from science to fun. You can have cool themes for your IGoogle pages and there is a tab option also available so that you can group your gadgets according to your choice. A view of the IGoogle



Now adding spice to the IGoogle, Google has provided a test version of the new IGoogle through sandbox where it groups all the features on the left hand side and makes you choose the service you wanted and that feature will be available in the right pane and all features of gmail are made available in the new IGoogle(only restricted features are available right now). The new version of the IGoogle is




You can chat, read your feeds, search,browse your mails and watch your youtube videos and do a lot of stuff, ain't Google doing a great stuff.Check out people its one of those features worth trying. Once again Google goes beyond search with a lot of creativity and innovation.

Trick of the Day !!!

One simple trick is we can read a mail in gmail and simulatneously compose a mail in another tab. A simple way of doing it is by pressing the shift button and clicking on the compose link, this will open the compose of a new mail in the new tab leaving the current window with the mail existing.

Tuesday, July 22, 2008

All praise to the Turbunator


The UPA got the confidence back this evening which paves the way for the much anticipated and much awaited Nuclear Deal which will get underway with ease now. The UPA led government was keen on the Nuclear Deal which made it lose their ally in the form of Communist, which let to a vote of confidence of the lower house of the parliament. The much confident and determined as always the prime minister of India accepted the challenge and was more confident of winning the votes. Though few of the parties didnt turn up for voting and the speaker being a communist didnt want to resign and a lot of speculations and controversies happening for the past one month UPA ended up with the winning note and here are the figures for you



Now how is this news related to this blog as the blog is related to techno - psycho stuff, Well the lesson from this winning note of UPA as we see is the confidence in you can do wonders, the confidence of the UPA chairman and the prime minister has made the govt come on a winning note, the confidence to face the challenge of the communist and the ability to withstand pressure and stay focused on the goal.

In terms of psychology , confidence of the inner self can do wonders. Hope you might have read about the power of your subconcious mind. So the winning of UPA is yet another way of saying that confidence in you can do wonders. Wanted to write a lot more but will come up with some other intresting topic

Sunday, July 20, 2008

A Boom which is a disaster on it Own

As i read through papers on the morning of a calm sunday , I came across the engineering admission column which took me back to my days when i used to visit counselling website daily to check the status of engineering seats in various colleges and to gather information abt colleges. those breath taking questions in the engineering, those nervous moments of counselling and the hefty competion where i was left behind 300 people for a mere 0.70 marks, those sad moments when I saw irrelevant people taking IT as major, Finally i became an engineer and now working as a consultant in a very reputed firm in India. Though I am happy that I work in the field which I wanted to, I am also sad as of why my field has dragged a lot of potential engineers in the other fields by its fancy pays and hi-tech lifes.

I turn around my cubicle and I find bright civil engineers,production engineers, mechanical engineers from one of the best institutes of India where getting admission is like a gift sitting and programming. Why did they join Civil engineering just to sit and do some certification in Java or is it be there travelling in a software company bus. Those who are supposed to build great bridges and revolutions in the Indian Industry are sitting there finding out logic for technical problems.

When I sit and think as of why are these people leaving what they have learnt whole heartedly and got a gold medal in it are sitting and coding, Only two things stuck my mind

1. Money and Fame
2. Hi-fi life
3. A Trip to abroad free of cost

I felt these are the two factors that are driving them to this field, they feel that they get recognised in the society if they are called as a Software Engineer and they get hefty money as salary which their father only got when he retired.


Because of this hefty money they try to have a hi-fi life which never belongs to them and ultimately they spoil the society in which they live in

This is the most important driving factor which driving the whole of engineers to this industry " A Trip to abroad" a word which is more often called as "Onsite". Well I dont know how people see onsite as, everybody who joins an IT company wants to go to onsite no matter they have the skill, competency, talent, communication, nothing they look out for. they bluntly ask for a onsite

Because of this enormous recuirting by the IT companies a lot of talented engineers are lost by the other industries and the environment in an IT company is totally screwed by some irrelevant guys from other streams, the whole of engineering methodology is lost, Hope this recession makes a correction in the recruiting of really skilled people by the IT companies