Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to optimize performance of this SQL query

Gayathri VenugopalJul 6 2015 — edited Jul 6 2015

Hi,

I need to find the age for each day ,but I need it for all previous dates in one query. So I used the following query:

select trunc(sysdate) - level + 1 DATE

,trunc(sysdate) - level + 1 - created_date AGE 

  from items

connect by trunc(sysdate) - level + 1 - created_date > 0

I am getting output(FOR DATE & AGE) which is fine and correct:

DATE              AGE

   --------- ----------

   6-JUL-15          22

   5-JUL-15          21

   4-JUL-15          20

   3-JUL-15          19

   2-JUL-15          18

   1-JUL-15          17

   30-JUN-15         16

   29-JUN-15         15

   28-JUN-15         14

   27-JUN-15         13

   26-JUN-15         12

   25-JUN-15         11

   24-JUN-15         10   

Now I need to calculate average age for each day so I added average in the following query:

select trunc(sysdate) - level + 1 DATE ,

   avg(trunc(sysdate) - level + 1 - created_date ) AVERAGE_AGE  

   from items

   connect by trunc(sysdate) - level + 1 - created_date > 0

   group by trunc(sysdate) - level + 1


is this query correct? When I add aggregate function (avg) to this query, it takes 1 hour to retrieve data .When I remove the average function from query it gives result in 2 seconds?What is the possible solution to calculate average without affecting the performance ?Please help

This post has been answered by BluShadow on Jul 6 2015
Jump to Answer

Comments

Frank Kulash

Hi, Suman,

You don't seem to have a problem using SQL or PLSQL, so the SQL/PLSQL forum might not be the best place for this thread.

I'll move it to the   forum for you.

3514477

Ok. No problem. Thank you!

ddf_dba

dfb12658-ba33-44b9-98a1-2b5f3a584262 wrote:

Hi,

I have written excel macro to connect Oracle Database to get the data. It is perfectly working fine.

I have tried below connection strings.

1. con.Open ("User ID=xxx;Password=yyy;Data Source=zz;Provider=oraOLEDB.Oracle")

2.

strCon = "Driver={Microsoft ODBC for Oracle}; " & _

"CONNECTSTRING=(DESCRIPTION=" & _

"(ADDRESS=(PROTOCOL=TCP)" & _

"(HOST=<servername>)(PORT=pno))" & _

"(CONNECT_DATA=(SID=sid))); uid=xxx; pwd=yyy;"

con.Open (strCon)

When I copy this excel file in end user machine it is throwing below exception:

Run-time error '3706':

Provider cannot be found. It may not be properly installed.

So, an end user must have the "Oracle Client" in his machine to run this macro on his machine? or is there any way we can connect to oracle database from Excel without installing oracle client?

Thanks,

Suman

WHERE is it 'perfectly working fine'?  What is apparent is you simply put the Excel workbook on a different computer that doesn't have that OLEDB source configured; just because it's configured on your machine doesn't make it configured on any other computer.

You should try to configure the datasource on the computer you simply dropped that Excel workbook  on and try again. 

David Fitzjarrell

jgarry

I've gotten good examples from the hoopercharles blog, every time I forget how to do what David said.

Edit:  You may also want to use the Oracle drivers.

3514477

Thanks David! Oracle client has been installed on my machine that's why it's working fine.

Can you please let me know how to configure OLEDB Source in users machine? or Please share the link if you have any.

-Regards

Suman

3514477

Thank you! Do you mean I have to install Oracle drivers in users machine? Could you please share more details on the same?

Regards

Suman

jgarry

Like I said, I can never remember how to do it, I just google odbc hoopercharles plus some more words describing exactly what I want. ORA - 12560 with ODBC

ddf_dba

Yes, to make this easy on  you the Oracle  client should be installed on the users machine.  Microsoft makes Oracle drivers but the Oracle client is more reliable.  Without some sort of OLEDB/ODBC driver for Oracle this won't work.

David Fitzjarrell

1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 3 2015
Added on Jul 6 2015
3 comments
271 views