Sql query tunning, need help!
below is the sql that runs very very slow:
SELECT h.STAT_HIST_ID, h.WF_ACTN, h.USR_ACTN,
h.WF_CHNG_DATA, h.USR_CHNG_DATA, h.PRCSG_TM, h.UPD_LOGIN, h.UPD_DT,
RTRIM(c.LAST_NM || ', ' || c.FIRST_NM || ' ' || c.MIDDLE_INITIAL)
FROM WF_STAT_HIST h, CSE_USER_DATA_VW c WHERE h.WRK_ITM_ID = 'GT1MPTG01ATBZP'
AND h.UPD_LOGIN = c.USER_NM (+) ORDER BY h.UPD_DT ;Here is its index:
all_indexes:
SCGDBA WF_STAT_HIST_INDX_ARCH NORMAL SCGDBA WF_STAT_HIST TABLE NONUNIQUE DISABLED IMS_I
SCGDBA PK_WF_STAT_HIST NORMAL SCGDBA WF_STAT_HIST TABLE UNIQUE DISABLED SCG_I
SCGDBA WF_STAT_HIST_IDX1 NORMAL SCGDBA WF_STAT_HIST TABLE NONUNIQUE DISABLED SCG_I
all_ind_columns:
SCGDBA WF_STAT_HIST_INDX_ARCH SCGDBA WF_STAT_HIST ARCHIVEDT 1 7 0 ASC
SCGDBA PK_WF_STAT_HIST SCGDBA WF_STAT_HIST STAT_HIST_ID 1 22 0 ASC
SCGDBA WF_STAT_HIST_IDX1 SCGDBA WF_STAT_HIST WF_ACTN 1 50 50 ASC
SCGDBA WF_STAT_HIST_IDX1 SCGDBA WF_STAT_HIST USR_ACTN 2 50 50 ASCHere is its explain plan:
Plan hash value: 479056202
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 216 | 30456 | 70864 (2)| 00:14:11 |
| 1 | SORT ORDER BY | | 216 | 30456 | 70864 (2)| 00:14:11 |
|* 2 | HASH JOIN OUTER | | 216 | 30456 | 70863 (2)| 00:14:11 |
|* 3 | TABLE ACCESS FULL| WF_STAT_HIST | 216 | 24840 | 70857 (2)| 00:14:11 |
| 4 | TABLE ACCESS FULL| CSE_USER_DATA_VW | 1013 | 26338 | 6 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("H"."UPD_LOGIN"="C"."USER_NM"(+))
3 - filter("H"."WRK_ITM_ID"='GT1MPTG01ATBZP')Could you guys take a look and tell me what the problem might be?
I can`t give you TKPROF, because I got no access to that trace files!
Any thoughts would be appreciated:-)
Edited by: PhoenixBai on Sep 27, 2009 3:13 PM
Pl post the output of the following
select count(*) from wf_stat_hist;
select count (distinct wrk_itm_id) from wf_stat_hist;
select count (distinct upd_login) from wf_stat_hist;Your code is performing poorly because your WHERE clause is searching on a column on WF_STAT_HIST that is not indexed. Hopefully the output of the above code will determine which column (WRK_ITM_ID or UPD_LOGIN) is a better candidate to be indexed on. If neither of these columns is a good candidate, you may have to change the WHERE clause to be more selective rather than do a full table scan as the plan indicates.
HTH
Srini
Similar Messages
-
Simple SQL query..need help
Hi All,
I have a requirement not to show the row, if all columns value is zero. Could someone help me how to do it?
For example:
A B C D E
1 1 5 1 2
1 0 5 0 2
0 9 7 1 0
0 0 0 0 0
From the above example the only last row should not be shown, because all columns value is zero.
Thanks in advance.Another way:
with abc as (select 1 a, 1 b, 5 c, 1 d, 2 e from dual union all
select 1 a, 0 b, 5 c, 0 d, 2 e from dual union all
select 0 a, 9 b, 7 c, 1 d, 0 e from dual union all
select 0 a, 0 b, 0 c, 0 d, 0 e from dual union all
select null a, 0 b, 0 c, 0 d, 0 e from dual)
select a, b, c, d, e
from abc
where (nvl(a, 0), nvl(b, 0), nvl(c, 0), nvl(d, 0), nvl(e, 0)) not in (select 0,0,0,0,0 from dual);
A B C D E
1 1 5 1 2
1 0 5 0 2
0 9 7 1 0 -
Hi All,
Could you please help me out the below SQL query tuning .
Temp table is having 1 Million records
Master table is having 60 Million records
Query :
SELECT B.*,U.ID, SD, LE, LAE
FROM client.Temp B, client.Master
U WHERE U.policyno = B.policyno
AND B.UPFLAG = 0
1. Indexes are created on both email columns and Upflag for both tables.
2. Gathered DBMS Stats for MASTER Table
Data is loading 100k/hour on production .When your query takes too long ...
When your query takes too long ...
HOW TO: Post a SQL statement tuning request - template posting
HOW TO: Post a SQL statement tuning request - template posting -
Select Query Problem-need help
hi,
Help me out from this problem.
Actually i have a table and the data in it as follows:
Department Name Job Name
Accounts Sr. Accountant
Accounts Jr. Accountant
Accounts Cleark
But i dont want the repeated Department Name.. and i want the same output as follows.
Department Name Job Name
Accounts Sr. Accountant
Jr. Accountant
Cleark
Without using sql reports and Sql Plus additional commands.
The same output should come from a sql query only.
Thanks in Advance
Md Anwer AliI shouldn't try to code before I have had at least three cups of coffee. The actual answer is:
select decode(department,lag(department) over (order by department),null,department)
, job
from departments
order by department.
I ran this:
select decode(job,lag(job) over (order by job),null,job)
, ename
from emp
order by job
and got this result:
SQL> /
DECODE(JO ENAME
ANALYST SCOTT
FORD
CLERK SMITH
ADAMS
MILLER
JAMES
MANAGER JONES
CLARK
BLAKE
PRESIDENT KING
SALESMAN ALLEN
MARTIN
TURNER
WARD
14 rows selected. -
SQL Query Performance needed.
Hi All,
I am getting performance issue with my below sql query. When I fired it, It is taking 823.438 seconds, but when I ran query in, it is taking 8.578 seconds, and query after in is taking 7.579 seconds.
SELECT BAL.L_ID, BAL.L_TYPE, BAL.L_NAME, BAL.NATURAL_ACCOUNT,
BAL.LOCATION, BAL.PRODUCT, BAL.INTERCOMPANY, BAL.FUTURE1, BAL.FUTURE2, BAL.CURRENCY, BAL.AMOUNT_PTD, BAL.AMOUNT_YTD, BAL.CREATION_DATE,
BAL.CREATED_BY, BAL.LAST_UPDATE_DATE, BAL.LAST_UPDATED_BY, BAL.STATUS, BAL.ANET_STATUS, BAL.COG_STATUS, BAL.comb_id, BAL.MESSAGE,
SEG.SEGMENT_DESCRIPTION FROM ACC_SEGMENTS_V_TST SEG , ACC_BALANCE_STG BAL where BAL.NATURAL_ACCOUNT = SEG.SEGMENT_VALUE AND SEG.SEGMENT_COLUMN = 'SEGMENT99' AND BAL.ACCOUNTING_PERIOD = 'MAY-10' and BAL.comb_id
in
(select comb_id from
(select comb_id, rownum r from
(select distinct(comb_id),LAST_UPDATE_DATE from ACC_BALANCE_STG where accounting_period='MAY-10' order by LAST_UPDATE_DATE )
where rownum <=100) where r >0)
Please help me in fine tuning above. I am using Oracle 10g database. There are total of 8000 records. Let me know if any other info required.
Thanks in advance.In recent versions of Oracle an EXISTS predicate should produce the same execution plan as the corresponding IN clause.
Follow the advice in the tuning threads as suggested by SomeoneElse.
It looks to me like you could avoid the double pass on ACC_BALANCE_STG by using an analytical function like ROW_NUMBER() and then joining to ACC_SEGMENTS_V_TST SEG, maybe using subquery refactoring to make it look nicer.
e.g. something like (untested)
WITH subq_bal as
((SELECT *
FROM (SELECT BAL.L_ID, BAL.L_TYPE, BAL.L_NAME, BAL.NATURAL_ACCOUNT,
BAL.LOCATION, BAL.PRODUCT, BAL.INTERCOMPANY, BAL.FUTURE1, BAL.FUTURE2,
BAL.CURRENCY, BAL.AMOUNT_PTD, BAL.AMOUNT_YTD, BAL.CREATION_DATE,
BAL.CREATED_BY, BAL.LAST_UPDATE_DATE, BAL.LAST_UPDATED_BY, BAL.STATUS, BAL.ANET_STATUS,
BAL.COG_STATUS, BAL.comb_id, BAL.MESSAGE,
ROW_NUMBER() OVER (ORDER BY LAST_UPDATE_DATE) rn
FROM acc_balance_stg
WHERE accounting_period='MAY-10')
WHERE rn <= 100)
SELECT *
FROM subq_bal bal
, acc_Segments_v_tst seg
where BAL.NATURAL_ACCOUNT = SEG.SEGMENT_VALUE
AND SEG.SEGMENT_COLUMN = 'SEGMENT99';However, the parentheses you use around comb_id make me question what your intention is here in the subquery?
Do you have multiple rows in ACC_BALANCE_STG for the same comb_id and last_update_date?
If so you may want to do a MAX on last_update_date, group by comb_id before doing the analytic restriction.
Edited by: DomBrooks on Jun 16, 2010 5:56 PM -
What SQL query I need for this?
I need to execute a SQL query but I don't know how.
To illustrate it, please take a look at some example data:
ARTICLEID SOLDON
1 2005-12-31
1 2005-11-31
1 2005-10-31
1 2005-09-31
1 2005-08-31
1 2005-07-31
1 2005-06-31
1 2005-05-31
1 2005-04-31
1 2005-03-31
1 2005-02-31
1 2005-01-31
1 2004-12-31
1 2004-11-31
2 2005-12-31
2 2005-11-31
2 2005-10-31
2 2005-09-31 This is a piece of the sales data for the articles (sales history).
Lets assume that today is the date 2005-12-31.
Two requirements for the query:
1. Get the sales data for the last 12 months.
2. Get only the sales data for articles where there is sales data since at least 6 months.
The result in my example should look like this:
ARTICLEID SOLDON
1 2005-12-31
1 2005-11-31
1 2005-10-31
1 2005-09-31
1 2005-08-31
1 2005-07-31
1 2005-06-31
1 2005-05-31
1 2005-04-31
1 2005-03-31
1 2005-02-31
1 2005-01-31 What is the SQL which I need to accomplish this query?To get all the information from the last 12 months
you will have to use date manipulation.
SELECT add_months(sysdate, -12) from
dual;This gives you the date 12 months ago.
So you will have to select your date between then and
the current date.If I do this I will get this data:
ARTICLEID SOLDON
1 2005-12-31
1 2005-11-31
1 2005-10-31
1 2005-09-31
1 2005-08-31
1 2005-07-31
1 2005-06-31
1 2005-05-31
1 2005-04-31
1 2005-03-31
1 2005-02-31
1 2005-01-31
2 2005-12-31
2 2005-11-31
2 2005-10-31
2 2005-09-31 But I want this data:
ARTICLEID SOLDON
1 2005-12-31
1 2005-11-31
1 2005-10-31
1 2005-09-31
1 2005-08-31
1 2005-07-31
1 2005-06-31
1 2005-05-31
1 2005-04-31
1 2005-03-31
1 2005-02-31
1 2005-01-31 I am no native English speaker. What didn't you understand in the two requirements?
Here are my two requirements for the query:
1. Get the sales data for the last 12 months.
2. But get ONLY the sales data for articles where there is sales data since AT LEAST 6 months.
The result can contain as many IDs as you want if the two requirements are met. Its not a trivial SQL statement for me. Please remember that the above data are only for illustration. They are just an example.
There should be a SQL statement for this.
Please tell me if you don't understand my problem. I will try to explain it in a better way if I can. -
Sql query ..need idea to write complex query
Hi there,
I have assigned the task to write a sql query to get the output as the below stored proc does.
In the proc conditions are given with IF statements. I really dont know how to give all the conditions for the period in a single sql query as I'm not much used to sql quries.
Is anyone could help me?
Any suggestions pls . writing complicated query is nightmare. no idea . if possible help me...
create or replace PROCEDURE vpp_station_summary_report (
in_user_id IN VARCHAR2,
in_report_id IN NUMBER,
in_time_from IN vppstation.avi_status_history.status_eff_date%TYPE,
in_time_to IN vppstation.avi_status_history.status_eff_date%TYPE,
result OUT SYS_REFCURSOR)
AS
CURSOR station_loop IS
SELECT ash.station_id,
ash.avi_id,
ash.state_id,
ash.state_eff_date
FROM vppstation.avi_state_history ash
JOIN vpproadside.vpp_report_stations
ON vpp_report_stations.station_id = ash.station_id
AND vpp_report_stations.vpp_report_seq_number = in_report_id
WHERE ash.state_eff_date BETWEEN in_time_from AND in_time_to
ORDER BY ash.station_id,
ash.avi_id,
ash.state_eff_date,
ash.ash_id;
-- cursor to find the 'entry state' i.e. the state the AVI was in AT the time of
-- in_time_from
CURSOR entry_state (
state_station_id vppstation.avi_state_history.station_id%TYPE,
state_avi_id vppstation.avi_state_history.avi_id%TYPE,
state_state_date vppstation.avi_state_history.state_eff_date%TYPE
IS
SELECT ash.state_id
FROM vppstation.avi_state_history ash
WHERE ash.station_id = state_station_id
AND ash.avi_id = state_avi_id
AND ash.state_eff_date < state_state_date
ORDER BY ash.state_eff_date DESC,
ash.ash_id DESC;
current_station_id vppstation.avi_state_history.station_id%TYPE;
current_avi_id vppstation.avi_state_history.avi_id%TYPE;
current_state_id vppstation.avi_state_history.state_id%TYPE;
current_state_eff_date vppstation.avi_state_history.state_eff_date%TYPE;
period_length NUMBER;
next_station_id vppstation.avi_state_history.station_id%TYPE;
next_avi_id vppstation.avi_state_history.avi_id%TYPE;
next_state_id vppstation.avi_state_history.state_id%TYPE;
next_state_eff_date vppstation.avi_state_history.state_eff_date%TYPE;
station_open_total NUMBER;
station_closed_total NUMBER;
station_all_report_total NUMBER;
current_station_name vpproadside.vpp_station_summary.station_name%TYPE;
state_open vppstation.avi_control_state_code.state_id%TYPE;
state_closed vppstation.avi_control_state_code.state_id%TYPE;
state_all_report vppstation.avi_control_state_code.state_id%TYPE;
BEGIN
SELECT state_id
INTO state_open
FROM vppstation.avi_control_state_code
WHERE state_type = 'E'
AND state_active_ind = 'A';
SELECT state_id
INTO state_closed
FROM vppstation.avi_control_state_code
WHERE state_type = 'D'
AND state_active_ind = 'A';
SELECT state_id
INTO state_all_report
FROM vppstation.avi_control_state_code
WHERE state_type = 'S'
AND state_active_ind = 'A';
current_station_id := -1;
current_avi_id := -1;
current_state_id := state_closed;
current_state_eff_date := in_time_from;
station_open_total := 0.0;
station_closed_total := 0.0;
station_all_report_total := 0.0;
-- for starters - ensure that there is report data for all requested stations...
INSERT INTO vpproadside.vpp_station_summary
vpp_report_seq_number,
station_id,
station_name,
ln_number,
lane_name,
station_open,
station_close,
station_all_report,
station_total
SELECT in_report_id,
vrs.station_id,
si.station_name,
l.ln_number,
l.lane_name,
0.0,
0.0,
0.0,
0.0
FROM vpproadside.vpp_report_stations vrs
LEFT OUTER JOIN vpproadside.stations_installed si
ON si.station_id = vrs.station_id
LEFT OUTER JOIN vppstation.lane_name l
ON l.station_id = vrs.station_id
WHERE vrs.vpp_report_seq_number = in_report_id;
-- loop over state history and update information for all stations found
OPEN station_loop;
LOOP
FETCH station_loop
INTO
next_station_id,
next_avi_id,
next_state_id,
next_state_eff_date;
IF station_loop%NOTFOUND THEN
next_station_id := -1;
next_avi_id := -1;
END IF;
-- if station/avi has changed take the end of the report period
IF (next_station_id <> current_station_id)
OR (next_avi_id <> current_avi_id)
THEN
period_length := in_time_to - current_state_eff_date;
ELSE
-- otherwise the start of the next period marks the end of the current period
period_length := next_state_eff_date - current_state_eff_date;
END IF;
-- if we have a real station id then do some work...
IF (current_station_id <> -1) THEN
-- determine which category the period fits to and apply calculation
IF current_state_id = state_open THEN
station_open_total := station_open_total + period_length - 1;
ELSIF current_state_id = state_closed THEN
station_closed_total := station_closed_total + period_length - 1;
ELSIF current_state_id = state_all_report THEN
station_all_report_total := station_all_report_total + period_length - 1;
ELSE
RAISE_APPLICATION_ERROR(-20111, 'Error: found unknown state code on avi_state_history - ' || current_state_id );
END IF;
-- if the station/avi has changed then commit changes to db
IF (next_station_id <> current_station_id)
OR (next_avi_id <> current_avi_id)
THEN
UPDATE vpproadside.vpp_station_summary
SET
station_open = station_open_total,
station_close = station_closed_total,
station_all_report = station_all_report_total
WHERE vpp_report_seq_number = in_report_id
AND station_id = current_station_id
AND ln_number = current_avi_id;
-- reset counts
station_open_total := 0.0;
station_closed_total := 0.0;
station_all_report_total := 0.0;
END IF;
END IF;
-- if we got past the last record then stop processing
EXIT WHEN station_loop%NOTFOUND;
-- if the station/avi is changing, get the state that was 'current' at in_time_from
IF (next_station_id <> current_station_id)
OR (next_avi_id <> current_avi_id)
THEN
current_state_eff_date := in_time_from;
OPEN entry_state (
next_station_id,
next_avi_id,
in_time_from
FETCH entry_state
INTO current_state_id;
IF entry_state%NOTFOUND THEN
current_state_id := state_closed;
END IF;
CLOSE entry_state;
period_length := next_state_eff_date - current_state_eff_date;
IF current_state_id = state_open THEN
station_open_total := station_open_total + period_length;
ELSIF current_state_id = state_closed THEN
station_closed_total := station_closed_total + period_length;
ELSIF current_state_id = state_all_report THEN
station_all_report_total := station_all_report_total + period_length;
ELSE
RAISE_APPLICATION_ERROR(-20111, 'Error: found unknown state code on avi_state_history - ' || current_state_id );
END IF;
END IF;
current_state_id := next_state_id;
current_state_eff_date := next_state_eff_date;
current_station_id := next_station_id;
current_avi_id := next_avi_id;
END LOOP;
CLOSE station_loop;
-- update the totals for the percentage calculation
UPDATE vpproadside.vpp_station_summary
SET
station_total = station_open + station_close+ station_all_report
WHERE vpp_report_seq_number = in_report_id;
-- 'fix' the totals that are still zero to avoid divide by zero errors...
-- note: all the percentages will still come out as zero since the total
-- was zero
UPDATE vpproadside.vpp_station_summary
SET
station_total = 1.0
WHERE vpp_report_seq_number = in_report_id
AND station_total = 0.0;
OPEN result FOR
SELECT station_name "Site Name",
lane_name "Lane Name",
TO_CHAR((station_open / station_total) * 100.0, 'FM990.0999') || '%' "Open %",
TO_CHAR((station_close / station_total) * 100.0, 'FM990.0999') || '%' "Closed %",
TO_CHAR((station_all_report / station_total) * 100.0, 'FM990.0999') || '%' "All Report %"
FROM vpproadside.vpp_station_summary
WHERE vpp_report_seq_number = in_report_id
ORDER BY UPPER(station_name),
UPPER(lane_name);
DELETE FROM vpproadside.vpp_station_summary
WHERE vpp_report_seq_number = in_report_id;
END;Edited by: Indhu Ram on Mar 10, 2010 9:51 AM
Edited by: Indhu Ram on Mar 10, 2010 9:56 AM
Edited by: Indhu Ram on Mar 10, 2010 10:58 AM
Edited by: Indhu Ram on Mar 10, 2010 11:12 AMExactly dont know what you are asking for but I can suggest you some tips here
- If you want to check the condition in SQL query then you can use CASE statement into select clause i.e.
SELECT CASE when table1.a=table2.b then table1.c else table2.c END, ... more case..., table columns...
FROM table1, table2
WHERE
<some conditions>
- If you want to achive same functionality (SELECT only, not UPDATE/INSERT/DELETE) then you can convert the part of same procedure into function and can use the same function into your query by passing the parameters.
something like this
SELECT function_name(parameter1, parameter2....) from dual
Hope this will help -
Simpler reprsentation to SQL Query is needed
Hi all,
I have this SQL Query working goon on the Database but have some errors with Form developer so can anybody simplify it?
SELECT *
FROM (SELECT COUNT (returned_goods.ID) AS "Max Occurence", products.Name
FROM Returned_Goods
JOIN Products
ON returned_goods.productId = products.Id
GROUP BY returned_goods.productId, products.Name
ORDER BY COUNT (returned_goods.ID) DESC)
WHERE ROWNUM = 1;btw, the error encounter me in Forms appears here if anybody can help: [SQL Code not working in Forms Developer without Functions|http://forums.oracle.com/forums/thread.jspa?threadID=842122&tstart=0]
Thanks in advance :)The simpler SQL Staement is
SELECT *
FROM (SELECT COUNT (returned_goods.ID) AS "Max Occurence", products.Name
FROM returned_goods, products
WHERE
Returned_Goods.ProductId = Products.id
GROUP BY returned_goods.productId, products.Name
ORDER BY COUNT (returned_goods.ID) DESC)
WHERE ROWNUM = 1; -
Hi everyone,
I need some help from you because before I update my View Object I need to know one value using a Query like this: SELECT cp FROM cpostais WHERE cp4=1234 AND cp3 = 123. The result of the Query I need to save it on a variable (let's call CP) so I can use it to update my View Object. Something like this: currentRow.setAttribute("CPostal", CP);
I'm using JDeveloper 10.1.2, struts and UIX pages.
Can anyone tell me how can I do this? I need an example or a tutorial.
This is really importante, can anyone help me??
Thanks,
Atena
Message was edited by:
AtenaI tried to use something like this:
ViewObject thirdView = daContext.getBindingContext().getDefaultDataControl().getApplicationModule().findViewObject("S2CodigosPostaisView1");
thirdView.setWhereClauseParam(0, currentRow1.getAttribute("Localidade"));
thirdView.setWhereClauseParam(1, currentRow1.getAttribute("Cp"));
thirdView.setWhereClauseParam(2, currentRow1.getAttribute("Ext"));
thirdView.executeQuery();
Row currentRow3 = thirdView.getCurrentRow();
currentRow3.getAttribute("Id");
But this is not working. :(
Can anyone help me with this????
Thanks,
Atena
Message was edited by:
Atena -
guys i need help please
How do u get points
You can get points when you answer peoples questions. The person asking the question awards the points as they deem appropriate. You can't awared points to yourself. Points aren't worth anything. -
UCCX SQL query assistance needed.
Hello,
I am working on a SQL query that does a lookup of the calling number and the last four digits entered by the customer. If I hard code the lastFourGC variable I am able to return a successful result. If I use $lastFourGC I get the following error.
$callingNumber - Is exactly that, the calling number.
$lastFourGC - Is the last four digits collected from the customer.
SELECT * FROM GiftCardFulfillment WHERE Phone=$callingNumber and Right (GiftCard16DigitNumber, 4)=$lastFourGCTanner,
I had tried that with single and double quotes and it didn't work. I did end up getting it to work by rearranging the order. I'm not sure why that worked, but this is what the query looks like now and it works.
SELECT * FROM [leads].[dbo].[GiftCardFulfillment]
WHERE Right (GiftCard16DigitNumber, 4)=$lastFourGC and Phone=$callingNumber -
Hi,
This is a part of the procedure which is veryyy slow.. Need some suggestions for the below Query to improve performance. The explain plan and all will be correct in PRODUCTION DB.. In that system, I cant have the explain plan to be copied here. So need suggestions by the Query.
Here PB,PBD and PBD1 are very huge tables..
I am unable to paste the Query.. Please help me
Thanks in advance..My car is broken, but I cannot produce it for repairs.
Not sure of your expectations, but this isn't a way where volunteers have been provided any tiny piece of information to suggest any tuning advice.
Please read the linked thread HOW TO: Post a SQL statement tuning request - template posting and post with mentioned information. -
Need SQL query - can anyone help
have a table which contains
Name StartDate EndDate Billable NonBillbale
AAA 01-Jan-2007 31-Mar-2007 2 3
AAA 01-Apr-2007 31-Jun-2007 4 6
BBB 01-Jan-2007 31-Mar-2007 2 3
I need to create a table from this table where the entry would be
Name Jan2007Billable Jan2007NonBillable
feb2007Billable Feb2007NonBillable Mar2007Billable Mar2007NonBillable
April2007Billable April2007NonBillable May2007Billable May2007NonBillable Jun2007Billable Jun2007NonBillable
AAA 2 3 2 3 2 3 4 6 4 6 4 6
BBB 2 3 2 3 2 3
I am very new to database programming... It will be highly appreciated if anyone help me to genrate the queryThis is called a PIVOT and you can see plenty of examples if you search this forum.
An example would be...
SQL> WITH t AS (select 'AAA' AS cust_name, to_date('01-jan-2007','dd-mon-yyyy') as StartDate, to_date('31-mar-2007','dd-mon-yyyy') EndDate, 2 as Billable, 3 as NonBillable from dual union all
2 select 'AAA', to_date('01-apr-2007','dd-mon-yyyy'), to_date('30-jun-2007','dd-mon-yyyy'), 4, 6 from dual union all
3 select 'BBB', to_date('01-jan-2007','dd-mon-yyyy'), to_date('31-mar-2007','dd-mon-yyyy'), 2, 3 from dual)
4 -- END OF TEST DATA - "CUT HERE" --
5 select cust_name
6 ,sum(case when to_date('jan-2007','mon-yyyy') between startdate and enddate then billable else 0 end) as jan2007_b
7 ,sum(case when to_date('jan-2007','mon-yyyy') between startdate and enddate then nonbillable else 0 end) as jan2007_nb
8 ,sum(case when to_date('feb-2007','mon-yyyy') between startdate and enddate then billable else 0 end) as feb2007_b
9 ,sum(case when to_date('feb-2007','mon-yyyy') between startdate and enddate then nonbillable else 0 end) as feb2007_nb
10 ,sum(case when to_date('mar-2007','mon-yyyy') between startdate and enddate then billable else 0 end) as mar2007_b
11 ,sum(case when to_date('mar-2007','mon-yyyy') between startdate and enddate then nonbillable else 0 end) as mar2007_nb
12 ,sum(case when to_date('apr-2007','mon-yyyy') between startdate and enddate then billable else 0 end) as apr2007_b
13 ,sum(case when to_date('apr-2007','mon-yyyy') between startdate and enddate then nonbillable else 0 end) as apr2007_nb
14 ,sum(case when to_date('may-2007','mon-yyyy') between startdate and enddate then billable else 0 end) as may2007_b
15 ,sum(case when to_date('may-2007','mon-yyyy') between startdate and enddate then nonbillable else 0 end) as may2007_nb
16 ,sum(case when to_date('jun-2007','mon-yyyy') between startdate and enddate then billable else 0 end) as jun2007_b
17 ,sum(case when to_date('jun-2007','mon-yyyy') between startdate and enddate then nonbillable else 0 end) as jun2007_nb
18 from t
19 group by cust_name
20 order by 1
21 /
CUS JAN2007_B JAN2007_NB FEB2007_B FEB2007_NB MAR2007_B MAR2007_NB APR2007_B APR2007_NB MAY2007_B MAY2007_NB JUN2007_B JUN2007_NB
AAA 2 3 2 3 2 3 4 6 4 6 4 6
BBB 2 3 2 3 2 3 0 0 0 0 0 0
SQL> -
Different Result for SQL and PL/SQL query..need a bit of help
If i just run:
SELECT COUNT(*) from DB where (CLOSE_TIME-OPEN_TIME<5) and (CLOSE_TIME-OPEN_TIME>=1) and OPEN_TIME>'1/1/2011';
It works great.
If I run
BEGIN
SELECT COUNT(*) from DB where (CLOSE_TIME-OPEN_TIME<5) and (CLOSE_TIME-OPEN_TIME>=1) and OPEN_TIME>'1/1/2011';
END
I get the following error.
ORA-04052: error occurred when looking up remote object [email protected]
ORA-00604: error occurred at recursive SQL level 3
ORA-06552: PL/SQL: Compilation unit analysis terminated
ORA-06553: PLS-488: invalid variable declaration: object 'NUMBER' must be a type or subtype
ORA-02063: preceding 2 lines from SMGLINKHi,
Whenever you have a problem, post a complete test script that people can run to re-create the problem and test their ideas. Include CREATE TABLE and INSERT statements for any or your own tables needed.
Post the results you want from that sample data.
Always say which version of Oracle you're using.
bostonmacosx wrote:
If i just run:
SELECT COUNT(*) from DB where (CLOSE_TIME-OPEN_TIME<5) and (CLOSE_TIME-OPEN_TIME>=1) and OPEN_TIME>'1/1/2011';If OPEN_TIME is a DATE, then don't try to compare it to a string, such as '1/1/2011'. (This is not related to the present problem, only a future one.)
It works great.
If I run
BEGIN
SELECT COUNT(*) from DB where (CLOSE_TIME-OPEN_TIME<5) and (CLOSE_TIME-OPEN_TIME>=1) and OPEN_TIME>'1/1/2011';
END
I get the following error.
ORA-04052: error occurred when looking up remote object [email protected]
ORA-00604: error occurred at recursive SQL level 3
ORA-06552: PL/SQL: Compilation unit analysis terminated
ORA-06553: PLS-488: invalid variable declaration: object 'NUMBER' must be a type or subtype
ORA-02063: preceding 2 lines from SMGLINKI would expect a different error "PLS_00428: an INTO clause is expected". -
A Oracle sql query is needed, please help me out
Hi,
I have a table similar to scott.emp table and i should get first three recently joined employees salaries according to their joined date from each deptno
output should like this:
deptno firstempsalary secondemplsal thirdemploysalary
10 xxx xxx xxxx
20 xx xx
30 xx xxx xxxx
40 xx
50 xx xxxx xxx
60 xx xx
70 xx
it means that 70 dept having only one employee and 20 dept having only two employees and 10 dept having more than three employees but we should get only three recently joined employees salaries from 10 dept.
Hope this is clear .. please give me a query to get this info ..
oracle is 10g version
great thanks in advance.select deptno,
max(case jd_rank when 1 then salary end) firstempsal,
max(case jd_rank when 2 then salary end) secondempsal,
max(case jd_rank when 3 then salary end) thirdempsal
from
(select salary, deptno, row_number() over(partition by deptno order by join_date desc) jd_rank from emp)
group by deptno
Problem here is what to do when employees have the same join date? This example will arbitrarily choose their order and if, for example, three employees share the same (most recent) join date then they will arbitrarily be classed as "first", "second" and "third". If four employees join at the same time, one of them will be ignored at random!
Edited by: user10548434 on 03-Dec-2008 06:27
Maybe you are looking for
-
How to remove 'MacKeeper' popup ad?
Somewhere during a surfing experience, t encountered the popup ad for 'MacKeeper'. Along with that problem i found that my homepage upon opening Safari came up to 'QuickSearch' from months using 'Google'. I do not know where of why I all of a sudde
-
Poor results when photos edited but look good when 'done'
HI, Apple seems to have ALMOST fixed the problems with the editing part of iPhoto. But pictures are still not very sharp in the edit mode. However when I finish editing and click done the pictures look better. But they really look GREAT when I email
-
Save a script or copy a script with SQL Script editor
Hello, I try to rename a script in the script repository I've loaded using the upload command. I enter the new script name in the text field and save with the new name as the pdf doc explain (page 18-8). It does not create any new script in the repos
-
How do I lock my iMac? (So that only I can use it)
I'm going to be having someone I don't know come in my house and take care of my cats over the holidays. How do I make sure he can't get in to my computer?
-
TS3899 cannot get mail mail server is not responding
Ipad and iphone will not get mail. message says server is not responding