I have a data set with 1000 OBS and I wish to select 10 random OBS.
To my understanding, I need to use RANUNI or RAND, but I can't figure out how to implement.
tanks
There are many ways to do this, but the simplest is probably this
data have;
do x=1 to 1000;
output;
end;
run;
proc surveyselect data=have out=want seed=123 noprint
method=srs
sampsize=10;
run;
This macro selects randomly observations from dataset.
Input:
+---------+---+----+
| counter | x | y |
+---------+---+----+
| 1 | 2 | 2 |
| 2 | 3 | 6 |
| 3 | 4 | 12 |
| 4 | 5 | 20 |
| 5 | 6 | 30 |
+---------+---+----+
data have;
do counter=1 to 1000;
x=counter+1;
y=counter*x;
output;
end;
run;
Macro:
%macro select_random_obs(libname,memname,num);%macro d;%mend d;
/*
libname - libname of your dataset
memname - name of dataset
num - num of obs to select randomly
*/
proc sql noprint; /*select num of obs in your dataset (if it is not static value)*/
select nobs into:max from dictionary.tables where libname="%upcase(&libname)" and memname="%upcase(&memname)";
quit;
%let rand_list=; /*macro variable that will contains random nums of obs to select*/
data _null_; /*init rand_list macro variable*/
length tList $32000.;
n=0;
do while (n<&num);
if n=0 then tList="";
repeat:
u = rand("Uniform");
k = ceil( &Max*u );
str=strip(input(k,best12.));
do i=1 to countw(tList,' ');
if scan(tList,i,' ') = k then goto repeat;
end;
tList=catx(' ',tList,str);
n=n+1;
end;
call symputx('rand_list',tList);
run;
%put &=rand_list;
data want; /*create new data set that contain right number of random observations*/
set have;
if _N_ in (&rand_list);
run;
%mend select_random_obs;
%select_random_obs(work,have,10);
Output:
+---------+-----+--------+
| counter | x | y |
+---------+-----+--------+
| 33 | 34 | 1122 |
| 344 | 345 | 118680 |
| 466 | 467 | 217622 |
| 478 | 479 | 228962 |
| 552 | 553 | 305256 |
| 861 | 862 | 742182 |
| 890 | 891 | 792990 |
| 904 | 905 | 818120 |
| 922 | 923 | 851006 |
| 941 | 942 | 886422 |
+---------+-----+--------+
Related
My table look like this in my oracle db;
ID | NI | NT | MB | ETC
-------------------------------------------
1 |1234567 | | | comments //valid
2 |9654875 | | | jhdsd //valid
3 |43gf543 | | | dd //in-valid
4 |123 | | | dfds //in-valid
5 |12654332 | | | dffd //in-valid
6 | |542 | | comments //valid
7 | |362 | | jhdsd //valid
8 | |9631 | | dd //invlaid
9 | |r45 | | dfds //in-valid
10 | |56 | | dffd // in-valid
11 | | |03121234567 | comments //valid
12 | | |03874514414 | jhdsd //valid
13 | | |05764544444 | dd //in-valid as not starts with 03
14 | | |30010101019 | dfds //in-valid
15 | | |038f5678543 | dffd //in-valid
I like select only valid records with select query
where
NI length should be fix 7 and all, starts with any digit
NT length should be fix 3 and all, starts with any digit
digits MB length should be fix 11, starts with 03 and all digits.
result should look like this;
1 |1234567 | | | comments
2 |9654875 | | | jhdsd
3 | |542 | | comments
4 | |362 | | jhdsd
5 | | |03121234567 | comments
6 | | |03874514414 | jhdsd
Try this:
NI length should be fix 7 and all, starts with any digit
REGEXP_LIKE(NI, '^\d{7}$')
NT length should be fix 3 and all, starts with any digit
REGEXP_LIKE(NT, '^\d{3}$')
digits MB length should be fix 11, starts with 03 and all digits.
REGEXP_LIKE(MB, '^03\d{9}$')
you could use a substr and length
select ID, NI, NT, MB, ETC
from my_table
where length(NI) = 7
and length(NT) = 3
and substr(MB,1,2) ='03'
AND REGEXP_LIKE(NI, '^[[:digit:]]+$')
AND REGEXP_LIKE(NT, '^[[:digit:]]+$')
Below is an example. TABLE 1 is manually created where the first three columns
are loaded here from an external file. Fourth column(SHOWROOM_ID) will be taken from TABLE2
and the rest of the columns in TABLE 1 will be updated based on criteria.
TABLE 1
NAME |OLD_CPR_NO |OLD_COS_NO |SHOWROOM_ID|NM_CPR_COS_MAT|NM_CPR_MAT|COS_CPR_MAT|
------------------------------------------------------------------------------------
FORD | 45 | 487 | | | |
TOYOTA | 78 | 562 | | | |
BENZ | 55 | 789 | | | |
JEEP | 66 | 124 | | | |
HONDA | 34 | 142 | | | |
KIA | 12 | 962 | | | |
GM | 89 | 7787 | | | |
CHRYSLER | 45 | 236 | | | |
AUDI | 67 | 4789 | | | |
TABLE 2
PK|NAME |OLD_CPR_NO |OLD_COS_NO |SHOWROOM_ID
---------------------------------------------
1 |FORD | 45 | 487 | 1
2 |TOYOTA | 78 | 562 | 2
3 |CIAT | 55 | 789 | 3
4 |JEEP | 66 | 124 | 5
5 |HONDA | 34 | 456 | 6
6 |MUSTANG | 12 | 962 | 7
7 |GM | 89 | 56 | 8
8 |CHRYSLER | 45 | 236 | 9
9 |AUDI | 67 | 4789 | 10
STEP 1: Update NM_CPR_COS_MAT column from table 1. This is an indicator field
where NAME,OLD_CPR_NO,OLD_COS_NO matches from TABLE 1 and TABLE 2 then assign indicator 'Y'
I was able to attain the results based on my below query:
UPDATE TABLE_1 TAB1
SET NM_CPR_COS_MAT = (SELECT 'Y'
FROM
TABLE_2 TAB2
WHERE
TRIM(TAB1.NAME) = TRIM(TAB2.NAME)
AND TRIM(TAB1.OLD_CPR_NO) = TRIM(TAB2.OLD_CPR_NO)
AND TRIM(TAB1.OLD_COS_NO) = TRIM(TAB2.OLD_COS_NO)
;
COMMIT;
UPDATE TABLE_1 TAB1
SET SHOWROOM_ID= (SELECT TAB2.SHOWROOM_ID
FROM
TABLE_2 TAB2
WHERE
TRIM(TAB1.NAME) = TRIM(TAB2.NAME)
AND TRIM(TAB1.OLD_CPR_NO) = TRIM(TAB2.OLD_CPR_NO)
AND TRIM(TAB1.OLD_COS_NO) = TRIM(TAB2.OLD_COS_NO)
AND TRIM(TAB1.NM_CPR_COS_MAT) = 'Y'
;
COMMIT;
RESULT:
TABLE 1
NAME |OLD_CPR_NO |OLD_COS_NO |SHOWROOM_ID|NM_CPR_COS_MAT|NM_CPR_MAT|COS_CPR_MAT|
------------------------------------------------------------------------------------
FORD | 45 | 487 | 1 | Y | |
TOYOTA | 78 | 562 | 2 | Y | |
BENZ | 55 | 789 | | | |
JEEP | 66 | 124 | 5 | Y | |
HONDA | 34 | 142 | | | |
KIA | 12 | 962 | | | |
GM | 89 | 7787 | | | |
CHRYSLER | 45 | 236 | 9 | Y | |
AUDI | 67 | 4789 | 10 | Y | |
But I am getting errors if I tried to use the join statements.
UPDATE TABLE_1 TAB1
SET NM_CPR_COS_MAT = 'Y'
FROM
TABLE_2 TAB2 JOIN
TABLE_1 TAB1 ON
TRIM(TAB1.OLD_CPR_NO) = TRIM(TAB2.OLD_CPR_NO)
WHERE
TRIM(TAB1.NAME) = TRIM(TAB2.NAME)
AND TRIM(TAB1.OLD_COS_NO) = TRIM(TAB2.OLD_COS_NO)
;
COMMIT;
ORA-00933: SQL command not properly ended.
From the below resulting table, I have to again UPDATE SHOWROOM_ID column and NM_CPR_MAT
TABLE 1
NAME |OLD_CPR_NO |OLD_COS_NO |SHOWROOM_ID|NM_CPR_COS_MAT|NM_CPR_MAT|COS_CPR_MAT|
------------------------------------------------------------------------------------
FORD | 45 | 487 | 1 | Y | |
TOYOTA | 78 | 562 | 2 | Y | |
BENZ | 55 | 789 | | | |
JEEP | 66 | 124 | 5 | Y | |
HONDA | 34 | 142 | | | |
KIA | 12 | 962 | | | |
GM | 89 | 7787 | | | |
CHRYSLER | 45 | 236 | 9 | Y | |
AUDI | 67 | 4789 | 10 | Y | |
STEP 2:
UPDATE TABLE_1 TAB1
SET NM_CPR_MAT = (SELECT 'Y'
FROM
TABLE_2 TAB2
WHERE
TRIM(TAB1.NAME) = TRIM(TAB2.NAME)
AND TRIM(TAB1.OLD_CPR_NO) = TRIM(TAB2.OLD_CPR_NO)
AND TRIM(NM_CPR_COS_MAT) IS NULL
;
COMMIT;
UPDATE TABLE_1 TAB1
SET SHOWROOM_ID= (SELECT TAB2.SHOWROOM_ID
FROM
TABLE_2 TAB2
WHERE
WHERE
TRIM(TAB1.NAME) = TRIM(TAB2.NAME)
AND TRIM(TAB1.OLD_CPR_NO) = TRIM(TAB2.OLD_CPR_NO)
AND TRIM(NM_CPR_COS_MAT) IS NULL
AND TRIM(NM_CPR_MAT) = 'Y'
;
COMMIT;
I AM GETTING THE BELOW RESULTS.I AM GETTING THE CORRECT 'Y' IN NM_CPR_MAT COLUMNS
AND ALSO THE CORRECT NUMBERS IN SHOWROOM_ID FOR THE NEW UPDATE STATEMENT BUT THE NUMBERS
THAT WAS UPDATED IN THE UPDATED STATEMENT WERE GONE.
TABLE 1
NAME |OLD_CPR_NO |OLD_COS_NO |SHOWROOM_ID|NM_CPR_COS_MAT|NM_CPR_MAT|COS_CPR_MAT|
------------------------------------------------------------------------------------
FORD | 45 | 487 | | Y | |
TOYOTA | 78 | 562 | | Y | |
BENZ | 55 | 789 | | | |
JEEP | 66 | 124 | | Y | |
HONDA | 34 | 142 | 6 | | Y |
KIA | 12 | 962 | | | |
GM | 89 | 7787 | 8 | | Y |
CHRYSLER | 45 | 236 | | Y | |
AUDI | 67 | 4789 | | Y | |
Incorrect syntax, missing (SELECT before 'Y', and don't forget the matching closing bracket at the end.
UPDATE TABLE_1 TAB1
SET NM_CPR_COS_MAT = (SELECT 'Y'
FROM
TABLE_2 TAB2 JOIN
TABLE_1 TAB1 ON
TRIM(TAB1.OLD_CPR_NO) = TRIM(TAB2.OLD_CPR_NO)
WHERE
TRIM(TAB1.NAME) = TRIM(TAB2.NAME)
AND TRIM(TAB1.OLD_COS_NO) = TRIM(TAB2.OLD_COS_NO));
I have a query which is a UNION query which joins multiple tables. All the stats are updated. The query used to run fine and completed within 30-40 seconds but in 12c it's taking insane amount of time(~10 mins to 5 mins)
I have captured the actual plan.
lan hash value: 1038754298
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | OMem | 1Mem | O/1/M |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 50 |00:00:49.93 | 960K| 7 | | | |
| 1 | SORT ORDER BY | | 1 | 1791 | 50 |00:00:49.93 | 960K| 7 | 267K| 267K| 1/0/0|
| 2 | UNION-ALL | | 1 | | 2648 |00:00:49.93 | 960K| 7 | | | |
| 3 | NESTED LOOPS SEMI | | 1 | 1 | 2648 |00:00:42.36 | 498K| 0 | | | |
|* 4 | HASH JOIN | | 1 | 1589 | 9111 |00:00:42.22 | 469K| 0 | 7900K| 2299K| 1/0/0|
|* 5 | HASH JOIN | | 1 | 1140K| 50569 |00:00:19.50 | 342K| 0 | 8321K| 2379K| 1/0/0|
|* 6 | HASH JOIN | | 1 | 44335 | 59660 |00:00:16.01 | 252K| 0 | 1817K| 1817K| 1/0/0|
|* 7 | TABLE ACCESS FULL | PROMHEAD | 1 | 870 | 870 |00:00:00.01 | 602 | 0 | | | |
|* 8 | HASH JOIN | | 1 | 1168K| 172K|00:00:15.94 | 251K| 0 | 1148K| 1148K| 1/0/0|
|* 9 | TABLE ACCESS FULL | PRICE_BATCH_TRAN | 1 | 857 | 857 |00:00:00.01 | 315 | 0 | | | |
| 10 | TABLE ACCESS FULL | PROMSTORE | 1 | 36M| 36M|00:00:04.64 | 251K| 0 | | | |
|* 11 | TABLE ACCESS FULL | PROMSKU | 1 | 6665K| 6665K|00:00:01.49 | 90224 | 0 | | | |
| 12 | PARTITION RANGE ALL | | 1 | 37M| 37M|00:00:10.72 | 127K| 0 | | | |
| 13 | INDEX FAST FULL SCAN | PK_ITEM_ZONE_PRICE | 24 | 37M| 37M|00:00:04.75 | 127K| 0 | | | |
| 14 | PARTITION RANGE ITERATOR | | 9111 | 32376 | 2648 |00:00:00.13 | 28752 | 0 | | | |
| 15 | PARTITION HASH ITERATOR | | 9111 | 32376 | 2648 |00:00:00.11 | 28752 | 0 | | | |
|* 16 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED | FDT_PRICE_FUTURE | 9111 | 32376 | 2648 |00:00:00.09 | 28752 | 0 | | | |
|* 17 | INDEX RANGE SCAN | FDT_PRICE_FUTURE_PK | 9111 | 1 | 2668 |00:00:00.07 | 26089 | 0 | | | |
|* 18 | HASH JOIN | | 1 | 1790 | 0 |00:00:07.31 | 450K| 7 | 1599K| 1599K| 1/0/0|
| 19 | TABLE ACCESS FULL | PRICE_SUSP_HEAD | 1 | 2056 | 2056 |00:00:00.01 | 965 | 0 | | | |
|* 20 | HASH JOIN | | 1 | 1912 | 0 |00:00:07.30 | 449K| 7 | 39M| 8299K| 1/0/0|
| 21 | PART JOIN FILTER CREATE | :BF0000 | 1 | 745K| 745K|00:00:00.12 | 1682 | 0 | | | |
| 22 | TABLE ACCESS FULL | PRICE_ZONE_GROUP_STORE | 1 | 745K| 745K|00:00:00.03 | 1682 | 0 | | | |
|* 23 | HASH JOIN | | 1 | 496K| 0 |00:00:07.03 | 448K| 7 | 73M| 6638K| 1/0/0|
| 24 | PARTITION RANGE ALL | | 1 | 1138K| 1138K|00:00:01.70 | 154K| 7 | | | |
| 25 | PARTITION HASH ALL | | 26 | 1138K| 1138K|00:00:01.53 | 154K| 7 | | | |
| 26 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED| FDT_PRICE_FUTURE | 208 | 1138K| 1138K|00:00:01.35 | 154K| 7 | | | |
|* 27 | INDEX SKIP SCAN | FDT_PRICE_FUTURE_I1 | 208 | 1138K| 1138K|00:00:00.89 | 125K| 7 | | | |
|* 28 | HASH JOIN | | 1 | 25M| 0 |00:00:04.84 | 293K| 0 | 977K| 977K| 1/0/0|
|* 29 | HASH JOIN | | 1 | 252K| 1123 |00:00:03.33 | 90826 | 0 | 1344K| 1344K| 1/0/0|
|* 30 | TABLE ACCESS FULL | PROMHEAD | 1 | 870 | 870 |00:00:00.01 | 602 | 0 | | | |
|* 31 | TABLE ACCESS FULL | PROMSKU | 1 | 6665K| 6665K|00:00:01.47 | 90224 | 0 | | | |
| 32 | PARTITION RANGE JOIN-FILTER | | 1 | 6719K| 32778 |00:00:01.51 | 202K| 0 | | | |
|* 33 | TABLE ACCESS FULL | PRICE_SUSP_DETAIL | 24 | 6719K| 32778 |00:00:01.51 | 202K| 0 | | | |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("IZP"."ITEM"="PSK"."SKU" AND "IZP"."ZONE_GROUP_ID"="PBT"."ZONE_GROUP_ID" AND "IZP"."ZONE_ID"="PBT"."NEW_ZONE_ID")
5 - access("PS"."PROMOTION"="PSK"."PROMOTION")
6 - access("PH"."PROMOTION"="PS"."PROMOTION")
7 - filter("PH"."STATUS"='A')
8 - access("PS"."STORE"="PBT"."STORE")
filter("PS"."START_DATE">"PBT"."EFFECTIVE_DATE")
9 - filter("PBT"."FDC_STATUS"='A')
11 - filter(("PSK"."CHANGE_TYPE"='MA' OR "PSK"."CHANGE_TYPE"='PC'))
16 - filter("PRICE_MOD_TYPE"='ZC')
17 - access("STORE"="PS"."STORE" AND "SKU"="PSK"."SKU" AND "START_DATE"<"PS"."START_DATE")
18 - access("PSH"."PRICE_CHANGE"="PSD"."PRICE_CHANGE")
filter("FDT"."START_DATE">"PSH"."ACTIVE_DATE")
20 - access("PSD"."ZONE_ID"="PZGS"."ZONE_ID" AND "PSD"."ZONE_GROUP_ID"="PZGS"."ZONE_GROUP_ID" AND "FDT"."STORE"="PZGS"."STORE")
23 - access("PSK"."PROMOTION"="FDT"."PRICE_MOD_NO" AND "PSD"."SKU"="FDT"."SKU")
27 - access("FDT"."PRICE_MOD_TYPE"='PR')
filter("FDT"."PRICE_MOD_TYPE"='PR')
28 - access("PSK"."SKU"="PSD"."SKU")
29 - access("PH"."PROMOTION"="PSK"."PROMOTION")
30 - filter("PH"."STATUS"='A')
31 - filter(("PSK"."CHANGE_TYPE"='MA' OR "PSK"."CHANGE_TYPE"='PC'))
33 - filter("PSD"."STATUS"='A')
Note
-----
- this is an adaptive plan
As you can see Oracle predicts the cardinality correctly in most cases. Can you suggest where should I start looking from.
SELECT
PBT.STORE ,
PSK.SKU ,
0 MULTI_UNITS ,
0 MULTI_UNIT_RETAIL ,
PS.START_DATE PS_START_DATE ,
PBT.EFFECTIVE_DATE PC_ACTIVE_DATE ,
PBT.ZONE_CHANGE PRICE_CHANGE ,
PH.PROMOTION PRICE_MOD_NO ,
'PR' PRICE_TYPE ,
'U' REC_TYPE
FROM PROMHEAD PH ,
PROMSTORE PS ,
PROMSKU PSK ,
PRICE_BATCH_TRAN PBT,
ITEM_ZONE_PRICE IZP
WHERE PH.PROMOTION =PS.PROMOTION
AND PS.PROMOTION =PSK.PROMOTION
AND PBT.FDC_STATUS ='A'
AND PH.STATUS ='A'
AND PS.STORE =PBT.STORE
AND PSK.CHANGE_TYPE IN('MA','PC')
AND IZP.ITEM =PSK.SKU
AND IZP.ZONE_GROUP_ID=PBT.ZONE_GROUP_ID
AND IZP.ZONE_ID =PBT.NEW_ZONE_ID
AND PS.START_DATE > PBT.EFFECTIVE_DATE
AND EXISTS
(
SELECT 1
FROM FDT_PRICE_FUTURE
WHERE SKU =PSK.SKU
AND STORE =PS.STORE
AND START_DATE < PS.START_DATE
AND PRICE_MOD_TYPE = 'ZC'
)
UNION ALL
SELECT FDT.STORE ,
FDT.SKU ,
0 MULTI_UNITS ,
0 MULTI_UNIT_RETAIL ,
FDT.START_DATE PS_START_DATE ,
PSH.ACTIVE_DATE PC_ACTIVE_DATE ,
PSD.PRICE_CHANGE PRICE_CHANGE ,
PH.PROMOTION PRICE_MOD_NO ,
'PR' PRICE_TYPE ,
'U' REC_TYPE
FROM FDT_PRICE_FUTURE FDT ,
PROMHEAD PH ,
PROMSKU PSK ,
PRICE_SUSP_DETAIL PSD,
PRICE_SUSP_HEAD PSH ,
PRICE_ZONE_GROUP_STORE PZGS
WHERE PH.PROMOTION =PSK.PROMOTION
AND PSK.PROMOTION =FDT.PRICE_MOD_NO
AND FDT.PRICE_MOD_TYPE='PR'
AND PH.STATUS ='A'
AND PSH.PRICE_CHANGE =PSD.PRICE_CHANGE
AND PSD.STATUS ='A'
AND PSD.ZONE_ID =PZGS.ZONE_ID
AND PSD.ZONE_GROUP_ID =PZGS.ZONE_GROUP_ID
AND FDT.STORE =PZGS.STORE
AND PSK.SKU =PSD.SKU
AND PSD.SKU =FDT.SKU
AND PSK.CHANGE_TYPE IN('MA','PC')
AND FDT.START_DATE > PSH.ACTIVE_DATE
ORDER BY 1,2,7;
Also, if Diagnostics and Tuning Pack licenses available, you can run the SQL Tuning Advisor on the SQL.
-- SQLTUNE.sql
-- Updated 04-Mar-2015 RDCornejo for spool error
-- takes sql_id,plan_hash_value, start and ent snap_id's as parameters
-- created sql tuning report on c:\temp
set echo off
set feedback off
set term off
set verify off
set head on
-- uncomment for debugging if neeed
-- set echo on term on
set termout on
accept SQL_ID char prompt 'Enter the sql_id to Tune? '
accept hash_value char prompt 'Enter the hash_value to Tune? '
accept BEGIN_SNAP char prompt 'Enter the begin snap_id to Tune? '
accept END_SNAP char prompt 'Enter the end snap_id to Tune? '
-- using bind variables:
variable sql_id_var varchar2(25);
variable plan_hash_value varchar2(25);
variable begin_snap_var varchar2(25);
variable end_snap_var varchar2(25);
variable task_name varchar2(100);
exec :sql_id_var := '&SQL_ID';
exec :plan_hash_value := '&hash_value';
exec :begin_snap_var := '&BEGIN_SNAP';
exec :end_snap_var := '&END_SNAP';
exec :task_name := :sql_id_var || '_' || :plan_hash_value || '_AWR';
column sql_id format a25
column plan_hash_value format 999999999999
column begin_snap format a10
column end_snap format a10
select :sql_id_var as sql_id
, :plan_hash_value as plan_hash_value
, :begin_snap_var as begin_snap
, :end_snap_var as end_snap
from dual;
-- get my prefered text in file name
column MY_TEXT new_val FILE_NAME noprint
Select instance_name||'_'|| :SQL_ID_VAR || '_' || :plan_hash_value|| '_' || :BEGIN_SNAP_VAR || '_' || :END_SNAP_VAR || '_' || to_char(sysdate, 'yyyy_mm_dd_hh24_mi') MY_TEXT
from
(select upper(substr(global_name, 1, instr(global_name,'.' )-1)) instance_name from global_name);
-- turn spooling on to capture any error and the report.
set serveroutput on
spool c:\temp\AWR_Tuning_Task_&FILE_NAME..txt
set linesize 210
set pagesize 9999
set long 10000000
set time on
set timing on
SET SERVEROUTPUT ON
prompt Drop existing task if exists. Ignore error if not
-- Drop existig tuning task; assuming I'm running new tasks and if one exists, it it no longer needed; can ignore error if no task exists yet
execute DBMS_SQLTUNE.drop_tuning_task (task_name => :task_name);
prompt Create Tuning Task
-- Tuning task created for specific a statement from the AWR. [SEE BELOW FOR EXAPLES USING OTHER METHODS OF IDENTIFYING THE SQL]
--If the TASK_NAME parameter is specified it's value is returned as the SQL tune task identifier.
--If ommitted a system generated name like "TASK_1478" is returned.
--If the SCOPE parameter is set to scope_limited the SQL profiling analysis is omitted.
--The TIME_LIMIT parameter simply restricts the time the optimizer can spend compiling the recommendations.
/**/
DECLARE
l_sql_tune_task_id VARCHAR2(100);
BEGIN
l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task (
begin_snap => to_number(:BEGIN_SNAP_VAR) ,
end_snap => to_number(:END_SNAP_VAR) ,
sql_id => :sql_id_var,
plan_hash_value => to_number(:plan_hash_value),
scope => DBMS_SQLTUNE.scope_comprehensive,
time_limit => 1200, -- in seconds
task_name => :task_name,
description => 'Tuning task for statement '|| :sql_id_var || ' hash: ' || :plan_hash_value|| ' in AWR ' || :BEGIN_SNAP_VAR || ' - ' || :END_SNAP_VAR || ' .');
DBMS_OUTPUT.put_line('l_sql_tune_task_id: ' || l_sql_tune_task_id);
END;
/
prompt Execute Tuning Task
-- With the tuning task defined the next step is to execute it using the EXECUTE_TUNING_TASK procedure.
EXEC DBMS_SQLTUNE.execute_tuning_task(task_name => :task_name);
/**/
-- Once the tuning task has executed successfully the recommendations can be displayed using the REPORT_TUNING_TASK function.
set linesize 210
set pagesize 9999
set long 10000000
set time on
set timing on
SET SERVEROUTPUT ON
-- width of output should not need more than 200
column recommendations format a200
prompt Report Tuning Task
SELECT DBMS_SQLTUNE.report_tuning_task(:task_name) AS recommendations FROM dual;
spool off
I have a table in SPSS that contains multiple columns, like this:
+--------+-------+-------+-------+-------+
| | Col 1 | Col 2 | Col 3 | Total |
+--------+-------+-------+-------+-------+
| Data 1 | 10 | 1 | 30 | 41 |
| Data 2 | 4 | 10 | 10 | 24 |
| Data 3 | 3 | 40 | 1 | 44 |
| Data 4 | 10 | 5 | 3 | 18 |
+--------+-------+-------+-------+-------+
I want to add a row at the bottom that calculates the total of each column. in the end, it would look something like this:
+--------+-------+-------+-------+-------+
| | Col 1 | Col 2 | Col 3 | Total |
+--------+-------+-------+-------+-------+
| Data 1 | 10 | 1 | 30 | 41 |
| Data 2 | 4 | 10 | 10 | 24 |
| Data 3 | 3 | 40 | 1 | 44 |
| Data 4 | 10 | 5 | 3 | 18 |
| TOTAL | 27 | 56 | 44 | 127 |
+--------+-------+-------+-------+-------+
Does anyone know what I would have to add to my current code to achieve this?
EDIT: Here is my current code:
TEMPORARY.
SELECT IF Remove = 0.
CTABLES
/VLABELS VARIABLES=TBI1 ME1 BFCE1 CFCE1 RTWPM1 VPA1 NPS1 NPA1 PROV
DISPLAY=LABEL
/TABLE TBI1 [C] + ME1 [C] + BFCE1 [C] + CFCE1 [C] + RTWPM1 [C] + VPA1 [C] + NPS1 [C] + NPA1 [C] BY PROV [C]
[COUNT F40.0, ROWPCT.COUNT PCT40.1]
/CATEGORIES VARIABLES=TBI1 ME1 BFCE1 CFCE1 RTWPM1 VPA1 NPS1 NPA1 [1.00] EMPTY=INCLUDE
/CATEGORIES VARIABLES=PROV ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES
/TITLES TITLE='Brain Injury' CAPTION='Type of assessment and volume of each service by provider.'.
You can get output like this from the CROSSTABS procedure (Analyze > Descriptive Statistics > Crosstabs)
Here is one option you may want to try.
Create two new variables in syntax window.
COMPUTE ALL_TAB_COL = 1.
COMPUTE ALL_TAB_ROW = 1.
EXECUTE.
VARIABLE LABELS ALL_TAB_COL 'All Cols'
/ALL_TAB_ROW 'All Rows'.
Go to Main Menu Analyze > Tables > Custom Tables
From Variable List
Drag ALL_TAB_COL to Column Area
Drag ALL_TAB_ROW to Row Area (if needed)
Thereafter you can drag other column to appear after ALL_TAB_COL;
and row variables to appear below ALL_TAB_ROW.
In Summary statistics submenu if you choose Count, the ALL_TAB_COL shows, Column Sum of all Counts against the ALL_TAB_ROW and further break ups by other variables.
Assume a range of values inserted in a schema table and in the end of the month i want to apply for these records (i.e. 2500 rows = numeric values) the algorithm: sort the values descending (from the smallest to highest value) and then find the 80% value of the sorted column.
In my example, if each row increases by one starting from 1, the 80% value will be the 2000 row=value (=2500-2500*20/100). This algorithm needs to be implemented in a procedure where the number of rows is not constant, for example it can varries from 2500 to 1,000,000 per month
Hint: You can achieve this using Oracle's cumulative aggregate functions. For example, suppose your table looks like this:
MY_TABLE
+-----+----------+
| ID | QUANTITY |
+-----+----------+
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
| E | 5 |
| F | 6 |
| G | 7 |
| H | 8 |
| I | 9 |
| J | 10 |
+-----+----------+
At each row, you can sum the quantities so far using this:
SELECT
id,
quantity,
SUM(quantity)
OVER (ORDER BY quantity ROWS UNBOUNDED PRECEDING)
AS cumulative_quantity_so_far
FROM
MY_TABLE
Giving you:
+-----+----------+----------------------------+
| ID | QUANTITY | CUMULATIVE_QUANTITY_SO_FAR |
+-----+----------+----------------------------+
| A | 1 | 1 |
| B | 2 | 3 |
| C | 3 | 6 |
| D | 4 | 10 |
| E | 5 | 15 |
| F | 6 | 21 |
| G | 7 | 28 |
| H | 8 | 36 |
| I | 9 | 45 |
| J | 10 | 55 |
+-----+----------+----------------------------+
Hopefully this will help in your work.
Write a query using the percentile_disc function to solve your problem. Sounds like it does what you want.
An example would be
select percentile_disc(0.8) within group (order by the_value)
from my_table