jeudi 8 janvier 2015

Rows to columns


I am new to Oracle. I know MySQL. I wrote below query in MySQL:


Tables:



CREATE TABLE agl_asset (
asset_id BIGINT(20) NOT NULL,
caption VARCHAR(64) DEFAULT NULL,
description VARCHAR(4000) DEFAULT NULL,
NAME VARCHAR(1024) DEFAULT NULL,
PRIMARY KEY (asset_id)
)ENGINE=INNODB ;


CREATE TABLE agl_ownership (
ownership_id BIGINT(20) NOT NULL,
owned_object_id BIGINT(20) NOT NULL,
owner_id BIGINT(20) NOT NULL,
ownershiptype_id BIGINT(20) NOT NULL,
PRIMARY KEY (ownership_id)
) ENGINE=INNODB ;


CREATE TABLE agl_ownershiptype (
ownershiptype_id BIGINT(20) NOT NULL,
NAME VARCHAR(255) NOT NULL,
display_name VARCHAR(255) DEFAULT NULL,
TYPE VARCHAR(32) DEFAULT NULL,
solution_flags BIGINT(20) DEFAULT NULL,
systemtenant_id BIGINT(20) NOT NULL,
PRIMARY KEY (ownershiptype_id)
) ENGINE=INNODB ;


Data:



INSERT INTO agl_asset
(asset_id,caption,description,NAME)
VALUES
(4503599627380496,'My Test Entity','sdfsadf','My Test Entity'),
(4503599627380497,'222','sadfsadff','222' ),
(4503599627381496,'my latest','sdfas','my latest'),
(49539595901085456,'Test Service Type Entity','This is for Testing','Test Service Type Entity'),
(207165582859052816,'New Entity','new entity for mobile device','New Entity'),
(211669182486423312,'111','sadasd','111'),
(211669182486424312,'My Latest Entity 11','sadfsad','My Latest Entity 11'),
(211669182486424313,'New Entity for Test','asdfdsaf','New Entity for Test'),
(216172782113793808,'my test11','sdfdsaf','my test11'),
(680043543732954896,'To Find Owners Entity','sadfdsafdsa','To Find Owners Entity');


