Monday 24 August 2015

WITH Clause Tips in Postgresql

Postgres provides "WITH" clause which helps to write auxiliary statements for use in larger query.It helps in breaking down complicated and large queries into simpler forms, which are easily readable. These statements, which are often referred to as Common Table Expressions. 
WITH clause has two forms; Simple & Recursive. 



  • Simple WITH Clause


  • Example is mentioned below. 


    1. With data_set AS  
    2. (Select  
    3. ID  
    4. , NAME  
    5. , AGE  
    6. , ADDRESS  
    7. , SALARY  
    8. FROM COMPANY )  
    9.   
    10. Select * from data_set; 



  • Recursive WITH Clause

  • Recursive WITH or Hierarchical queries, is a form of Common Table Expressions(CTE) where a CTE can reference to itself, i.e., a WITH query can refer to its own output, hence the name recursive. 



  • Example to generate 1000 Rows.


    1. WITH RECURSIVE ROWS100 AS (  
    2. SELECT 1  AS ID  
    3. UNION ALL  
    4. SELECT ID+1 FROM ROWS100  
    5. WHERE ID<100  
    6. ) SELECT * FROM ROWS100  



  • To populate the date dimension.


    1. WITH RECURSIVE time_dim(id) AS (  
    2.                 select 1 AS ID, 0.000694444444444444444444444444444444444444 as interval_period, date_trunc('Day',current_timestamp) + interval '0.000694444444444444444444444444444444444444 days'as date  
    3.                 UNION ALL   
    4.                 SELECT id+1 , (id+1)*interval_period, date_trunc('Day',current_timestamp) + interval '0.000694444444444444444444444444444444444444 days'*(id+1) as date  
    5. from time_dim  
    6.                 WHERE id < 1440  
    7. )  
    8. select id, date, lpad(extract('hour' from date)::varchar(2),2,'0')  as hour , lpad(extract('minute' from date)::varchar(2),2,'0') as minute,to_char(date,'HH') TWLVE,  
    9. TO_CHAR(date,'AM') am_pm  
    10. from time_dim  

    Another example specified below will give the sum of the salaries < 20000

    1. WITH RECURSIVE t(n) AS (  
    2.     VALUES (0)  
    3.     UNION ALL  
    4.     SELECT SALARY FROM COMPANY WHERE SALARY < 20000  
    5. )  
    6. SELECT sum(n) FROM t;  

    Example for will delete the data from a set the deleted records are inserted into another table using WITH Clause

    1. CREATE TABLE COMPANY1(  
    2.    ID INT PRIMARY KEY     NOT NULL,  
    3.    NAME           TEXT    NOT NULL,  
    4.    AGE            INT     NOT NULL,  
    5.    ADDRESS        CHAR(50),  
    6.    SALARY         REAL  
    7. );  
    8.   
    9. WITH moved_rows AS (  
    10.     DELETE FROM COMPANY  
    11.     WHERE  
    12.         SALARY >= 30000  
    13.     RETURNING *  
    14. )  
    15. INSERT INTO COMPANY1 (SELECT * FROM moved_rows);  

    Thursday 18 June 2015

    Steps to configure Oracle Wallet for Storing schema password

       Following are steps to configure Oracle wallet so that the authorized user need not be specified the password and username while connecting to the database. It will be helpful if we have a shell scripts to be executed and which the credential in clear case. Once this is configured, credential information can be removed from the file and database can be accessed using the alias specified in the wallet configuration. 



  • Decide the location of the Oracle wallet say("/u01/app/oracle/wallet")



  • Add the following lines to the sqlnet.ora which is present at $ORACLE_HOME/network/admin/sqlnet.ora

    1. WALLET_LOCATION =  
    2.    (SOURCE =  
    3.      (METHOD = FILE)  
    4.      (METHOD_DATA =  
    5.        (DIRECTORY = /u01/app/oracle/wallet)  
    6.      )  
    7.    )  
    8.   
    9. SQLNET.WALLET_OVERRIDE = TRUE  
    10. SSL_CLIENT_AUTHENTICATION = FALSE  
    11. SSL_VERSION = 0 
    The SQLNET.WALLET_OVERRIDE entry allows this method to override any existing OS authentication configuration. 



  • Create an Oracle wallet in the previously specified location using the mkstore utility with the -create option. The wallet is password protected, but is defined with the "Auto Login" property enabled so connection attempts by the user who created the wallet do not require a password. It will ask for a wallet password.

    1. $ mkdir /u01/app/oracle/wallet  
    2. $ mkstore -wrl "/u01/app/oracle/wallet" -create  
    3. Enter password:  
    4. Enter password again:  
    5. $  

    Add the password credential to the wallet using the following command.

    1. $ mkstore -wrl "/u01/app/oracle/wallet" -createCredential db10g_test scott tiger  
    2. Enter password:  
    3.   
    4. Create credential oracle.security.client.connect_string1  
    5.   
    6. $  

    The db_alias, in this case "db10g", is the identifier used in the "/@db_alias" syntax, and must have a matching entry in the "tnsnames.ora" file. 



  • If multiple users are present, simply add a new entry into the wallet using a different db_alias and make sure the alias is present in the "tnsnames.ora" file.
    Make a new entry for the "db10g" database in the client "tnsnames.ora" file.

    1. DB10G_TEST =  
    2.   (DESCRIPTION =  
    3.     (ADDRESS = (PROTOCOL = TCP)(HOST = <IP_ADDRESS>)(PORT = 1521))  
    4.     (CONNECT_DATA =  
    5.       (SERVER = DEDICATED)  
    6.       (SERVICE_NAME = <SID>)  
    7.     )  
    8.   )  



  • Now connect to the "test" user as shown below.

    1. $ sqlplus /@db10g_test  
    2.   
    3. SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jul 19 10:17:47 2007  
    4.   
    5. Copyright (c) 19822005, Oracle.  All rights reserved.  
    6.   
    7. Connected to:  
    8. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
    9. With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options  
    10.   
    11. SQL> show user  
    12. USER is "SCOTT"  
    13.   
    14. SQL  

    Wednesday 10 June 2015

    Pivot and UnPivot Examples in Oracle

    Hi, 

    Today I am explaining you about two simple functions;but powerful as it can be used for data warehousing application. Those are mentioned below. 



  • Pivot Functions : Pivot Functions basically converts a row information into a column. Assume we have a table "sales_sample" with the structure as mentioned below.


    1. create table sales_sample ( sale_region varchar2(30), sale_year number,  sale_month varchar2(20), sale_product_code varchar2(30), quantity  number )   

    I am populating the table with the following values. 

    1. insert into sales_sample values('North America',2014,'Jan','Iphone',100);  
    2.   
    3. insert into sales_sample values('North America',2014,'Jan','Samsung',200);  
    4.   
    5. insert into sales_sample values('North America',2014,'Jan','Nexus',300);  
    6.   
    7. insert into sales_sample values('Asia Pacific',2014,'Jan','Iphone',510);  
    8.   
    9. insert into sales_sample values('Asia Pacific',2014,'Jan','Samsung',220);  
    10.   
    11. insert into sales_sample values('Asia Pacific',2014,'Jan','Nexus',320);  
    12.   
    13. insert into sales_sample values('North America',2014,'Feb','Iphone',11);  
    14.   
    15. insert into sales_sample values('North America',2014,'Feb','Samsung',22);  
    16.   
    17. insert into sales_sample values('North America',2014,'Feb','Nexus',33);  
    18.   
    19. insert into sales_sample values('Asia Pacific',2014,'Feb','Iphone',44);  
    20.   
    21. insert into sales_sample values('Asia Pacific',2014,'Feb','Samsung',55);  
    22.   
    23. insert into sales_sample values('Asia Pacific',2014,'Feb','Nexus',66);  
    24.   
    25. SQL>select * from sales_sample;  
    26.   
    27. SALE_REGION    SALE_YEAR  SALE_MONTH  SALE_PRODUCT_CODE   QUANTITY  
    28. --------------------------------------------------------------------------------------------  
    29. North America   2014             Jan                  Iphone                            100    
    30. North America   2014             Jan                  Samsung                           200    
    31. North America   2014             Jan                  Nexus                             300    
    32. North America   2014             Feb                  Iphone                            11    
    33. North America   2014             Feb                  Samsung                           22    
    34. North America   2014             Feb                  Nexus                             33    
    35. Pacific         2014             Jan                  Iphone                            510    
    36. Pacific         2014             Jan                  Samsung                           220    
    37. Pacific         2014             Jan                  Nexus                             320    
    38. Pacific         2014             Feb                  Iphone                            44    
    39. Pacific         2014             Feb                  Samsung                           55    
    40. Pacific         2014             Feb                  Nexus                             66    
    41. -----------------------------------------------------------------------------------------

    Now apply the pivot function on the data set; the syntax is mentioned below 

    Pivot Function which will give region,year and month level aggregates of totla quantity of products sold. 

    1. select * from   
    2. (  
    3. select sale_region, SALE_YEAR, sale_month,sale_product_code,quantity from sales_sample ) PIVOT  
    4. (  
    5. SUM(quantity)   
    6.   FOR sale_product_code IN ('Iphone' AS IPHONE_QUANTITY, 'Samsung' SAMSUNG_QUANTITY,'Nexus' NEXUS_QUANTITY)   
    7. )  
    8. ORDER BY SALE_REGION,sale_month  
    9.   
    10. Output  
    11. --------  
    12.   
    13. REGION           YEAR     MONTH  IPHONE_QUANTITY  SAMSUMG_QUANITY   NEXUS_QUANTITY   
    14. ----------------------------------------------------------------------------------------- ---------------------  
    15. Asia Pacific        2014      Feb           44                        55                            66    
    16. Asia Pacific        2014      Jan           510                       220                           320    
    17. North America       2014      Feb           11                        22                            33    
    18. North America       2014      Jan           100                       200                           300    
    19. --------------------------------------------------------------------------------------------------------------  




  • Un Pivot Functions : Converts a column into a corresponding row. It is opposite process of pivoting.


  • UnPivot Example 
    -------------------- 

    1. create table sales_region_year(  
    2. SALE_REGION           VARCHAR2(30)   
    3. SALE_YEAR             NUMBER         
    4. IPHONE_QUANTITY       NUMBER         
    5. SAMSUNG_QUANTITY      NUMBER         
    6. NEXUS_QUANTITY        NUMBER     
    7. )  
    8.   
    9. SELECT * FROM SALES_REGION_YEAR   
    10. UNPIVOT INCLUDE NULLS (quantity FOR product_code IN (IPHONE_QUANTITY AS 'Iphone', SAMSUNG_QUANTITY AS 'Samsung', NEXUS_QUANTITY AS 'Nexus'));  
    11.   
    12. SQL>SELECT * FROM SALES_REGION_YEAR ;  
    13.   
    14. SALE_REGION    SALE_YEAR    PRODUCT_CODE   QUANTITY  
    15. ------------------------------------------------------------------  
    16. North America  2014         Iphone               111  
    17. North America  2014         Samsung              222  
    18. North America  2014         Nexus                333  
    19. Pacific        2014         Iphone               554  
    20. Pacific        2014         Samsung              275  
    21. Pacific        2014         Nexus                386  
    22. -------------------------------------------------------------------- 

    Hope it is very informative.