Query to list Oracle tablespaces - oracle

How can I list Oracle all tablespaces +temp Tablespace by displaying
allocate space(MB), used space(MB), status(online\offline) and type with one SQL query?

Something like this?
SQL> set pagesize 100
SQL> set linesize 100
SQL> break on tablespace_name on status on total_mb
SQL> compute sum of used_mb on tablespace_name
SQL> WITH total
2 AS ( SELECT f.tablespace_name,
3 ROUND (SUM (f.bytes) / POWER (2, 20)) total_mb
4 FROM dba_data_files f
5 GROUP BY f.tablespace_name),
6 used
7 AS ( SELECT s.tablespace_name,
8 s.segment_type,
9 ROUND (SUM (s.bytes) / POWER (2, 20)) used_mb
10 FROM dba_segments s
11 GROUP BY s.tablespace_name, s.segment_type)
12 SELECT ts.tablespace_name,
13 ts.status,
14 t.total_mb,
15 u.segment_type,
16 u.used_mb
17 FROM dba_tablespaces ts
18 JOIN total t ON t.tablespace_name = ts.tablespace_name
19 JOIN used u ON u.tablespace_name = ts.tablespace_name
20 ORDER BY ts.tablespace_name, u.segment_type;
TABLESPACE_NAME STATUS TOTAL_MB SEGMENT_TYPE USED_MB
------------------------------ --------- ---------- ------------------ ----------
APEX ONLINE 544 INDEX 150
LOBINDEX 8
LOBSEGMENT 157
TABLE 179
****************************** ********* ********** ----------
sum 494
APEX_9695076087226093 ONLINE 200 INDEX 1
LOBINDEX 0
LOBSEGMENT 0
TABLE 1
****************************** ********* ********** ----------
sum 2
SYSAUX ONLINE 9816 CLUSTER 3
INDEX 565
INDEX PARTITION 3198
LOB PARTITION 0
LOBINDEX 46
LOBSEGMENT 727
NESTED TABLE 3
TABLE 643
TABLE PARTITION 4017
TABLE SUBPARTITION 2
****************************** ********* ********** ----------
sum 9204
SYSTEM ONLINE 1724 CLUSTER 68
INDEX 184
LOBINDEX 7
LOBSEGMENT 16
NESTED TABLE 1
ROLLBACK 0
TABLE 813
****************************** ********* ********** ----------
sum 1089
UNDOTBS1 ONLINE 4608 TYPE2 UNDO 144
****************************** ********* ********** ----------
sum 144
USER_DATA ONLINE 38820 INDEX 5335
LOBINDEX 40
LOBSEGMENT 9168
TABLE 18104
****************************** ********* ********** ----------
sum 32647
30 rows selected.
SQL>

Related

is it uniform distribution HBC ORACLE HISTOGRAME?

We have this data : Table R(A,..) with attribute A, nbLine of R is 1000, distinct value for A are 500.
data are displayed like this : bucket -> end_point_value.
1 -> 800
2 -> 900
3 -> 1000
4 -> 1200
5 -> 1500
6 -> 2000
7 -> 2300
8 -> 2400
9 -> 2550
10 -> 2590
the question is : Does this histogram confirm or deny the hypothesis of a uniform distribution uniform?
I think I can not confirm nor deny, what do you think ?
First you must ask, which histogram type is defined on the column.
Oracle provides four different histogram types and is you want to claim about uniform distribution the frequency histogram must be defined.
The frequency histogram has one bucket for each distinct value (stored in ENDPOINT_VALUE and the frequency is (additive) stored in the column ENDPOINT_NUMBER)
So if you histogram has only 10 buckets (as you show in the data) you are ready and you can say nothing about the distribution.
Example of a Uniform Distribution
create table r as
select
1 + trunc((rownum-1)/2) A
from dual connect by level <= 1000;
select count(*), count(distinct a), min(a), max(a) from r;
COUNT(*) COUNT(DISTINCTA) MIN(A) MAX(A)
---------- ---------------- ---------- ----------
1000 500 1 500
Create FREQUENCY Histogram with 500 Buckets
exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'R', method_opt=>'for all columns size 500');
select NUM_BUCKETS, HISTOGRAM from user_tab_columns where table_name = 'R';
NUM_BUCKETS HISTOGRAM
----------- ---------------
500 FREQUENCY
select ENDPOINT_VALUE, ENDPOINT_NUMBER from user_histograms where table_name = 'R' order by ENDPOINT_VALUE;
ENDPOINT_VALUE, ENDPOINT_NUMBER
1 2
2 4
3 6
4 8
...
498 996
499 998
500 1000

