Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Simulating Dead Locks


session A
update  t1 set c1=x

session  B
update t2 set c2=y

now exchange the sqls in both sessions.

TIMESTAMP Datatypes

TIMESTAMP WITH TIMEZONE - data is stored with a record kept of the time zone to which it refers.
TIMESTAMP WITH LOCAL TIMEZONE - data is normalized to the DB time zone on saving. When retrieved, it is normalized to the time zone of the user process selecting it.

INTERVAL YEAR TO MONTH - contains year-month
INTERVAL '123-2' YEAR(3) TO MONTH --the 3 suffix specifies the no. of digits to be consideres for YEAR.
INTERVAL '5-3' YEAR TO MONTH + INTERVAL'20' MONTH =  INTERVAL '6-11' YEAR TO MONTH

INTERVAL DAY TO SECOND - contains day-hour-min-sec
INTERVAL'20' DAY - INTERVAL'240' HOUR = INTERVAL'10-0' DAY TO SECOND

READ ONLY TRANSACTIONS

During a read-only transaction, all queries refer to the same snapshot of the database***, providing a multi-table, multi-query, read-consistent view. Other users can continue to query or update data as usual. A commit or rollback ends the transaction. eg: a store manager uses a read-only transaction to gather order totals for the day, the past week, and the past month. The totals are unaffected by other users updating the database during the transaction.

command:

SET TRANSACTION READ ONLY;

PIVOT and UNPIVOT

PIVOT:

 select * from
 (
     (
        select 'a' value  from dual union all
        select 'e' value  from dual union all
        select 'i'  value  from dual union all
        select 'o' value  from dual union all
        select 'u' value  from dual
    )
   pivot  -- use pivot xml to get the o/p in xml
    (
       count( value)
       for value in   ('a','e','i','o','u')
    )
 )


UNPIVOT:

 select value from
 (
    (
        select
            'a' v1,
            'e' v2,
            'i' v3,
            'o' v4,
            'u' v5
        from dual
    )
    unpivot [INCLUDE NULLS]
    (
        value
        for value_type in
            (v1, v2,v3,v4,v5) – Also can give ANY here.
    )
 )

Grouping Functions


The key advantages of CUBE and ROLLUP are that they will allow more robust aggregations than COMPUTE and they work with any SQL-enabled tool.

GROUPING SETS allows to specify more than one GROUP BY options in the same record set. Logically expressed as seperate GROUP BY queries connected by UNION.

            select a, b, SUM( c ) FROM tab1 GROUP BY GROUPING SETS ( (a,b), a) ==  select ... tab1 GROUP BY a, b  UNION  select a, null, SUM( c ) ...GROUP BY a

ROLLUP creates subtotals moving left to right using columns provided in ROLLUP. A grand total is provided only if the ROLLUP includes all the cols in GROUP BY clause.

              The order of the columns in the ROLLUP modifier is significant, When you reverse the order, you get different subtotal.
              GROUP BY ROLLUP (a, b, c) is equivalent to GROUPING SETS ( (a, b, c), (a, b), (a), ( )).

The CUBE modifier in the GROUP BY clause creates subtotals for all possible combinations of grouping columns. order of cols is not significant here unlike ROLLUP.

              GROUP BY CUBE( a, b, c) is equivalent to  GROUP BY GROUPING SETS ( (a, b, c), (a, b), (b, c), (a, c), (a), (b), (c), ( )).

            
The GROUPING(expr) function returns a 1 for these summary rows (which has a null) and a 0 for the nonsummary rows for expr. Only for ROLLUP and CUBE.
            
              You can use the GROUPING function in the HAVING clause to filter out rows.


GROUPING_ID (<col_list>) - Simplifies the above grouping function. It is derived from the GROUPING function by concatenating the GROUPING levels together as bits.
           
               The GROUPING_ID function returns the exact level of the group.


GROUP_ID( ) takes no arguments and requires a GROUP BY clause. GROUP_ID returns a numeric identifier that can be used to uniquely identify duplicate groups.

             Assigns a unique number for each duplicate group.For i duplicate groups, It will return values 0 through i-1. Can be used in HAVING to filter out duplicate rows.