 |
|
| Computers Forum Index » Computer - Databases - Oracle (Server) » Materialized View... |
|
Page 1 of 1 |
|
| Author |
Message |
| The Magnet... |
Posted: Fri Oct 23, 2009 11:56 pm |
|
|
|
Guest
|
Hi,
Hope I can explain this properly:
I'm hoping this can be done in just 1 long SQL statement. We have
several queries that gather counts from different tables. Each takes
a while and since they are part of the same app, the screen takes a
while. So, we want to use a materialized view.
Problem is that each column would need to be a query. The tables are
different, though we basically need the same data from them, a date
and a count of records on that date. So, each query will return
different results, but yet we want 1 materialized view to contain the
counts of the 6 tables. For example, here are 2 of the queries:
SELECT
event_date, count(*) DIVIDENDS
FROM
(
SELECT
DISTINCT(ticker) ticker,
company_name company,
div_rate_per_share dividend,
announcement_date event_date,
announcement_date announcement_date,
div_expected_date ex_date,
record_date record_date,
payment_date payment_date
FROM
data_holder.expected_dividends
UNION
SELECT
DISTINCT(ticker) ticker,
company_name company,
div_rate_per_share dividend,
div_expected_date event_date,
announcement_date announcement_date,
div_expected_date ex_date,
record_date record_date,
payment_date payment_date
FROM data_holder.expected_dividends
UNION
..
..
..
And another one:
SELECT
t.q0_expected_report_date, count(*) earnings
FROM
master_table m,
estimates e,
data_holder.eps_reports_ex5 t
WHERE
e.m_ticker(+) = m.m_ticker AND
m.ticker = t.ticker AND
t.late_last_flag = 0 AND
t.report_date = (SELECT MAX(a.report_date)
FROM
data_holder.eps_reports_ex5 a
WHERE
t.ticker = a.ticker AND
a.late_last_flag = 0)
group by t.q0_expected_report_date
order by t.q0_expected_report_date;
So, each column in the view will represent a query of counts from a
particular table. Is this possible? It would speed everything up
since we have the results already.
I was able to create a view, but not with the proper data. My guess
is some combination of ROW_NUMBER or COUNT or something like that.... |
|
|
| Back to top |
|
|
|
| Shakespeare... |
Posted: Sat Oct 24, 2009 12:38 pm |
|
|
|
Guest
|
The Magnet schreef:
Quote: Hi,
Hope I can explain this properly:
I'm hoping this can be done in just 1 long SQL statement. We have
several queries that gather counts from different tables. Each takes
a while and since they are part of the same app, the screen takes a
while. So, we want to use a materialized view.
Problem is that each column would need to be a query. The tables are
different, though we basically need the same data from them, a date
and a count of records on that date. So, each query will return
different results, but yet we want 1 materialized view to contain the
counts of the 6 tables. For example, here are 2 of the queries:
SELECT
event_date, count(*) DIVIDENDS
FROM
(
SELECT
DISTINCT(ticker) ticker,
company_name company,
div_rate_per_share dividend,
announcement_date event_date,
announcement_date announcement_date,
div_expected_date ex_date,
record_date record_date,
payment_date payment_date
FROM
data_holder.expected_dividends
UNION
SELECT
DISTINCT(ticker) ticker,
company_name company,
div_rate_per_share dividend,
div_expected_date event_date,
announcement_date announcement_date,
div_expected_date ex_date,
record_date record_date,
payment_date payment_date
FROM data_holder.expected_dividends
UNION
.
.
.
And another one:
SELECT
t.q0_expected_report_date, count(*) earnings
FROM
master_table m,
estimates e,
data_holder.eps_reports_ex5 t
WHERE
e.m_ticker(+) = m.m_ticker AND
m.ticker = t.ticker AND
t.late_last_flag = 0 AND
t.report_date = (SELECT MAX(a.report_date)
FROM
data_holder.eps_reports_ex5 a
WHERE
t.ticker = a.ticker AND
a.late_last_flag = 0)
group by t.q0_expected_report_date
order by t.q0_expected_report_date;
So, each column in the view will represent a query of counts from a
particular table. Is this possible? It would speed everything up
since we have the results already.
I was able to create a view, but not with the proper data. My guess
is some combination of ROW_NUMBER or COUNT or something like that....
Yes you can. Create your different queries per result column in the
materialized view like
select max(first_column), max(second_column), max(third_column)
from
select first_column,0,0
union
select 0, second_column, 0
union
select 0, 0, third_column
The unions create a view with three (or n) rows, and you condense it by
selecting with max. You get a result with one row in your materialized
view. You could even consider using a table, and mark the row with a
calculated date column, so you could even keep your history in the table!
(I even think there is a function to return the first non-null column
you could use in stead of the max, and then you could use null in stead
of 0)
You could get your results in *different* rows (if you would want that)
by using a marker column in your query parts:
select 'count ticker' marker, <your query here>
union
select 'count earnings' marker, <your query here>
union
select 'count value3' marker, <your query here>
which would give you n rows with a column telling you which count is in
that row.
Shakespeare |
|
|
| Back to top |
|
|
|
| The Magnet... |
Posted: Sat Oct 24, 2009 4:54 pm |
|
|
|
Guest
|
On Oct 24, 3:38 am, Shakespeare <what... at (no spam) xs4all.nl> wrote:
Quote: The Magnet schreef:
Hi,
Hope I can explain this properly:
I'm hoping this can be done in just 1 long SQL statement. We have
several queries that gather counts from different tables. Each takes
a while and since they are part of the same app, the screen takes a
while. So, we want to use a materialized view.
Problem is that each column would need to be a query. The tables are
different, though we basically need the same data from them, a date
and a count of records on that date. So, each query will return
different results, but yet we want 1 materialized view to contain the
counts of the 6 tables. For example, here are 2 of the queries:
SELECT
event_date, count(*) DIVIDENDS
FROM
(
SELECT
DISTINCT(ticker) ticker,
company_name company,
div_rate_per_share dividend,
announcement_date event_date,
announcement_date announcement_date,
div_expected_date ex_date,
record_date record_date,
payment_date payment_date
FROM
data_holder.expected_dividends
UNION
SELECT
DISTINCT(ticker) ticker,
company_name company,
div_rate_per_share dividend,
div_expected_date event_date,
announcement_date announcement_date,
div_expected_date ex_date,
record_date record_date,
payment_date payment_date
FROM data_holder.expected_dividends
UNION
.
.
.
And another one:
SELECT
t.q0_expected_report_date, count(*) earnings
FROM
master_table m,
estimates e,
data_holder.eps_reports_ex5 t
WHERE
e.m_ticker(+) = m.m_ticker AND
m.ticker = t.ticker AND
t.late_last_flag = 0 AND
t.report_date = (SELECT MAX(a.report_date)
FROM
data_holder.eps_reports_ex5 a
WHERE
t.ticker = a.ticker AND
a.late_last_flag = 0)
group by t.q0_expected_report_date
order by t.q0_expected_report_date;
So, each column in the view will represent a query of counts from a
particular table. Is this possible? It would speed everything up
since we have the results already.
I was able to create a view, but not with the proper data. My guess
is some combination of ROW_NUMBER or COUNT or something like that....
Yes you can. Create your different queries per result column in the
materialized view like
select max(first_column), max(second_column), max(third_column)
from
select first_column,0,0
union
select 0, second_column, 0
union
select 0, 0, third_column
The unions create a view with three (or n) rows, and you condense it by
selecting with max. You get a result with one row in your materialized
view. You could even consider using a table, and mark the row with a
calculated date column, so you could even keep your history in the table!
(I even think there is a function to return the first non-null column
you could use in stead of the max, and then you could use null in stead
of 0)
You could get your results in *different* rows (if you would want that)
by using a marker column in your query parts:
select 'count ticker' marker, <your query here
union
select 'count earnings' marker, <your query here
union
select 'count value3' marker, <your query here
which would give you n rows with a column telling you which count is in
that row.
Shakespeare
Thanks Shakespeare, one question I have though.
If each column comes from a different query, which has different FROM
clauses, doesn't there have to be some common column? Maybe I am not
looking at it right, but say query #1 returns 100 rows from TABLE1,
and query #2 returns 95 rows from TABLE2 & TABLE3. The 5 rows that
are missing from query #2 should be NULL in the final view. Does this
mean that somehow I need to maybe create "virtual" tables/views and
join them before creating the actual materialized view?? And if yes,
this could get messy with so many columns, 6 to be exact.
What is good about this view, is that it is date driven, so I only
need 1 date column.
Many thanks! |
|
|
| Back to top |
|
|
|
| Randolf Geist... |
Posted: Sun Oct 25, 2009 1:24 pm |
|
|
|
Guest
|
On Oct 24, 12:56 am, The Magnet <a... at (no spam) unsu.com> wrote:
Quote: Hi,
Hope I can explain this properly:
I'm hoping this can be done in just 1 long SQL statement. We have
several queries that gather counts from different tables. Each takes
a while and since they are part of the same app, the screen takes a
while. So, we want to use a materialized view.
Problem is that each column would need to be a query. The tables are
different, though we basically need the same data from them, a date
and a count of records on that date. So, each query will return
different results, but yet we want 1 materialized view to contain the
counts of the 6 tables. For example, here are 2 of the queries:
If this is still about the query that you have asked about in your
previous thread: I've updated that old thread how I think you can
speed up that query, may be then you don't have the need for an
additional materialized view.
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Thu Dec 10, 2009 10:48 am
|
|