Need output in below format in oracle. I have tried with lag function it worked if we have only 2 rows in table

Consider below table table.
Id balance
1 100
2 500
3 4000
I need output in below format.
Id balance begin_bal end_bal
1 100 0 100
2 500 100 600
3 4000 600 4600
A little bit of analytics, as you presumed:
SQL> with test (id, balance) as
2 (select 1, 100 from dual union all
3 select 2, 500 from dual union all
4 select 3, 4000 from dual
5 ),
6 temp as
7 (select id, balance, sum(balance) over (order by id) rsum
8 from test
9 )
10 select id,
11 balance,
12 nvl(lag(rsum) over (order by id), 0) begin_bal,
13 rsum end_bal
14 from temp
15 order by id;
ID BALANCE BEGIN_BAL END_BAL
---------- ---------- ---------- ----------
1 100 0 100
2 500 100 600
3 4000 600 4600
SQL>

Decreasing running total in oracle sql

Table A
Week Column_1 Column_2
0 9 92
1 0 84
2 1 84
3 4 83
The result I want is
Week Column_1 Column_2 Remaining
0 9 92 83
1 0 84 83
2 1 84 82
3 4 83 78
So, if you notice. I want to calculate the Remaining as Column_2 - Column_1 for week 0 and after week 0 I want to calculate the Remaining as Remaining - Column_1
Is it possible?
You can use this query.
SELECT a.*,
SUM (CASE
WHEN a.week = 0 THEN column_2 - column_1
ELSE -column_1
END)
over (
ORDER BY week )
FROM tablea a;

add a new column for unique ID in hive table

i have a table in hive with two columns: session_id and duration_time like this:
|| session_id || duration||
1 14
1 10
1 20
1 10
1 12
1 16
1 8
2 9
2 6
2 30
2 22
i want to add a new column with unique id when:
the session_id is changing or the duration_time > 15
i want the output to be like this:
session_id duration unique_id
1 14 1
1 10 1
1 20 2
1 10 2
1 12 2
1 16 3
1 8 3
2 9 4
2 6 4
2 30 5
2 22 6
any ideas how to do that in hive QL?
thanks!
SQL tables represent unordered sets. You need a column specifying the ordering of the values, because you seem to care about the ordering. This could be an id column or a created-at column, for instance.
You can do this using a cumulative sum:
select t.*,
sum(case when duration > 15 or seqnum = 1 then 1 else 0 end) over
(order by ??) as unique_id
from (select t.*,
row_number() over (partition by session_id order by ??) as seqnum
from t
) t;

Oracle 10g ROLLUP and TO_CHAR function

I have 2 queries:
select zam_klt_id,zam_order_date, count(*) as sum from orders
group by rollup (zam_order_date,zam_klt_id);
which produce output:
ZAM_KLT_ID ZAM_order_date SUM
---------- ------------------- ----------
1002 98/03/13 1
98/03/13 1
1004 98/03/14 1
98/03/14 1
1003 98/09/11 1
98/09/11 1
1003 99/01/05 1
99/01/05 1
1003 99/03/01 1
99/03/01 1
1003 99/07/26 1
99/07/26 1
1003 99/10/30 1
99/10/30 1
1002 00/05/08 1
00/05/08 1
1004 00/06/14 1
00/06/14 1
00/07/12 1
00/07/12 1
1000 00/12/10 2
00/12/10 2
1004 00/12/21 1
00/12/21 1
13
This is OK, under every date there is short summary (count) like in
1000 00/12/10 2
00/12/10 2
However then I wanted to know in every year how many clients made orders, so I changed preveious query (zam_order_date was changed to to_char(zam_order_date,'yyyy'))
select zam_klt_id,to_char(zam_order_date,'yyyy'), count(*) as sum from orders
group by rollup ((to_char(zam_order_date,'yyyy'),zam_klt_id));
will produce output
ZAM_KLT_ID TO_CHAR(ZAM_order_date,'YYYY') SUM
---------- ----------------------------------- ----------
1002 1998 1
1003 1998 1
1004 1998 1
1003 1999 4
2000 1
1000 2000 2
1002 2000 1
1004 2000 2
13
9 rows selected
this time there is no summary under every date (year in this case), I think the output should look like this :
ZAM_KLT_ID TO_CHAR(ZAM_order_date,'YYYY') SUM
---------- ----------------------------------- ----------
1002 1998 1
1003 1998 1
1004 1998 1
*1998* *3*
etc
Why summaries are not added this time, is it have something to do with the to_char function?

Resources