Wednesday, June 15, 2011

SQL - Update Statements



Scenario: (Uses Oracle-SQL)


HR asks to increment the salary


1. <= 3000, increase by 1000;
2. <= 4000, increase by 500;
3. <= 5000 increase by 250;


Example:
Current Salary: 3000 then New Salary should be 3000 + 1000 + 500 + 250 = 4750


Solution:





create table emp_salary
(
  id               number,
  name             varchar2(20),
  current_sal      number(20,2),
  new_sal          number(20,2)
);


insert into EMP_SALARY (id, name, current_sal)
values (1, 'Mani', 3000);
insert into EMP_SALARY (id, name, current_sal)
values (2, 'abhishek', 4000);
insert into EMP_SALARY (id, name, current_sal)
values (3, 'ajay', 5000);
insert into EMP_SALARY (id, name, current_sal)
values (4, 'arun', 2000);
commit;




1. Not efficient:



update emp_salary
set new_sal = current_sal + 1000 + 500 + 250
where current_sal > 0 and current_sal <= 3000;

update emp_salary
set new_sal = current_sal +  500 + 250
where current_sal >= 3001 and current_sal <= 4000;
update emp_salary
set new_sal = current_sal +  250
where current_sal >= 4001 and current_sal <= 5000;
commit;



2. Efficient:


update test_emp_sal
set current_sal =
   case
      when current_sal > 0 AND current_sal <= 3000 then current_sal + 1000 + 500 + 250
      when current_sal > 3001 AND current_sal <= 4000 then current_sal +  500 + 250
      when current_sal > 4001 AND current_sal <= 5000 then current_sal +  250
   end
;

commit;


==================================================================================

update orders
set order_mode_num =
   decode(order_mode,
             'direct',100,
             'online',200,
             'walmart',300,
             'amazon',400,
          0)
;

==================================================================================

update orders
set order_mode_num =
   case
      when order_mode = 'direct' and
           customer_id < 102 then 500
      when order_mode = 'direct' then 100
      when order_mode = 'online' then 200
      when order_mode = 'walmart' then 300
      when order_mode = 'amazon' then 400
      else 0
   end
;

=================================================================================



CAREER!!!

What one skill, if you developed and practiced it consistently in an excellent fashion, would have the greatest positive impact on your career?   ----Brian Tracy.

Wednesday, April 27, 2011

Computer Number System

We know and use Decimal Number System in our day to day life.  Though daily we use computers, we may not think much about how computers understand numbers and their internals.  Looks like school day topics. But it's always good to refresh few basics.


We know Binary, Octal, Hexa Decimal etc.  Let's get into more details....


http://en.wikipedia.org/wiki/Computer_numbering_formats  


It covers 
00010111101010
01237635265376
3637ABCDEF8738


Happy Numbering...Number represents God?




Monday, April 25, 2011

Hash Table: index = f(key, arrayLength)

In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.[1][2]
Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, B-trees are particularly well-suited for implementation of databases, while compiler implementations usually use hash tables to look up identifiers.


http://en.wikipedia.org/wiki/Associative_array
An associative array (also associative container, map, mapping, dictionary, finite map, and in query-processing an index or index file) is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values). The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by an associative array. The relationship between a key and its value is sometimes called a mapping or binding.


Hash Tables


http://en.wikipedia.org/wiki/Hash_table


Read & understand clearly about Hash tables, their use & applications.


hash table - internals
http://stackoverflow.com/questions/4453476/hash-how-does-it-work-internally




Hash table advantages:



In a well-dimensioned hash table, the average cost (number of instructions) for each lookup is independent of the number of elements stored in the table. Many hash table designs also allow arbitrary insertions and deletions of key-value pairs, at constant average (indeed, amortized[1]) cost per operation.[2][3]

In many situations, hash tables turn out to be more efficient than search trees or any other table lookup structure. For this reason, they are widely used in many kinds of computer software, particularly for associative arraysdatabase indexing,caches, and sets.



Hash Table Samples

JVM - Java Virtual Machine Internals

http://www.artima.com/insidejvm/ed2/index.html  - Good JVM Internals


Another must read book:



  • Effective Java™, Second Edition

  • By: Joshua Bloch
  • Publisher: Prentice Hall
  • Pub. Date: May 08, 2008
  • Print ISBN-10: 0-321-35668-3
  • Print ISBN-13: 978-0-321-35668-0
  • Web ISBN-10: 0-13-715002-4
  • Web ISBN-13: 978-0-13-715002-1
  • Pages in Print Edition: 384

Friday, April 15, 2011

Use Open Source Software, Donate and Contribute

Now a days we get lot of very useful softwares for personal use at free of cost. If you need to modify/distribute, please check the licensing terms clearly


You get so many open source applications free here. http://sourceforge.net/


Notable applications include, 7Zip for compressing and un-compressing and PDF Generator.  There are great softwares available for day to day use. Explore, Contribute and Donate.