Help with Query
Hi All,
our auditors wanted a report, basically FBL3N + the vendor name, i tried using
tcode O758 to just add the vendor number there but looks like i have to add
in BSEG inorder for it to appear in O758.
so i created a query which does this.
I was able to link BSIS to BSISK with BELNR and BSIK to LFA1 with LIFNR
to get the open items
to get the cleared items i linked BSIS to BSAK with BELRN and BSAK to
LFA1 with LIFNR. This works fine but i want to combinbe these reports
so that we get the open and cleared items both in the same report.
BELNR first sits in BSIK and once its cleared it goes to BSAK and theres no
AUGBL in BSIK but its there in BSAK so not sure how to link these tables.
Linking BELNR from BSIS to BSAK and BSIK doesnt work .
please advice........
looks like i havent figured it out fully, i am not getting the vendor name to show
only the vendor number, have to test it fully . will let you know Ravi
Similar Messages
-
Help with query calculations (recursive)
Hi All,
I want some help with a query using a base rate and the result use in the next calculation year.
Here an example:
create table rate_type(
rate_type_id number,
rate_desc nvarchar2(50),
rate_base_year number
insert into rate_type(rate_type_id, rate_desc, rate_base_year) values (1, 'Desc1', 4.6590);
insert into rate_type(rate_type_id, rate_desc, rate_base_year) values (2, 'Desc2', 4.6590);
create table rates (
rate_type_id number
rate_year number,
rate_value number
insert into rates(rate_type_id, rate_year, rate_value) values (1, 2012, 1.2);
insert into rates(rate_type_id, rate_year, rate_value) values (1, 2013, 1.3);
insert into rates(rate_type_id, rate_year, rate_value) values (1, 2014, 1.4);
insert into rates(rate_type_id, rate_year, rate_value) values (2, 2012, 1.2);
insert into rates(rate_type_id, rate_year, rate_value) values (2, 2013, 1.3);
insert into rates(rate_type_id, rate_year, rate_value) values (2, 2014, 1.4);The calculation for the first year should be the base rate of the rate type. The next year should use the result of the previous year and so on.
The result of my sample data is:
2012 = 4.659 + 1.2 + 4.659 * (1.2 * 0.01) = 5.9149
2013 = 5.9149 + 1.3 + 5.9149 * (1.3 * 0.01) = 7.1859
2014 = 7.1859 + 1.4 + 7.1859 * (1.4 * 0.01) = 8.4721Query result:
NAME 2012 2013 2014
Desc1 5.9149 7.1859 8.4721
Desc2 XXXX XXX XXXX
How can I do this in one select statement? Any ideas?
Thanks!Assuming you are on 11.2:
with t as (
select a.rate_type_id,
rate_desc,
rate_year,
rate_base_year,
rate_value,
count(*) over(partition by a.rate_type_id) cnt,
row_number() over(partition by a.rate_type_id order by rate_year) rn
from rate_type a,
rates b
where a.rate_type_id = b.rate_type_id
r(
rate_type_id,
rate_desc,
rate_year,
rate_base_year,
rate_value,
cnt,
rn,
result
) as (
select rate_type_id,
rate_desc,
rate_year,
rate_base_year,
rate_value,
cnt,
rn,
rate_base_year + rate_value + rate_base_year * rate_value * 0.01 result
from t
where rn = 1
union all
select t.rate_type_id,
t.rate_desc,
t.rate_year,
t.rate_base_year,
t.rate_value,
t.cnt,
t.rn,
r.result + t.rate_value + r.result * t.rate_value * 0.01 result
from r,
t
where t.rate_type_id = r.rate_type_id
and t.rn = r.rn + 1
select *
from (
select rate_desc name,
rate_year,
result
from r
where rn <= cnt
pivot (sum(result) for rate_year in (2012,2013,2014))
order by name
NAME 2012 2013 2014
Desc1 5.914908 7.2918018 8.79388703
Desc2 5.914908 7.2918018 8.79388703
SQL> Obviously pivoting assumes you know rate_year values upfront. If not, then without pivoting:
with t as (
select a.rate_type_id,
rate_desc,
rate_year,
rate_base_year,
rate_value,
count(*) over(partition by a.rate_type_id) cnt,
row_number() over(partition by a.rate_type_id order by rate_year) rn
from rate_type a,
rates b
where a.rate_type_id = b.rate_type_id
r(
rate_type_id,
rate_desc,
rate_year,
rate_base_year,
rate_value,
cnt,
rn,
result
) as (
select rate_type_id,
rate_desc,
rate_year,
rate_base_year,
rate_value,
cnt,
rn,
rate_base_year + rate_value + rate_base_year * rate_value * 0.01 result
from t
where rn = 1
union all
select t.rate_type_id,
t.rate_desc,
t.rate_year,
t.rate_base_year,
t.rate_value,
t.cnt,
t.rn,
r.result + t.rate_value + r.result * t.rate_value * 0.01 result
from r,
t
where t.rate_type_id = r.rate_type_id
and t.rn = r.rn + 1
select rate_desc name,
rate_year,
result
from r
where rn <= cnt
order by name,
rate_year
NAME RATE_YEAR RESULT
Desc1 2012 5.914908
Desc1 2013 7.2918018
Desc1 2014 8.79388703
Desc2 2012 5.914908
Desc2 2013 7.2918018
Desc2 2014 8.79388703
6 rows selected.
SQL> SY. -
Hello, I have the following query that I'm running in Oracle SQL Developer 1.2.1
WITH group_by_4_column_results AS
(SELECT m_atschunk.employee AS employee,
SUM(CASE
WHEN
M_ATSCHUNK.rolloffDaysCount = '108545043' AND m_atschunk.infractiondate BETWEEN SYSDATE - 365 AND SYSDATE THEN 1 ELSE 0 END) as rolloffs,
M_ATSCHUNK.INFRACTIONDATE + 365 as infractionDate
FROM M_ATSCHUNK
group by employee, infractionDate, rolloffDaysCount
SELECT g4.*,
SUM (rolloffs) OVER (PARTITION BY employee) AS total_rolloffs
FROM group_by_4_column_results g4
It will output the key elements of what I need. But where it sums up the 'total_rolloffs', I need to add that number back into the infractiondate column. Any help would be greatly appreciated.
CREATE TABLE M_ATSCHUNK
(EMPLOYEE varchar(50),
ROLLOFFDAYSCOUNT varchar(3),
INFRACTIONDATE date)
INSERT INTO M_ATSCHUNK (EMPLOYEE, ROLLINGOFFDAYSCOUNT, INFRACTIONDATE)
VALUES ('PHIL','YES', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('PHIL','YES', (to_date('2010/01/02 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('PHIL','YES', (to_date('2010/01/03 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('PHIL','YES', (to_date('2010/01/04 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('PHIL','YES', (to_date('2010/01/05 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('PHIL','NO', (to_date('2010/02/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('PHIL','NO', (to_date('2010/03/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','YES', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','YES', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','YES', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','NO', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','NO', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','NO', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss'))),
VALUES ('NIKI','NO', (to_date('2010/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss')))Phil3061 wrote:
I need to add that number back into the infractiondate column.Well, in general you need to use UPDATE or better MERGE. Howebver, your data sample does not show any rollofs:
SQL> SELECT * FROM M_ATSCHUNK;
EMPLOYEE ROL INFRACTIO
PHIL YES 01-JAN-10
PHIL YES 02-JAN-10
PHIL YES 03-JAN-10
PHIL YES 04-JAN-10
PHIL YES 05-JAN-10
PHIL NO 01-FEB-10
PHIL NO 01-MAR-10
NIKI YES 01-JAN-10
NIKI YES 01-JAN-10
NIKI YES 01-JAN-10
NIKI NO 01-JAN-10
EMPLOYEE ROL INFRACTIO
NIKI NO 01-JAN-10
NIKI NO 01-JAN-10
NIKI NO 01-JAN-10
14 rows selected.
SQL> WITH group_by_4_column_results AS
2 (SELECT m_atschunk.employee AS employee,
3 SUM(CASE
4 WHEN
5 M_ATSCHUNK.rolloffDaysCount = '108545043' AND m_atschunk.infractiondate BETWEEN SYSDATE - 365 AND SYSDATE THEN 1 ELSE 0 END) as
rolloffs,
6 M_ATSCHUNK.INFRACTIONDATE + 365 as infractionDate
7 FROM M_ATSCHUNK
8 group by employee, infractionDate, rolloffDaysCount
9 )
10 SELECT g4.*,
11 SUM (rolloffs) OVER (PARTITION BY employee) AS total_rolloffs
12 FROM group_by_4_column_results g4
13 /
EMPLOYEE ROLLOFFS INFRACTIO TOTAL_ROLLOFFS
NIKI 0 01-JAN-11 0
NIKI 0 01-JAN-11 0
PHIL 0 01-JAN-11 0
PHIL 0 02-JAN-11 0
PHIL 0 03-JAN-11 0
PHIL 0 04-JAN-11 0
PHIL 0 05-JAN-11 0
PHIL 0 01-FEB-11 0
PHIL 0 01-MAR-11 0
9 rows selected.
SQL> So adjust data sample and based on it tell us what are the expected results.
SY. -
Need help with query that can look data back please help.
hi guys i have a table like such
CREATE TABLE "FGL"
"FGL_GRNT_CODE" VARCHAR2(60),
"FGL_FUND_CODE" VARCHAR2(60),
"FGL_ACCT_CODE" VARCHAR2(60),
"FGL_ORGN_CODE" VARCHAR2(60),
"FGL_PROG_CODE" VARCHAR2(60),
"FGL_GRNT_YEAR" VARCHAR2(60),
"FGL_PERIOD" VARCHAR2(60),
"FGL_BUDGET" VARCHAR2(60)
)and i have a data like such
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','1','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','2','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7470','4730','02','10','2','200');I bascially need to get the total of the budget column. however its not as simple as it sound(well atleast not for me.) the totals carry over to the new period. youll noticed the you have a period column. basically what im saying is that
fgl_grant_year 10 period 1 = for account 7600 its $100 and $100 for period 2 you see 100 dollars again this is not to be added this is the carried over balance. which remains $100.
so im trying to write a query that basically does the following.
im given a period for the sake of this example lets say period 1 i get nothing else. I have to find the greates grant year grab the amount for period 14(which is the total from the previous year) and add it to the amount of the current period. in this case period 1 grnt_year 11
so the expected outcome should be $700
240055 240055 7240 4730 02 10 14 200
240055 240055 7600 4730 02 10 14 100
240055 240055 7600 4730 02 11 1 400keep in mind that im not given a year just a period.
any help that you guys can offer would be immensely appreciated. I have been trying to get this to work for over 3 days now.
finally broke down and put together this post
Edited by: mlov83 on Sep 14, 2011 8:48 PMFrank
wondering if you can help me modify this sql statement that you provided me with .
table values have been modified a bit.
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','00','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7200','4730','02','10','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7600','4730','02','10','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','2','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','11','2','600');i need to take one more thing into consideration. if the greatest year has a value on period 00 i need to ignore the period 14 and the current period total would be
the current period +(current period - greatest year 00)
hope that makes sense so in other words with the new data above. if i was querying period two of grant year 11. i would end up with $800
because the greatest year is 11 it contains a period 0 with amount of $400 so my total should be
period 2 amount $ 600
period 0 amount $ 400 - period 2 amount of $600 = 200
600+200 = $800
if i query period 1 of grant 360055 i would just end up with 800 of grnt year 10.
i have tried to modify that query you supplied to me with no luck. I have tried for several day but im embarrased to say i just can get it to do what im trying to do .
can you please help me out.
Miguel -
Need Help With Query Using Aggregation
If I have a table, defined like this:
CREATE TABLE range_test
range_id NUMBER(20) NOT NULL,
grade CHAR(1) NOT NULL,
lower_bound_of_range NUMBER(5,2) NOT NULL,
upper_bound_of_range NUMBER(5,2) NOT NULL,
received_date_time_stamp TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL
And I wanted to query the table to find the range associated with the last inserted row for each 'grade' (e.g. 'A', 'B', 'C', etc), how would I go about that?
I want something like the following, but I know that this won't work right:
SELECT
grade,
lower_bounding_of_range,
upper_bounding_of_range,
max(received_date_time_stamp)
FROM
range_test GROUP BY received_date_time_stamp;
Thanks for your help. . .I'm frustrating myself with this one and I think it should be possible without having to use PL/SQL (i.e. SQL aggregate functions or sub-queries should work).Perhaps something along the lines of...
SQL> ed
Wrote file afiedt.buf
1 select deptno, empno, ename, hiredate
2 from emp
3* order by deptno, empno
SQL> /
DEPTNO EMPNO ENAME HIREDATE
10 7782 CLARK 09-JUN-1981 00:00:00
10 7839 KING 17-NOV-1981 00:00:00
10 7934 MILLER 23-JAN-1982 00:00:00
20 7369 SMITH 17-DEC-1980 00:00:00
20 7566 JONES 02-APR-1981 00:00:00
20 7788 SCOTT 19-APR-1987 00:00:00
20 7876 ADAMS 23-MAY-1987 00:00:00
20 7902 FORD 03-DEC-1981 00:00:00
30 7499 ALLEN 20-FEB-1981 00:00:00
30 7521 WARD 22-FEB-1981 00:00:00
30 7654 MARTIN 28-SEP-1981 00:00:00
30 7698 BLAKE 01-MAY-1981 00:00:00
30 7844 TURNER 08-SEP-1981 00:00:00
30 7900 JAMES 03-DEC-1981 00:00:00
14 rows selected.
SQL> ed
Wrote file afiedt.buf
1 select deptno, empno, ename, hiredate
2 from (
3 select deptno, empno, ename, hiredate
4 ,row_number() over (partition by deptno order by hiredate desc) as rn
5 from emp
6 )
7 where rn = 1
8* order by deptno, empno
SQL> /
DEPTNO EMPNO ENAME HIREDATE
10 7934 MILLER 23-JAN-1982 00:00:00
20 7876 ADAMS 23-MAY-1987 00:00:00
30 7900 JAMES 03-DEC-1981 00:00:00
SQL> -
Hi,
Good day everyone! I need help writing a query. I have this table with the following data in them...
ACCT_CODE FSYR YTD_AMT
A123 11 100
A456 11 200
A123 10 50
A456 10 100I want the output to look like this:
ACCT_CODE CURRENT_YEAR(11) PRIOR_YEAR(10)
A123 100 50
A456 200 100The user will input the fiscal year and based on that input, I want to get the prior year value as well.
Thank you for all your help!!
Edited by: user5737516 on Jun 29, 2011 6:48 AM
Edited by: user5737516 on Jun 29, 2011 6:50 AMuser5737516 wrote:
Hi,
Good day everyone! I need help writing a query. I have this table with the following data in them...
ACCT_CODE FSYR YTD_AMT
A123 11 100
A456 11 200
A123 10 50
A456 10 100
I want the output to look like this:
ACCT_CODE CURRENT_YEAR PRIOR_YEAR
A123 100 50
A456 200 100
The user will input the fiscal year and based on that input, I want to get the prior year value as well.
Thank you for all your help!!what is prior year? -
Query off of Oracle using WinSql - Need help with query
I am trying to query off of Oracle using program WinSql.
I have a table(tticpr200110) that has the following sample data:
ITEM CODE T$AMNT
23500076 ACL .0049
23500076 APM 0
23500076 APO .0093
23500076 EXP .0001
23500076 RES .0072
and what I want it to look like is:
ITEM ACL APM APO EXP RES
23500076 0.0049 0 0.0093 0.0001 0.0072
(actually I need the last 2 columns added together to be MATL-but can deal with that down the road).
Seems simple enough, but I don't know to put into the columns.
Any help would be GREATLY appreciated as soon as possible would be even better.My table - tticpr200110 when it runs I get the following sample data for part number 23500076:
The first coloumn ITEM is the part number.
The second column CODE is 1 of 5 different cost codes
The third column is the cost for that code for that part.
ITEM CODE AMNT
23500076 ACL 0.0049
23500076 APM 0.0000
23500076 APO 0.0093
23500076 EXP 0.0001
23500076 RES 0.0072
I want to make a query that makes the data look like this:
ITEM ACL APM APO EXP RES
23500076 0.0049 0.0000 0.0093 0.0001 0.0072
(similar to a pivot table in excel or acess)
I hope this helps better.
Thanks! -
Need help with Query to determine Credit Memos and Invoices
Hi All
Thanks for all the help here.
I need a query to determine any credits or invoices issued within a given period.
OINV and ORIN with UNION ALL?
Please advise any help.
Thank you!Hi Daniel,
Please check below Query.
SELECT T0.[DocNum] as 'Invice No', T0.[DocDate] as 'Invoice Date', T0.[CardName] as 'Invoiced Customer', T3.[DocNum] as 'Credit Memo No', T3.[DocDate] as 'Credit Mamo Date', T3.[CardName] as 'Credit Memo Customer' FROM OINV T0 LEFT JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry] LEFT JOIN RIN1 T2 ON T2.[BaseEntry] = T1.[DocEntry] AND T2.[BaseLine] = T1.[LineNum] LEFT JOIN ORIN T3 ON T2.[DocEntry] = T3.[DocEntry] WHERE T0.[DocDate] >=[%3] AND T0.[DocDate] <=[%4]
Hope this helps
Regards::::
Atul Chakraborty -
[Oracle 8i] Help with query performance
The following query is running VERY slowly for me:
SELECT oord.part_nbr
, sopn.ord_nbr
, sopn.sub_ord_nbr
, sopn.major_seq_nbr
, sopn.wctr_id
, sopn.oper_desc
, SUM(pact.act_dlrs_earned+pact.act_brdn_dls_earned+pact.tool_dlrs_earned+pact.act_fix_brdn_dls_ea)
, pact.activity_date
FROM PACT pact
, SOPN sopn
, OORD oord
WHERE pact.order_nbr = sopn.ord_nbr AND
pact.maj_seq_nbr = sopn.major_seq_nbr AND
sopn.sub_ord_nbr = pact.sub_order_nbr AND
sopn.ord_nbr = oord.ord_nbr AND
sopn.sub_ord_nbr = oord.sub_ord_nbr AND
pact.activity_date >= ? AND
sopn.rework_ind = 'N' AND
(oord.part_nbr, sopn.major_seq_nbr, sopn.wctr_id)
NOT IN (
SELECT rout.doc_nbr
, rout.major_seq_nbr
, rout.wctr_id
FROM ROUT rout
WHERE (rout.begn_eff_dt <=SYSDATE) AND
(rout.end_eff_dt >SYSDATE) AND
(rout.po_rework_ind ='N')
GROUP BY oord.part_nbr
, sopn.ord_nbr
, sopn.sub_ord_nbr
, sopn.major_seq_nbr
, sopn.wctr_id
, sopn.oper_desc
, pact.activity_dateI sent a request off to my IT department (specifically asking for the explain plan and tkprof, as described in the [main post on this topic|http://forums.oracle.com/forums/thread.jspa?threadID=501834] ), and they replied with a screen shot of the 'explain plan' the tool they use (Toad) provides.
!http://temp-sample.webs.com/explain_plan.jpg!
I don't know if anyone can help me based off this, since I know it's not really what the main post says to provide, but it's all I was given.
My IT department also made a few changes to my original query (see below) and told me it got rid of one of the full scans of the PACT table, but they aren't sure why it helped, and I have to say I'm suspect of any fixes where it's not understood why it helped.
SELECT oord.part_nbr
, sopn.ord_nbr
, sopn.sub_ord_nbr
, sopn.major_seq_nbr
, sopn.wctr_id
, sopn.oper_desc
, SUM(pact.act_dlrs_earned+pact.act_brdn_dls_earned+pact.tool_dlrs_earned+pact.act_fix_brdn_dls_ea)
, pact.activity_date
FROM PACT pact
, SOPN sopn
, OORD oord
WHERE sopn.ord_nbr = pact.order_nbr AND
sopn.major_seq_nbr = pact.maj_seq_nbr AND
pact.sub_order_nbr = sopn.sub_ord_nbr AND
oord.ord_nbr = sopn.ord_nbr AND
oord.sub_ord_nbr = sopn.sub_ord_nbr AND
(pact.activity_date >= ?) AND
'N' = sopn.rework_ind AND
pact.order_nbr = oord.ord_nbr AND
oord.sub_ord_nbr = pact.sub_order_nbr AND
(oord.part_nbr, pact.maj_seq_nbr, sopn.wctr_id) NOT IN
SELECT /*+ INDEX_JOIN(ROUT) */ rout.doc_nbr
, rout.major_seq_nbr
, rout.wctr_id
FROM ROUT rout
WHERE rout.begn_eff_dt <= SYSDATE AND
rout.end_eff_dt > SYSDATE AND
'N' = rout.po_rework_ind
GROUP BY oord.part_nbr
, sopn.ord_nbr
, sopn.sub_ord_nbr
, sopn.major_seq_nbr
, sopn.wctr_id
, sopn.oper_desc
, pact.activity_dateAny help on this would be appreciated... when I run this (right now) for 2-3 months of data, it takes a minimum of 3 hours to complete, and I'll eventually need to run this for up to a year's worth of data.Hi,
Well, let's see.
You get 156 rows returned using IN and 121 rows using exists.
You need identify the 'missing records' and conclude if that's correct or not, I'm not able to do that from remote, without knowing your data or system.
It would be helpful if we could see cost and cardinalities, you (or your IT dept.) can get them easily be running the queries from your SQL*Plus prompt.
Type
SET AUTOTRACE TRACEONLYbefore running the queries.
That gives you the explain plan and additional statistics (sorts in memory and sorts to disk).
Since you use a group by, and you're on 8i can you also post results of these queries:
select banner from v$version;
select name, value, isdefault from v$parameter where name like '%area%';Finally, does below query give you a different plan?
select oord.part_nbr
, oord.ord_nbr
, oord.sub_ord_nbr
, pact.major_seq_nbr
, sopn.wctr_id
, sopn.oper_desc
, sum(pact.act_dlrs_earned + pact.act_brdn_dls_earned + pact.tool_dlrs_earned + pact.act_fix_brdn_dls_ea)
, pact.activity_date
from oord oord
, pact pact
, sopn sopn
where oord.ord_nbr = pact.order_nbr
and oord.sub_ord_nbr = pact.sub_order_nbr
and oord.ord_nbr = sopn.ord_nbr
and oord.sub_ord_nbr = sopn.sub_ord_nbr
and sopn.major_seq_nbr = pact.maj_seq_nbr
and (pact.activity_date >= ?)
and 'N' = sopn.rework_ind
and (oord.part_nbr, pact.maj_seq_nbr, sopn.wctr_id) not in ( select rout.doc_nbr
, rout.major_seq_nbr
, rout.wctr_id
from rout rout
where rout.begn_eff_dt <= sysdate
and rout.end_eff_dt > sysdate
and 'N' = rout.po_rework_ind)
group by oord.part_nbr
, oord.ord_nbr
, oord.sub_ord_nbr
, pact.major_seq_nbr
, sopn.wctr_id
, sopn.oper_desc
, pact.activity_date -
Help with query rewrite and materialized views
Hello everybody,
I'm currently learning how to use Oracle (10G Enterprise) and in particular, Materialized Views.
I seem to have a problem making the optimizer use a materialized view. I have already set the OPTIMIZER_MODE, QUERY_REWRITE_ENABLED and QUERY_REWRITE_INTEGRITY as needed.
I need to create a materialized view for the following query:
Q1:
SELECT PS_SUPPKEY, PS_PARTKEY, PS_SUPPCOST
FROM PARTSUPPLIER E, PART WHERE PS_PARTKEY=P_PARTKEY and (lower(P_COMMENT) LIKE ''_o_a\%'' or lower(P_COMMENT) LIKE ''_o_u\%'')
and PS_SUPPCOST =
(SELECT min( PS_SUPPCOST)
FROM PARTSUPPLIER I
WHERE E.PS_PARTKEY=I.PS_PARTKEY)'
I created it using the following code:
CREATE MATERIALIZED VIEW mv_q1
ENABLE QUERY REWRITE
AS SELECT PS_SUPPKEY, PS_PARTKEY, PS_SUPPCOST
FROM PARTSUPPLIER E JOIN PART ON (PS_PARTKEY=P_PARTKEY)
WHERE lower(P_COMMENT) LIKE '_o_a%' or lower(P_COMMENT) LIKE '_o_u%'
and PS_SUPPCOST=
(SELECT min( PS_SUPPCOST)
FROM PARTSUPPLIER I
WHERE E.PS_PARTKEY=I.PS_PARTKEY);
I have created the statistics using:
execute dbms_stats.gather_table_stats('frandres',' mv_q1');
execute dbms_stats.gather_table_stats('frandres','PARTSUPPLIER');
execute dbms_stats.gather_table_stats('frandres','PART');
Both partsupplier and part are tables and not views.
When executing Q1, the plan does not use the materialized view. Furthermore, when using explain rewrite:
DECLARE
qrytxt VARCHAR2(3000) := 'SELECT PS_SUPPKEY, PS_PARTKEY, PS_SUPPCOST
FROM PARTSUPPLIER E, PART WHERE PS_PARTKEY=P_PARTKEY and (lower(P_COMMENT) LIKE ''_o_a\%'' or lower(P_COMMENT) LIKE ''_o_u\%'')
and PS_SUPPCOST =
(SELECT min( PS_SUPPCOST)
FROM PARTSUPPLIER I
WHERE E.PS_PARTKEY=I.PS_PARTKEY)';
BEGIN
dbms_mview.EXPLAIN_REWRITE
(qrytxt,'MV_Q1','MV_Q1');
END;
I get the following message:
MESSAGE
QSM-01150: query did not rewrite
QSM-01263: query rewrite not possible when query references a dictionary table o
r view
QSM-01219: no suitable materialized view found to rewrite this query
What I can't understand is why it says I am referencing the dictionary or a view?
If I remove the (lower(P_COMMENT) LIKE ''_o_a\%'' or lower(P_COMMENT) LIKE ''_o_u\%'') condition to the query (using the same materialized view), I get the following message from EXPLAIN_REWRITE:
MESSAGE
QSM-01150: query did not rewrite
QSM-01219: no suitable materialized view found to rewrite this query
Which is reasonable.
I don't know if the like condition is messing up my materialized view. Can anyone please help?
Thanks a lot in advance.
Edited by: user12072111 on Oct 29, 2009 9:43 PMBingo!
The 10.2.0.3 patch set is supposed to fix this ( [List of bugs fixed (MVs)|http://www.dbatools.net/doc/bug10203.html#MVIEW] )
In particular:
5052568 Query rewrite does not work for SQL with LIKE clause.
Thank you very much for your message!
The downside is that I'm only using Oracle for educational purposes and consequently have no Metalink id, so I can't install the patch. Thanks a lot though! -
Need help with query for converting columns to rows
Hello,
I know this is a very common question asked in the forum. I have searched regading this, i did find some threads, but i was not able to achieve what i require from the answers posted. So anybody please help me.
I have a table which is having multiple columns as follows:
Insert into table_1 (X,Y,Z,A,B,C,D,E,F,G,H,I) values (0,0,2,0,0,1,3,0,0,0,0,0);I want to convert the result into a two column, multiple rows i.e., I want the result as follows:
Col1 Col2
X 0
Y 0
Z 2
A 0
B 0
C 1
D 3
E 0
F 0
G 0
H 0
I 0Please anybody help me in writing the query for this..Is this what you are expecting:
SQL> WITH T AS
2 (
3 SELECT 0 X, 0 Y, 2 Z, 0 A, 0 B, 1 C, 3 D, 0 E, 0 F, 0 G, 0 H, 0 I FROM DUAL
4 )
5 SELECT 'X' col1, X col2 FROM T
6 UNION ALL
7 SELECT 'Y' col1, Y col2 FROM T
8 UNION ALL
9 SELECT 'Z' col1, Z col2 FROM T
10 UNION ALL
11 SELECT 'A' col1, A col2 FROM T
12 UNION ALL
13 SELECT 'B' col1, B col2 FROM T
14 UNION ALL
15 SELECT 'C' col1, C col2 FROM T
16 UNION ALL
17 SELECT 'D' col1, D col2 FROM T
18 UNION ALL
19 SELECT 'E' col1, E col2 FROM T
20 UNION ALL
21 SELECT 'F' col1, F col2 FROM T
22 UNION ALL
23 SELECT 'G' col1, G col2 FROM T
24 UNION ALL
25 SELECT 'H' col1, H col2 FROM T
26 UNION ALL
27 SELECT 'I' col1, I col2 FROM T
28 /
C COL2
X 0
Y 0
Z 2
A 0
B 0
C 1
D 3
E 0
F 0
G 0
H 0
C COL2
I 0
12 rows selected. -
Can anyone plese tell me or guide me how to write the query to show the 5 lastest opening balance from the A/R invoice on the specific BP partner?
Example
The box to input "BP partner"
and then show,
Lastest transaction | Ship date | Invoice number | Balance Amount | Culmulative
1
2
3
4
5
Your help will be very very appreciated.This is very good but I would like to show only the top 5 remaining invoice. I think that I have to combine it with JDT1. I have seen one query that may be able to apply on my case.
SELECT
T1.CardCode + '' AS 'BP Code',
T2.Notes2 AS 'BP Name',
T0.RefDate,
CASE
WHEN T0.TransType = 13 THEN 'IN'
WHEN T0.TransType = 14 THEN 'CN'
WHEN T0.TransType = 30 THEN 'JE'
WHEN T0.TransType = 24 THEN 'RC'
WHEN T0.TransType = 46 THEN 'PS'
ELSE 'Error ! ! !'
END AS 'Doc Type',
T0.Ref1 'Doc. Number',
ISNULL(T0.FCCurrency, ' - ') AS 'Ccy',
(T0.BalFcDeb - T0.BalFcCred) AS 'Bal. F. Ccy',
(T0.BalDueDeb - T0.BalDueCred) AS 'Bal. Rs',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate, '[%1]') <= -1) ,0) AS 'Future',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate, '[%1]') = 0) ,0) AS 'Current Mth',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate, '[%1]') = 1) ,0) AS '1 Mth Ago',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate, '[%1]') = 2) ,0) AS '2 Mth Ago',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate ,'[%1]') = 3) ,0) AS '3 Mth Ago',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate, '[%1]') between 4 and 6) ,0) AS '4 - 6 Mth Ago',
ISNULL((SELECT T0.BalDueDeb -T0.BalDueCred WHERE DateDiff(mm, T0.RefDate, '[%1]') >= 7) ,0) AS '>7 Mth Ago'
FROM JDT1 T0
INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode
LEFT OUTER JOIN OCPR T2 ON T1.CardCode = T2.Cardcode
LEFT OUTER JOIN OJDT T3 ON T0.TransID = T3.TransID
LEFT OUTER JOIN OINV T4 ON T3.TransID = T4.TransID
LEFT OUTER JOIN ORIN T5 ON T3.TransID = T5.TransID
WHERE
T1.CardType = 'C' and Balance != 0
and (T0.BalDueDeb - T0.BalDueCred) != 0
but still try to figure it out.... -
11g: Multicolumn pivot - help with query
Hi everyone,
My first attempt at posting here was not very successful. I have now read the rules, and done more homework.
I'm fairly new to Oracle databases, but have some experience with MySQL and Postgres from earlier.
First of all, here is an example table of what I have today. This is only an extract of the full table, but these fields are the interesting ones.
CREATE TABLE trans
("FROM_LIC" int, "FROM_LOCATION" varchar2(18), "TO_LOCATION" varchar2(18), "CREATE_DT" timestamp)
INSERT ALL
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100002563, '215', 'INN_MONO_05', '04-Mar-2013 11:54:21 AM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100002563, 'INN_MONO_05', 'INN_MONO_06_BANE_R', '04-Mar-2013 11:55:08 AM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100002563, 'INN_MONO_06_BANE_R', 'TROLLEY_19', '04-Mar-2013 12:01:06 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100002563, 'TROLLEY_19', 'UT_OPPLAST_5_2', '04-Mar-2013 12:01:56 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100002563, 'UT_OPPLAST_5_2', 'STG010801', '04-Mar-2013 12:01:56 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003259, '231', 'INN_MONO_04', '04-Mar-2013 02:18:31 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003259, 'INN_MONO_04', 'INN_MONO_04_BANE_L', '04-Mar-2013 02:19:28 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003259, 'INN_MONO_04_BANE_L', 'TROLLEY_5', '04-Mar-2013 02:22:41 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003259, 'TROLLEY_5', 'UT_OPPLAST_3_1', '04-Mar-2013 02:23:35 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003262, '243', 'INN_MONO_06', '04-Mar-2013 01:37:49 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003262, 'INN_MONO_06', 'INN_MONO_06_BANE_R', '04-Mar-2013 01:39:09 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003262, 'INN_MONO_06_BANE_R', 'TROLLEY_10', '04-Mar-2013 01:43:48 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003262, 'TROLLEY_10', 'UT_OPPLAST_5_2', '04-Mar-2013 01:44:58 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003262, 'UT_OPPLAST_5_2', 'STG010904', '04-Mar-2013 01:46:30 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003263, '231', 'INN_MONO_04', '04-Mar-2013 02:18:31 PM')
INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
VALUES (4100003263, 'INN_MONO_04', 'INN_MONO_04_BANE_R', '04-Mar-2013 02:19:20 PM')
SELECT * FROM dual
;As of now, I have a query that returns a data grid from the table, looking exactly like the example table I've included.
Here is a copy of the query that gives the example table as a result from the table in our database:
select * from (
select distinct from_lic, from_location, to_location, create_dt from trans where to_location like 'INN_MONO___' and create_dt > sysdate-(3/24)
union
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO___' and to_location like 'INN_MONO%BANE%' and create_dt > sysdate-(3/24)
union
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO%BANE%' and to_location like 'TROLLEY%' and create_dt > sysdate-(3/24)
union
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'TROLLEY%' and to_location like 'UT_OPPLAST____' and create_dt > sysdate-(3/24)
union
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'UT_OPPLAST____%' and (to_location like 'STG%' or to_location like 'UT_OPPLAST%GULV') and create_dt > sysdate-(3/24)
) order by from_lic, create_dtI would be delighted if you could help me formulating a new query that gives THIS output, from the same table, with the same constraints as in the original query:
CREATE TABLE new_table
("FROM_LIC" int, "FLOOR" timestamp, "INFEED" timestamp, "TROLLEY" timestamp, "OUTFEED" timestamp, "STAGING" varchar2(16))
INSERT ALL
INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
VALUES (4100002563, '04-Mar-2013 11:54:00 AM', '04-Mar-2013 11:55:00 AM', '04-Mar-2013 12:01:00 PM', '04-Mar-2013 12:01:00 PM', '03.04.2013 12:01')
INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
VALUES (4100003259, '04-Mar-2013 02:18:00 PM', '04-Mar-2013 02:19:00 PM', '04-Mar-2013 02:22:00 PM', '04-Mar-2013 02:23:00 PM', NULL)
INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
VALUES (4100003262, '04-Mar-2013 01:37:00 PM', '04-Mar-2013 01:39:00 PM', '04-Mar-2013 01:43:00 PM', '04-Mar-2013 01:44:00 PM', '03.04.2013 13:46')
SELECT * FROM dual
;What happened here is that for each instance of from_lic, the line yielded from
select distinct from_lic, from_location, to_location, create_dt from trans where to_location like 'INN_MONO___' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> in the new column called FLOOR.
And the following for the rest of the columns:
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO___' and to_location like 'INN_MONO%BANE%' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> INFEED.
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO%BANE%' and to_location like 'TROLLEY%' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> TROLLEY.
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'TROLLEY%' and to_location like 'UT_OPPLAST____' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> OUTFEED.
select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'UT_OPPLAST____%' and (to_location like 'STG%' or to_location like 'UT_OPPLAST%GULV') and create_dt > sysdate-(3/24)The value for CREATE_DT ---> STAGING.
Originally I was thinking of a pivot for this, but there is possibly better ways to accomplish this.
I would like to have the data in the described format to make data mining easier.
The query will run on a table with >3M lines.
I hope this way of formulating the question is better, and more comprehensible.
Please do not hesitate to ask questions, and I'll do my best to answer.
I can test any suggested queries in the production database when necessary.
Edited by: 997749 on Apr 3, 2013 5:18 AM
Edited by: 997749 on Apr 3, 2013 6:20 AMHi,
what about something like this:
with trans as
SELECT 4100002563 from_lic, 'INN_MONO_05' to_location, TO_DATE('03.04.2013 11:54:21', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100002563 from_lic, 'INN_MONO_06_BANE_R' to_location, TO_DATE('03.04.2013 11:55:08', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100002563 from_lic, 'TROLLEY_19' to_location, TO_DATE('03.04.2013 12:01:06', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100002563 from_lic, 'STG010801' to_location, TO_DATE('03.04.2013 12:01:56', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100002563 from_lic, 'UT_OPPLAST_5_2' to_location, TO_DATE('03.04.2013 12:01:56', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100005037 from_lic, 'INN_MONO_05' to_location, TO_DATE('03.04.2013 11:18:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100005037 from_lic, 'INN_MONO_04_BANE_R' to_location, TO_DATE('03.04.2013 11:21:54', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100005037 from_lic, 'TROLLEY_16' to_location, TO_DATE('03.04.2013 11:25:43', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100005037 from_lic, 'UT_OPPLAST_3_4' to_location, TO_DATE('03.04.2013 11:26:37', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100005037 from_lic, 'STG010703' to_location, TO_DATE('03.04.2013 11:27:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100006658 from_lic, 'INN_MONO_08' to_location, TO_DATE('03.04.2013 11:00:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100006658 from_lic, 'INN_MONO_08_BANE_L' to_location, TO_DATE('03.04.2013 11:02:35', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100006658 from_lic, 'TROLLEY_22' to_location, TO_DATE('03.04.2013 11:07:54', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
SELECT 4100006658 from_lic, 'UT_OPPLAST_7_3' to_location, TO_DATE('03.04.2013 11:09:03', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL
, got_category AS
SELECT from_lic
, CASE
WHEN to_location like 'INN_MONO%BANE%'
THEN 'INFEED'
WHEN to_location like 'INN_MONO%'
THEN 'FLOOR'
WHEN to_location like 'TROLLEY%'
THEN 'TROLLEY'
WHEN to_location like 'STG' OR to_location like 'UT%GULV'
THEN 'STAGED'
WHEN to_location like 'UT_OPPLAST%'
THEN 'OUTFEED'
ELSE 'UNKNOWN'
END as cat
, create_dt
FROM trans
WHERE create_dt > (sysdate -10/24) -- adjusted for different timezone
SELECT * --FROM_LIC, lic_a, lic_b, lic_c, lic_d, lic_e
FROM got_category
PIVOT(MAX(create_dt) FOR cat IN('FLOOR' as floor,
'INFEED' as infeed,
'TROLLEY' as trolley,
'OUTFEED' as ooutfeed,
'STAGED' as staged));
FROM_LIC FLOOR INFEED TROLLEY OOUTFEED STAGED
4100002563 03-APR-2013 11:54:21 03-APR-2013 11:55:08 03-APR-2013 12:01:06 03-APR-2013 12:01:56
4100005037 03-APR-2013 11:18:31 03-APR-2013 11:21:54 03-APR-2013 11:25:43 03-APR-2013 11:26:37
4100006658 03-APR-2013 11:00:31 03-APR-2013 11:02:35 03-APR-2013 11:07:54 03-APR-2013 11:09:03 If not useful, please post your input data (CREATE TABLE and INSERT statements) and your expected output.
Regards.
Al -
Help with query to do with aggregating for MIN(DATE)
Say i have the following table of data:
Table: TEST
ID TDATE CLIENT VAL
1A 1/10/2005 client1 10
2A 2/10/2005 client1 30
3A 3/11/2005 client2 22
4A 5/10/2005 client2 43
5A 5/10/2005 client2 3
6A 6/10/2005 client3 5
7A 7/10/2005 client3 76
I want to create a query that will retrieve a distinct list of the CLIENT column (ie. in the above table I want only 3 records retrieved in the query as there are only 3 clients) and the corresponding ID and VAL for the client record with the MIN(DATE). In the case where 2 dates are the same, I dont care which record is selected as long as the ID and VAL columns belong to the same record.
For example in the above data I would expect the following returned in the query:
ID CLIENT VAL
1A client1 10
4A client2 43 <- this record could also be 5A, client2, 3
6A client3 5
Please help.Hi Joshua,
You didn't post your Oracle version but I hope it supports analytic functions.
Try as follows:
SQL> select id,
2 tdate,
3 client,
4 val
5 from (select t.*,
6 row_number() over
7 (partition by client order by tdate) rn
8 from test t)
9 where rn = 1;
ID TDATE CLIENT VAL
1A 01/10/2005 client1 10
4A 05/10/2005 client2 43
6A 06/10/2005 client3 5Regards. -
Need help with query joining several tables into a single return line
what i have:
tableA:
puid, task
id0, task0
id1, task1
id2, task2
tableB:
puid, seq, state
id0, 0, foo
id0, 1, bar
id0, 2, me
id1, 0, foo
id2, 0, foo
id2, 1, bar
tableC:
puid, seq, date
id0, 0, 12/21
id0, 1, 12/22
id0, 2, 12/22
id1, 0, 12/23
id2, 0, 12/22
id2, 1, 12/23
what i'd like to return:
id0, task0, 12/21, 12/22, 12/22
id1, task1, 12/23, N/A, N/A
id2, task2, 12/22, 12/23, N/A
N/A doesn't mean return the string "N/A"... it just means there was no value, so we don't need anything in this column (null?)
i can get output like below through several joins, however i was hoping to condense each "id" into a single line...
id0, task0, 12/21
id0, task0, 12/22
id0, task0, 12/23
id1, task1, 12/23
is this possible fairly easily?
Edited by: user9979830 on Mar 29, 2011 10:53 AM
Edited by: user9979830 on Mar 29, 2011 10:58 AMHi,
Welcome to the forum!
user9979830 wrote:
what i have:...Thanks for posting that so clearly!
Whenever you have a question, it's even better if you post CREATE TABLE and INSERT statements for your sample data, like this:
CREATE TABLE tablea
( puid VARCHAR2 (5)
, task VARCHAR2 (5)
INSERT INTO tablea (puid, task) VALUES ('id0', 'task0');
INSERT INTO tablea (puid, task) VALUES ('id1', 'task1');
INSERT INTO tablea (puid, task) VALUES ('id2', 'task2');
CREATE TABLE tablec
( puid VARCHAR2 (5)
, seq NUMBER (3)
, dt DATE -- DATE is not a good column name
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 0, DATE '2010-12-21');
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 1, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id0', 2, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id1', 0, DATE '2010-12-23');
INSERT INTO tablec (puid, seq, dt) VALUES ('id2', 0, DATE '2010-12-22');
INSERT INTO tablec (puid, seq, dt) VALUES ('id2', 1, DATE '2010-12-23');This way, people can re-create the problem and test their ideas.
It doesn't look like tableb plays any role in this problem, so I didn't post it.
Explain how you get the results from that data. For example, why do you want this row in the results:
PUID TASK DT1 DT2 DT3
id0 task0 12/21/2010 12/22/2010 12/22/2010rather than, say
PUID TASK DT1 DT2 DT3
id0 task0 12/22/2010 12/21/2010 12/22/2010? Does 12/21 have to go in the first column because it is the earliest date, or is it because 12/21 is related to the lowest seq value? Or do you even care about the order, just as long as all 3 dates are shown?
Always say what version of Oracle you're uisng. The query below will work in Oracle 9 (and up), but starting in Oracle 11, the SELECT ... PIVOT feature could help you.
i can get output like below through several joins, however i was hoping to condense each "id" into a single line... Condensing the output, so that there's only one line for each puid, sounds like a job for "GROUP BY puid":
WITH got_r_num AS
SELECT puid
, dt
, ROW_NUMBER () OVER ( PARTITION BY puid
ORDER BY seq -- and/or dt
) AS r_num
FROM tablec
-- WHERE ... -- If you need any filtering, put it here
SELECT a.puid
, a.task
, MIN (CASE WHEN r.r_num = 1 THEN r.dt END) AS dt1
, MIN (CASE WHEN r.r_num = 2 THEN r.dt END) AS dt2
, MIN (CASE WHEN r.r_num = 3 THEN r.dt END) AS dt3
, MIN (CASE WHEN r.r_num = 4 THEN r.dt END) AS dt4
FROM tablea a
JOIN got_r_num r ON a.puid = r.puid
GROUP BY a.puid
, a.task
ORDER BY a.puid
;I'm guessing that you want the dates arranged by seq; that is, for each puid, the date related to the lowest seq comes first, regardless of whther that date is the earliest date for that puid or not. If that's not what you need, then change the analytic ORDER BY clause.
This does not assume that the seq values are always consecutive integers (0, 1, 2, ...) for each puid. You can skip, or even duplicate values. However, if the values are always consecutive integers, starting from 0, then you could simplify this. You won't need a sub-query at all; just use seq instead of r_num in the main query.
Here's the output I got from the query above:
PUID TASK DT1 DT2 DT3 DT4
id0 task0 12/21/2010 12/22/2010 12/22/2010
id1 task1 12/23/2010
id2 task2 12/22/2010 12/23/2010As posted, the query will display the first 4 dts for each puid.
If there are fewer than 4 dts for a puid, the query will still work. It will leave some columns NULL at the end.
If there are more than 4 dts for a puid, the query will still work. It will display the first 4, and ignore the others.
There's nothing special about the number 4; you could make it 3, or 5, or 35, but whatever number you choose, you have to hard-code that many columns into the query, and always get that many columns of output.
For various ways to deal with a variable number of pivoted coolumns, see the following thread:
PL/SQL
This question actually doesn't have anything to do with SQL*Plus; it's strictly a SQL question, and SQL questions are best posted on the "SQL and PL/SQL" forum:
PL/SQL
If you're not sure whether a question is more of a SQL question or a SQL*Plus question, then post it on the SQL forum. Many more people pay attention to that forum than to this one. -
Right now my query takes anywhere from 30 to 60 sec's to execute. What should I do to cut this time down?
Here is my Query
select * from a , b, c
where a.a_seq = b.b_seq
and a.a_seq = c.c_seq
and a.a_type between 3 and 19
and a.detl_cod = 'AV'
and b.detl_stat = 'CLD'
and c.date is not null
and c.id != 92342
these tables have 100 thousand rows each. What should I be looking at to cut down the execution time on this query?Sorry, I am fairly new to SQL. I can write queries but, I have no idea where to even begin with when it comes to checking performance. What are the statistics that you are mentioning? how will it effect performance? and what about indexed attributes? there are a lot of indexed attrinutes on each of these tables. I am unable to give you the exact tables because it is production application code(Cofidential).
I really appreciate any help from you
Thank you.
Maybe you are looking for
-
Archiving ERS documents into an external archive
Hello guys, I'm an apprentice that is currently getting worked into SAP. We are currently working on a release Change for SRM. Now my task is to think about ways to realize the archiving of ERS documents (credit items?) from SRM into an external arch
-
Spacing, alignment and labels position ...
How can I adjust the spacing between items? When exporting as a PDF there's a ton of wasted space on printed forms, making it hard to fit much on a single page. Why do I have to adjust the "labels position" for the whole document? How can I just adju
-
Does anyone know what software is used for creating this?
I have seen this effect on a few television adverts and wondered how I would go about creating it. In this click the ball is kicked and as it gets to the screen it "breaks" into pieces and the ball comes out of the screen - I know this isnt the same
-
Allow Browser invocation of Air application
Hello Friends Can Anybody guide me how to implement Allow browser invocation Feature in my Air pplication.i mean To say application can be launched when the user clicks a link in a web browser. Please tell me After Change<allow
-
Vista Service Pack 2 will not install
I have tried everything that I have found on Google and nothing has worked. Called Lenovo...........told to check Documents 66956 and 72758. I did that and updated the ThinkVantage software then installing SP2 as told by tech support.............NO