INSERT INTO agl_ownership
(ownership_id,owned_object_id,owner_id,ownershiptype_id)
VALUES
(1441151880758571720,4503599627380496,495395959010766560,1445655480385940221),
(1441151880758571723,4503599627380497,495395959010766560,1445655480385940221),
(1441151880758572729,4503599627381496,495395959010766560,1445655480385940217),
(1441151880758572720,4503599627381496,495395959010766560,1445655480385940221),
(1441151880758572728,4503599627381496,495395959010767560,1445655480385940222),
(1441151880758572730,4503599627381496,495395959010767562,1445655480385940220),
(1441151880758570720,49539595901085456,495395959010765560,1445655480385940221),
(1441151880758570727,49539595901085456,495395959010767560,1445655480385940220),
(1441151880758570728,49539595901085456,495395959010767562,1445655480385940222),
(1441151880758569720,207165582859052816,495395959010766560,1445655480385940221),
(1441151880758570733,207165582859052816,495395959010767560,1445655480385940222),
(1441151880758570734,207165582859052816,495395959010767562,1445655480385940220),
(1441151880758571722,211669182486423312,495395959010766560,1445655480385940221),
(1441151880758572738,211669182486424312,495395959010766560,1445655480385940220),
(1441151880758572737,211669182486424312,495395959010766560,1445655480385940221),
(1441151880758572740,211669182486424312,495395959010767560,1445655480385940217),
(1441151880758572739,211669182486424312,495395959010767562,1445655480385940222),
(1441151880758572749,211669182486424313,495395959010766560,1445655480385940221),
(1441151880758572750,211669182486424313,495395959010767562,1445655480385940222),
(1441151880758572747,216172782113793808,495395959010766560,1445655480385940218),
(1441151880758572746,216172782113793808,495395959010766560,1445655480385940221),
(1441151880758572748,216172782113793808,495395959010767562,1445655480385940222),
(1441151880758568720,391813167581245152,517913957147607041,1445655480385940230),
(1441151880758568721,391813167581245152,517913957147607041,1445655480385940231),
(1441151880758568722,391813167581245153,517913957147607041,1445655480385940230),
(1441151880758568723,391813167581245153,517913957147607041,1445655480385940231),
(1441151880758568724,391813167581245154,517913957147607041,1445655480385940230),
(1441151880758568725,391813167581245154,517913957147607041,1445655480385940231),
(1441151880758568726,391813167581245155,517913957147607041,1445655480385940230),
(1441151880758568727,391813167581245155,517913957147607041,1445655480385940231),
(1441151880758568728,391813167581245156,517913957147607041,1445655480385940230),
(1441151880758568729,391813167581245156,517913957147607041,1445655480385940231),
(1441151880758572742,680043543732954896,495395959010766560,1445655480385940221),
(1441151880758572743,680043543732954896,495395959010767560,1445655480385940217),
(1441151880758572744,680043543732954896,495395959010767560,1445655480385940220),
(1441151880758572745,680043543732954896,495395959010767562,1445655480385940222),
(1441151880758570723,905223525101503696,495395959010766560,1445655480385940225),
(1441151880758570721,905223525101503696,495395959010766560,1445655480385940226),
(1441151880758570724,905223525101503696,495395959010766560,1445655480385940227),
(1441151880758570722,905223525101503696,517913957147607041,1445655480385940229),
(1441151880758570730,905223525101503698,495395959010766560,1445655480385940225),
(1441151880758570732,905223525101503698,495395959010766560,1445655480385940226),
(1441151880758570729,905223525101503698,495395959010766560,1445655480385940227),
(1441151880758570731,905223525101503698,517913957147607041,1445655480385940229),
(1441151880758572727,905223525101504696,495395959010766560,1445655480385940224),
(1441151880758572724,905223525101504696,495395959010766560,1445655480385940225),
(1441151880758572722,905223525101504696,495395959010766560,1445655480385940226),
(1441151880758572725,905223525101504696,495395959010766560,1445655480385940227),
(1441151880758572726,905223525101504696,495395959010767560,1445655480385940228),
(1441151880758572723,905223525101504696,517913957147607041,1445655480385940229),
(1441151880758572733,905223525101504698,495395959010766560,1445655480385940224),
(1441151880758572736,905223525101504698,495395959010766560,1445655480385940225),
(1441151880758572732,905223525101504698,495395959010766560,1445655480385940226),
(1441151880758572735,905223525101504698,495395959010766560,1445655480385940227),
(1441151880758572734,905223525101504698,495395959010767560,1445655480385940228),
(1441151880758572731,905223525101504698,517913957147607041,1445655480385940229),
(1441151880758570725,1215971899390043920,495395959010766560,1445655480385940221),
(1441151880758570735,1215971899390043921,495395959010767560,1445655480385940221),
(1441151880758571724,1215971899390044921,495395959010766560,1445655480385940221),
(1441151880758572721,1215971899390045920,495395959010766560,1445655480385940221),
(1441151880758572741,1215971899390045921,495395959010766560,1445655480385940221);

