Friday, October 23, 2020

Window functions in SQL Server

we have 3 type of Window functions, many of the candidate is not aware about the window function during interview session
  • Aggregate Window Functions
    • SUM(), MAX(), MIN(), AVG(). COUNT()
  • Ranking Window Functions
    • RANK(), DENSE_RANK(), ROW_NUMBER(), NTILE()
  • Value Window Functions
    • LAG(), LEAD(), FIRST_VALUE(), LAST_VALUE()

Working
    We all know the aggregate functions, SUM(), MAX(), MIN(), AVG(). COUNT(), if these are executed on whole table then we need to GROUP BY clause in a query. and It will return single result for a query.

These can also used with Over (partition by ) clause in a query. In this case no need to mention group by clause and aggregate function will be return result for each row.

Example of Query, aggregate function with group by clause
SELECT c1, SUM(c2) alias FROM table_name GROUP BY c1;

Example of query, aggregate function without group clause
SELECT c1, c2, c3, c4, c5 ,SUM(c6) OVER(PARTITION BY c2) as alias FROM table_name;

Note* - You can not include the ID (PRIMARY_KEY column of table) while using aggregate function with GROUP by clause.

But we can use ID column in query, when aggregate function using as window function.

This post is for intend to know only type of window function. 

ORA-01940: cannot drop a user that is currently connected

Cause : One is attempt to drop a user and user is currently connect to database, 

Resolution : we need to drop existing session of user, using below command :

ALTER SYSTEM KILL SESSION '*SID*, *SERIAL*';

See Full Post here, to find SID and SERIAL number of connected user (CLICK HERE)

Now you will be able to drop user using command 

Drop user username cascade;

 



Kill Connection of Connect Users Oracle

This post is going to help you if you would like to drop session of connected users of Oracle.

Find the list of connected users using below query :

SELECT 
vs.sid
, vs.serial#
, vs.status
, vp.spid 
FROM 
v$session vs
, v$process vp 
WHERE 
vp.addr(+) = vs.paddr;

If you would like to find the connected sessions for 1 specific user use below where condition :

and vs.username = '*****'

Note * Oracle is case sensitive, use CAPITAL letters only

Use below query to kill session of users, use input of SID/SERIAL from above query.

ALTER SYSTEM KILL SESSION '*SID*, *SERIAL*';





Kill EXPDP Job -Oracle

impdp/expdp give you facility to kill the job if any thing you have commanded wrong or written wrong parameter in impdp/expdp command.

How :

  1. Assume you have already started expdp command
  2. Press ctrl + C to exit prompt
  3. enter "KILL JOB" or 
  4. STOP_JOB=IMMEDIATE

Thursday, October 1, 2020

ORA-02304: Invalid Object Identifier Literal

Was doing the impdp (import) on oracle, restore of database and observed new error populate in logs

ORA-02304: Invalid Object Identifier Literal

CREATE TYPE ***.**** OID "someNUMBERS" AS OBJECT
(
)
ORA-39083: Object type TYPE:  failed to create with error:
ORA-02304: invalid object identifier literal 

SOLUTION:
Use Parameter transform=oid:n in impdp command, it will be looks like

impdp ******* *** ***** transform=oid:n
web stats