INSERT INTO agl_ownershiptype
(ownershiptype_id,NAME,display_name,TYPE,solution_flags,systemtenant_id)
VALUES
(1445655480385939216,'Incident Viewer','Incident Viewer','ASSET',127,535928355657089024),
(1445655480385939217,'Business Owner','Business Owner','ASSET',127,535928355657089024),
(1445655480385939218,'Custom Owner1','Custom Owner1','ASSET',127,535928355657089024),
(1445655480385939219,'Custom Owner2','Custom Owner2','ASSET',127,535928355657089024),
(1445655480385939220,'Executive Owner','Executive Owner','ASSET',127,535928355657089024),
(1445655480385939221,'Primary Owner','Primary Owner','ASSET',127,535928355657089024),
(1445655480385939222,'Security Owner','Security Owner','ASSET',127,535928355657089024),
(1445655480385939223,'Vendor Contact','Vendor Contact','ASSET',127,535928355657089024),
(1445655480385939224,'Policy Approver','Policy Approver','POLICYSET',93,535928355657089024),
(1445655480385939225,'Policy Author','Policy Author','POLICYSET',93,535928355657089024),
(1445655480385939226,'Policy Primary Owner','Primary Owner','POLICYSET',93,535928355657089024),
(1445655480385939227,'Policy Requestor','Policy Requestor','POLICYSET',93,535928355657089024),
(1445655480385939228,'Policy Reviewer','Policy Reviewer','POLICYSET',93,535928355657089024),
(1445655480385939229,'Policy Viewer','Policy Viewer','POLICYSET',93,535928355657089024),
(1445655480385939230,'Reader','Reader','OBJECTGROUP',127,535928355657089024),
(1445655480385939231,'Writer','Writer','OBJECTGROUP',127,535928355657089024),
(1445655480385939232,'Reader','Reader','ATTACHMENTCOLLECTION',127,535928355657089024),
(1445655480385939233,'Writer','Writer','ATTACHMENTCOLLECTION',127,535928355657089024),
(1445655480385940216,'Incident Viewer','Incident Viewer','ASSET',127,535928355657089025),
(1445655480385940217,'Business Owner','Business Owner','ASSET',127,535928355657089025),
(1445655480385940218,'Custom Owner1','Custom Owner1','ASSET',127,535928355657089025),
(1445655480385940219,'Custom Owner2','Custom Owner2','ASSET',127,535928355657089025),
(1445655480385940220,'Executive Owner','Executive Owner','ASSET',127,535928355657089025),
(1445655480385940221,'Primary Owner','Primary Owner','ASSET',127,535928355657089025),
(1445655480385940222,'Security Owner','Security Owner','ASSET',127,535928355657089025),
(1445655480385940223,'Vendor Contact','Vendor Contact','ASSET',127,535928355657089025),
(1445655480385940224,'Policy Approver','Policy Approver','POLICYSET',93,535928355657089025),
(1445655480385940225,'Policy Author','Policy Author','POLICYSET',93,535928355657089025),
(1445655480385940226,'Policy Primary Owner','Primary Owner','POLICYSET',93,535928355657089025),
(1445655480385940227,'Policy Requestor','Policy Requestor','POLICYSET',93,535928355657089025),
(1445655480385940228,'Policy Reviewer','Policy Reviewer','POLICYSET',93,535928355657089025),
(1445655480385940229,'Policy Viewer','Policy Viewer','POLICYSET',93,535928355657089025),
(1445655480385940230,'Reader','Reader','OBJECTGROUP',127,535928355657089025),
(1445655480385940231,'Writer','Writer','OBJECTGROUP',127,535928355657089025),
(1445655480385940232,'Reader','Reader','ATTACHMENTCOLLECTION',127,535928355657089025),
(1445655480385940233,'Writer','Writer','ATTACHMENTCOLLECTION',127,535928355657089025);


Query:



SELECT a.asset_id AS AssetId,a.name AssetName,
MAX(IF(aot.name = 'Primary Owner', asu.userid, 'NO')) PrimaryOwner,
MAX(IF(aot.name = 'Business Owner', asu.userid, 'NO')) BusinessOwner,
MAX(IF(aot.name = 'Business Owner', asu.userid, 'NO')) ExecutiveOwner,
MAX(IF(aot.name = 'Security Owner', asu.userid, 'NO')) SecurityOwner
FROM agl_asset a
INNER JOIN agl_ownership ao ON (ao.owned_object_id=asset_id)
INNER JOIN agl_ownershiptype aot ON (aot.ownershiptype_id=ao.ownershiptype_id)
INNER JOIN agl_systemuser asu ON (asu.systemuser_id=ao.owner_id)

GROUP BY a.asset_id ;


This is correct as of my requirement it is working fine in MySQL.


How can I write same thing in Oracle 11g?





Aucun commentaire:

Enregistrer un commentaire