Need to simplify a query
Brief description of the data and the sample data is in the second post:I have to take the following data:
BUSINESSDATE STORENBR DESCRIPTION CAT_COST CAT_IN cat_s&h
------------ -------- --------------- ---------------------- ---------------------- ----------
21-MAR-10 0002 chicken 1450.34 0 0
21-MAR-10 0002 filets 357.2 0 0
21-MAR-10 0002 popcorn_chicken 204.6 0 0
21-MAR-10 0003 chicken 2064.38 117.96 0
21-MAR-10 0003 filets 494 0 0
and make it look like this:store inv_date chicken chicken_in chicken_S&H filets filets_in filets_S&H popcorn_chicken popcorn_chicken_in popcorn_chicken_S&H
43 4/4/2010 1284.73 0 685.8 208.64 0 0 375.72 0 0
19 4/4/2010 2906.66 91.44 0 418.98 0 0 279 0 0
3 4/4/2010 1917.55 0 22.45 376.86 0 17.5 297.6 0 0
2 4/4/2010 1470.66 22.45 0 365.8 0 8.75 178.56 0 0
where each element/categpry sold by a store has 3 pieces of data: category cost, inventory of start of category, category shipping and handling
Hello
Something like that?
SQL> select storenbr,max(case description when'chicken' then CAT_COST end) chicken,
2 max(case description when 'chicken' then CAT_IN end) chicken_in,
3 max(case description when 'chicken' then "cat_s&h" end) "chicken_S&H",
4 max(case description when 'filets' then CAT_COST end) filets,
5 max(case description when 'filets' then CAT_IN end) filets_in,
6 max(case description when 'filets' then "cat_s&h" end) "filets_S&H",
7 max(case description when 'popcorn_chicken' then CAT_COST end) popcorn_chicken,
8 max(case description when 'popcorn_chicken' then CAT_IN end) popcorn_chicken_in,
9 max(case description when 'popcorn_chicken' then "cat_s&h" end) "popcorn_chicken_S&H"
10 from dummy_data
11 group by storenbr
12 order by storenbr desc;
STOR CHICKEN CHICKEN_IN chicken_S&H FILETS FILETS_IN filets_S&H POPCORN_CHICKEN POPCORN_CHICKEN_
---- ---------- ---------- ----------- ---------- ---------- ---------- --------------- ------------
0043 1113.78 0 688.65 482.2 0 0 238.08 0 0
0019 3388.93 238.73 0 770.53 28.28 0 327.36 74.4 0
0003 2064.38 117.96 0 494 0 0 297.6 0 0
0002 1450.34 0 0 357.2 0 0 204.6 0 0
Twinkle
Tags: Database
Similar Questions
-
Hi gurus
I have the following data:
Drop table
DROP TABLE lb_source;
DROP TABLE lb;
Insertion and table creation
CREATE TABLE lb_source
(
source_cd VARCHAR2 (5));
INSERT INTO lb_source
(
SELECT 'a' STARTING from two
UNION ALL
SELECT 'b' double
UNION ALL
Select 'l' to double
);CREATE TABLE lb
(
group_number VARCHAR2 (10),
source_cd varchar2 (5)
);INSERT INTO lb
(
SELECT '100', 'l' FROM dual
UNION ALL
SELECT '100', double null);
------------
If you see table lb then it contained a line with source_cd = l against 100 and source_cd = Null group against group 100, now I want to create this kind of query, in which all correspondence lines in tables to join both if lb is null, then query join all the source_cd of table lb_source except source_cd = 'l '. So far, I created the following query and its works ok but I want to do all my work in one in where condition...
Query
SELECT * FROM
(
SELECT s1, s2 b.source_cd A.source_cd
OF lb_source A
lb b
WHERE A.source_cd = nvl (b.source_cd, A.source_cd)
)
WHERE s2 IS NULL
;
Result
s1 s2
a null value
b zero
l null
Please guide
Hello
So, you want something that produces the same accurate results as the query you posted, but does more just; Isn't it?
Here's one way:
SELECT ls.source_cd S1
NULL AS s2
OF ls lb_source
JOIN lb l WE l.source_cd IS NULL
AND ls.source_cd IS NOT NULL;
I'm curious; What is the business problem that requires this request? What tables and columns represent, and what tell you the query?
Thanks for posting the CREATE TABLE and INSERT statements; It's very useful!
-
Can anyone simplify this query
Here's a DOF from two tables
1. CREATE TABLE (EMPL)
NUMBER OF SNO
ENAME VARCHAR2 (25).
USE VARCHAR2 (25).
KEY ELEMENTARY SCHOOL (SNO)
);
2. CREATE TABLE EMPL_DET)
NUMBER OF SNO
SAL VARCHAR2 (25)
);
Here are tables LMD
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (1, 'SMITH', 'CLERK');
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (2, 'SMITH', 'MANAGER');
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (3, 'TOM', 'CLK');
INSERT INTO EMPL_DET (SNO, SAL) VALUES (1, '1000');
INSERT INTO EMPL_DET (SNO, SAL) VALUES (2, "10000");
INSERT INTO EMPL_DET (SNO, SAL) VALUES (3, '900');
I want to calculate TotalSAL (column: empl_det.) SAL) of each employee (empl.ename) with job-description (empl.job).
Means I want following the lines of output
1.(Job,TotalSAL,Ename)-> (CLERK, 11000, SMITH)
2->.(Job,TotalSAL,Ename) (MANAGER, 11000, SMITH)
3->.(Job,TotalSAL,Ename) (CLK, 900, TOM)
I tried to write down to unique ename
Select JOB, x.sal, ename in empl,
(
Select sum (sal) sal empl_det where sno in
(select sno to empl where ename = 'SMITH')
) x
where ename = 'SMITH '.
order by ename
each ename, I draw from this query. How can I make the ename list (TOM SMITH) to this request?
Or can anyone simplify this query?Hello
in this case, you need to use is analytical functions:
that is to say:
SELECT e.job, SUM (d.sal) OVER (PARTITION BY e.ename) AS "TotalSal" , e.ename FROM empl e, empl_det d WHERE d.sno = e.sno AND e.ename IN ('SMITH', 'TOM') ORDER BY e.ename; JOB TotalSal ENAME ------------------------- ---------- ------------------------- CLERK 11000 SMITH MANAGER 11000 SMITH CLK 900 TOM
Kind regards.
AlPublished by: Alberto Faenza on 27 November 2012 15:34
Corrected query -
Oracle Version: 11.2.0.2.0
I need assistance with the output of the query. Here is the table.
With Tbl_Nm as
(
Select 'ABC1' SYSTEM_ID, REGION 'US', 'CHI' SUB_REGION 4000 BALANCE, to_date('1-JUN-2012 10:45:00 am', 'dd-mon-yyyy hh:mi:ss am') LAST_UPD_TIME, 'A' FLAG of union double all the
Select 'PQR2', 'UK', 'LN', 2000, To_Date('1-JUL-2012 10:46:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select 'ABC1', 'IND","MAMA", 3500, To_Date('1-AUG-2012 11:47:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "LMN3", "US", "NJ", 2500, To_Date('1-SEP-2012 09:49:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "PQR2", "UK", "MC", 2600, To_Date('1-OCT-2012 04:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select 'ABC1', 'US', 'NY', 3200, To_Date('1-OCT-2012 06:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select "LMN3", "UK", "BT", 2400, To_Date('1-NOV-2012 07:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' From Dual
)
Select * from tbl_nm
I need the output below.
PQR2 UK MC 2600 1 OCTOBER 2012 04:45
ABC1 US NY 3500 October 1, 2012 06:45
LMN3 UK BT 2500 November 1, 2012 07:45
The need the disc according to this system_id flagged as "A". But if the last disc of 'd' then it must show that the amount, but the file should be displayed in 'A '.
I've tried a few and got stuck. Help, please. Not able to get a balance '.
This question is a bit similar to needing help with a query result
With Tbl_Nm as
(
Select 'ABC1' System_Id, region 'US', 'CHI' Sub_Region, 4000 balance, To_Date('1-JUN-2012 10:45:00 am', 'dd-mon-yyyy hh:mi:ss am') Last_Upd_Time, 'A' flag of double Union All
Select 'PQR2', 'UK', 'LN', 2000, To_Date('1-JUL-2012 10:46:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select 'ABC1', 'IND","MAMA", 3500, To_Date('1-AUG-2012 11:47:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "LMN3", "US", "NJ", 2500, To_Date('1-SEP-2012 09:49:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "PQR2", "UK", "MC", 2600, To_Date('1-OCT-2012 04:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select 'ABC1', 'US', 'NY', 3200, To_Date('1-OCT-2012 06:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select "LMN3", "UK", "BT", 2400, To_Date('1-NOV-2012 07:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' From Dual
)
Select System_Id, region, Sub_Region, Balance, Last_Upd_Time of Tbl_Nm T1
where t1. Last_Upd_Time = (select max (Last_Upd_Time) in the Tbl_Nm T2 where T1.) SYSTEM_ID = T2. SYSTEM_ID)
So maybe you'd then
ORDER BY DECODE(flag,'D',9,1) ASC...
to get the Ds at the end of the list.
or
ORDER BY CASE WHAT flag = has ' (your other filters) AND then 9 or 1 end CSA,...
HTH
-
When we need to use a query variable and how to use it?
Hello
When we create a line of dashboard, we set the variable, there are two types: variable presentation and application.
I'm confuse when we need to use a query variable and how to use it?
Thank you
AnneHello
The variable 'demand' can be used if you want to use session related information specific to you.See below a:
http://gerardnico.com/wiki/dat/OBIEE/logical_sql/obiee_set_request_variable_dashboard_promptFYI... example of
for the example query variable (a good post Nico)http://gerardnico.com/wiki/dat/OBIEE/set_variable
for the variable of presentation with example
http://gerardnico.com/wiki/dat/OBIEE/presentation_variable
Thank you
Deva
-
Hi gurus
need help with this query,
I want to display the records in the table emp
I want just the recordsSQL> Select Deptno,sal,sal/SUMSAL Percent,rn 2 From ( Select emp.*,Sum(Sal) Over() "SUMSAL",Row_number() Over(Order by sal Desc) rn 3 From emp 4 ) 5 / EMPNO DEPTNO SAL PERCENT RN --------- ---------- ---------- ---------- ---------- 7839 10 5000 .172265289 1 7902 20 3000 .103359173 2 7788 20 3000 .103359173 3 7566 20 2975 .102497847 4 7698 30 2850 .098191214 5 7782 10 2450 .084409991 6 7499 30 1600 .055124892 7 7844 30 1500 .051679587 8 7934 10 1300 .044788975 9 7521 30 1250 .043066322 10 7654 30 1250 .043066322 11 7876 20 1100 .037898363 12 7900 30 950 .032730405 13 7369 20 800 .027562446 14 14 rows selected.
Thank youEMPNO DEPTNO SAL PERCENT RN ---------- ---------- ---------- ---------- ---------- 7839 10 5000 .172265289 1 7902 20 3000 .103359173 2 7788 20 3000 .103359173 3 7566 20 2975 .102497847 4 7698 30 2850 .098191214 5 with sum (Percent) of remaing records..... Others .420327304
Published by: SeenuGuddu on February 27, 2011 03:39with a as ( Select Empno, Deptno ,sal, sal/SUMSAL Percent, case when rn<=5 then rn else null end rnm From (Select emp.*, Sum(Sal) Over() "SUMSAL", Row_number() Over(Order by sal Desc) rn From emp) ) select case when max(rnm) is not null then to_char(max(empno)) else 'Others:' end empno, case when max(rnm) is not null then max(deptno) else null end deptno, case when max(rnm) is not null then max(sal) else null end sal, to_char(sum(percent), '90.99') percent, max(rnm) rn from a group by rnm order by rnm
EMPNO DEPTNO SAL PERCENT RN ---------------------------------------- ---------------------- ---------------------- ------- ---------------------- 7839 10 5000 0.17 1 7902 20 3000 0.10 2 7788 20 3000 0.10 3 7566 20 2975 0.10 4 7698 30 2850 0.10 5 Others: 0.42 6 rows selected
-
Extend the VO - need to change the query of VO - screen is off
{Re-post the message in this forum (thanks to John Stegeman to lead me here...) {Posted: August 28, 2009 04:27}
Dear all,
Thanks to this great body of knowledge, I ask my questions.
I need to extend a VO to display the name of the country.
The column is not in the VO. So, I need to get another table: FndTerritories
I want to change the query, but the request frame is turned off (in the wizard the View - step 5 of 6 - Generated statement object substitution)
Could you be it someone please let me know: how to change the application of the VO in this case?
OR is there another solution for such cases, to maintain development standards
Thank you very much
Kind regardsHello
I think that's when you just have to add an attribute, you should not click the expert mode checkbox... make use of
Mix and add the button.But when you need to modify the query, and then we want to use to modify the query window...
Thank you
Gerard -
How to simplify this query in sql simple select stmt
Hello
Please simplify the query
I want to convert this query in a single select statement. Is this possible?
If uarserq_choice_ind is not null then
Select ubbwbst_cust_code
From ubbwbst,utrchoi
Where utrchoi_prop_code=ubbwbst_cancel_prod
Else
Select max(utvsrvc_ranking)
From utvsrvc,ubbwbst
Where utvsrvc_code=ubbwbst_cancel_prod
End ifSelect ubbwbst_cust_code as val From ubbwbst,utrchoi Where utrchoi_prop_code=ubbwbst_cancel_prod AND uarserq_choice_ind is not null union all Select max(utvsrvc_ranking) as val From utvsrvc,ubbwbst Where utvsrvc_code=ubbwbst_cancel_prod and uarserq_choice_ind is null
Without more information, we are unable to combine the two queries in 1 without a union.
Looks like you select values totally disperate of totally different tables -
[8i] not to simplify this query?
In my application, I need to pick up three possible prices for each part number in an array.
The three possible prices that I need to recover are:
(1) the price paid on the order more recently closed,
(2) the price mentioned on the first pending order, and
(3) the price on the furthest on pending order.
However, there is a complication in that. Orders include the part number, but may also include a prefix of 4 characters (always the same) on a reference number which should be treated the same as the reference database. For example, "DSB-part1" should be considered "part1", "DSB-part2" should be regarded as 'part 2', etc.
In addition, it is quite possible for several orders expected or closed on the same day, and I want to only return the price of one of these lines for each of the 3 methods of pricing.
(Technically, there is another level of complication to this, but if I have problems with it later, I'll create a new job for him...)
Some examples of data:
And here's my ugly query to get the results I want. It can be simplified?CREATE TABLE part ( part_no VARCHAR2(9) NOT NULL, part_desc VARCHAR2(25), qty_instock NUMBER, CONSTRAINT part_pk PRIMARY KEY (part_no) ); INSERT INTO part VALUES ('part1 ','description 1 here',5); INSERT INTO part VALUES ('part2 ','description 2 here',10); INSERT INTO part VALUES ('part3 ','description 3 here',0); CREATE TABLE ords ( ord_no NUMBER NOT NULL, ord_part_no VARCHAR2(9), date_closed DATE, orig_dock_date DATE, date_due_instock DATE, unit_price NUMBER, qty_order NUMBER, ord_stat VARCHAR2(2), CONSTRAINT ords_pk PRIMARY KEY (ord_no) ); INSERT INTO ords VALUES (1,'part1 ',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL'); INSERT INTO ords VALUES (2,'part1 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL'); INSERT INTO ords VALUES (3,'part1 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL'); INSERT INTO ords VALUES (4,'part1 ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP'); INSERT INTO ords VALUES (5,'ORD-part1',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP'); INSERT INTO ords VALUES (6,'ORD-part2',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL'); INSERT INTO ords VALUES (7,'part2 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL'); INSERT INTO ords VALUES (8,'ORD-part2',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL'); INSERT INTO ords VALUES (9,'part2 ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP'); INSERT INTO ords VALUES (10,'ORD-part2',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP'); INSERT INTO ords VALUES (11,'part3 ',To_Date('01/01/2009','mm/dd/yyyy'),To_Date('12/01/2008','mm/dd/yyyy'),To_Date('12/15/2008','mm/dd/yyyy'),100,10,'CL'); INSERT INTO ords VALUES (12,'part3 ',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/01/2009','mm/dd/yyyy'),To_Date('01/05/2009','mm/dd/yyyy'),105,15,'CL'); INSERT INTO ords VALUES (13,'ORD-part3',To_Date('01/31/2009','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),100,20,'CL'); INSERT INTO ords VALUES (14,'ORD-part3',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/15/2009','mm/dd/yyyy'),To_Date('01/25/2009','mm/dd/yyyy'),103,10,'OP'); INSERT INTO ords VALUES (15,'part3 ',To_Date('12/31/1900','mm/dd/yyyy'),To_Date('01/20/2009','mm/dd/yyyy'),To_Date('01/31/2009','mm/dd/yyyy'),101,10,'OP');
And here are the results, I expect to get, according to data from the sample:SELECT p.part_no , p.part_desc , p.qty_instock , a2.unit_price AS last_closed_price , a2.qty_order AS last_closed_qty , a2.date_closed AS last_closed_date , b2.unit_price AS first_open_price , b2.qty_order AS first_open_qty , b2.date_due_instock AS first_open_date , c2.unit_price AS last_open_date , c2.qty_order AS last_open_qty , c2.date_due_instock AS last_open_date FROM part p , ( SELECT lc.part_no , lc.unit_price , lc.qty_order , lc.date_closed , ROW_NUMBER() OVER(PARTITION BY lc.part_no ORDER BY lc.orig_dock_date DESC) AS rnk_nbr FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_closed , o.orig_dock_date , o.unit_price , o.qty_order FROM ords o ) lc --for last closed , ( SELECT a.part_no , MAX(a.date_closed) AS last_closed_date FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_closed FROM ords o ) a GROUP BY a.part_no ) a1 WHERE lc.part_no = a1.part_no AND lc.date_closed = a1.last_closed_date ) a2 , ( SELECT fo.part_no , fo.unit_price , fo.qty_order , fo.date_due_instock , ROW_NUMBER() OVER(PARTITION BY fo.part_no ORDER BY fo.orig_dock_date DESC) AS rnk_nbr FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock , o.orig_dock_date , o.unit_price , o.qty_order FROM ords o WHERE o.ord_stat = 'OP' ) fo --for first open , ( SELECT b.part_no , MIN(b.date_due_instock) AS first_open_date FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock FROM ords o WHERE o.ord_stat = 'OP' ) b GROUP BY b.part_no ) b1 WHERE fo.part_no = b1.part_no AND fo.date_due_instock = b1.first_open_date ) b2 , ( SELECT lo.part_no , lo.unit_price , lo.qty_order , lo.date_due_instock , ROW_NUMBER() OVER(PARTITION BY lo.part_no ORDER BY lo.orig_dock_date DESC) AS rnk_nbr FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock , o.orig_dock_date , o.unit_price , o.qty_order FROM ords o WHERE o.ord_stat = 'OP' ) lo --for last open , ( SELECT c.part_no , MAX(c.date_due_instock) AS last_open_date FROM ( SELECT CASE WHEN SUBSTR(o.ord_part_no,1,4) = 'ORD-' THEN SUBSTR(o.ord_part_no,5) ELSE o.ord_part_no END AS part_no , o.date_due_instock FROM ords o WHERE o.ord_stat = 'OP' ) c GROUP BY c.part_no ) c1 WHERE lo.part_no = c1.part_no --EDIT: changed from l1 to c1 AND lo.date_due_instock = c1.last_open_date --EDIT: changed from l1 to c1 ) c2 WHERE p.part_no = a2.part_no AND a2.part_no = b2.part_no AND b2.part_no = c2.part_no AND a2.rnk_nbr = 1 AND b2.rnk_nbr = 1 AND c2.rnk_nbr = 1
Published by: user11033437 on February 5, 2010 08:48. LAST_ LAST_ LAST_ FIRST_ FIRST_ FIRST_ LAST_ LAST_ LAST_ . QTY_ CLOSED_ CLOSED_ CLOSED_ OPEN_ OPEN_ OPEN_ OPEN_ OPEN_ OPEN_ PART_NO PART_DESC INSTOCK PRICE QTY DATE PRICE QTY DATE PRICE QTY_ DATE_ --------------------------------------------------------------------------------------------------------------------------------- part1 description 1 here 5 100 20 1/31/2009 103 10 1/25/2009 101 10 1/31/2009 part2 description 2 here 10 100 20 1/31/2009 103 10 1/25/2009 101 10 1/31/2009 part3 description 3 here 0 100 20 1/31/2009 103 10 1/25/2009 101 10 1/31/2009
Correction of errors (see the comments in the request above for changes)Hello
It is a little bit shorter than what you have posted, probably more effective and (in my opinion) much easier to debug and maintain:
SELECT p.part_no , p.part_desc , p.qty_instock , o.last_closed_price , o.last_closed_qty , o.last_closed_date , o.first_open_price , o.first_open_qty , o.first_open_date , o.last_open_price , o.last_open_qty , o.last_open_date FROM part p , ( -- Begin in-line view o for pivoted order data SELECT part_no , MAX (CASE WHEN lc_num = 1 THEN unit_price END) AS last_closed_price , MAX (CASE WHEN lc_num = 1 THEN qty_order END) AS last_closed_qty , MAX (CASE WHEN lc_num = 1 THEN date_closed END) AS last_closed_date , MAX (CASE WHEN fo_num = 1 AND ord_stat = 'OP' THEN unit_price END) AS first_open_price , MAX (CASE WHEN fo_num = 1 AND ord_stat = 'OP' THEN qty_order END) AS first_open_qty , MAX (CASE WHEN fo_num = 1 AND ord_stat = 'OP' THEN date_due_instock END) AS first_open_date , MAX (CASE WHEN lo_num = 1 AND ord_stat = 'OP' THEN unit_price END) AS last_open_price , MAX (CASE WHEN lo_num = 1 AND ord_stat = 'OP' THEN qty_order END) AS last_open_qty , MAX (CASE WHEN lo_num = 1 AND ord_stat = 'OP' THEN date_due_instock END) AS last_open_date FROM ( -- Begin in-line view to get lc_, fo_, lo_num SELECT gpo.* , ROW_NUMBER () OVER ( PARTITION BY part_no ORDER BY date_closed DESC , ord_no DESC ) AS lc_num , ROW_NUMBER () OVER ( PARTITION BY part_no ORDER BY CASE WHEN ord_stat = 'OP' THEN 1 ELSE 2 END , orig_dock_date , ord_no ) AS fo_num , ROW_NUMBER () OVER ( PARTITION BY part_no ORDER BY CASE WHEN ord_stat = 'OP' THEN 1 ELSE 2 END , orig_dock_date DESC , ord_no DESC ) AS lo_num FROM ( -- Begin in-line view gpo to get part_no SELECT ords.* , RTRIM ( CASE WHEN ord_part_no LIKE 'ORD-%' THEN SUBSTR (ord_part_no, 5) ELSE ord_part_no END ) AS part_no FROM ords ) gpo -- End in-line view gpo to get part_no ) -- End in-line view to get lc_, fo_, lo_num GROUP BY part_no ) o -- End in-line view o for pivoted order data WHERE RTRIM (p.part_no) = o.part_no ORDER BY p.part_no ;
Whenever you want to have a WHERE clause only applies to certain columns, think about a pivot and expression BOX.
Is there a reason to have raw triling part_no or ord_part_no?
Published by: Frank Kulash, February 5, 2010 13:26
After this announcement, I saw that Max had posted essentially the same query.
In general, where the solution of Max seems more simple, I like her way better.
The only possible exception is tested for "OP" at the derivation of the values first_open and last_open. If none of the lines to an ord_stat part_no = "OP", solution of Max will be used a the lines with another ord_stat. That does not occur in the sample data, and I don't know if this is still possible in your application, but if this is the case, I don't know this isn't what you want. -
Hi all
I use the 10.2.5 oracle database 64-bit on windows 64-bit.
I have the following tables
CREATE TABLE LABS ( "LAB_ID" NUMBER primary key, "LAB_NAME" VARCHAR2(30 BYTE) ); CREATE TABLE LAB_PC ( "LAB_PC_ID" NUMBER primary key, "LAB_ID" NUMBER references labs(lab_id), "PC_NAME" VARCHAR2(30 BYTE) ); CREATE TABLE FREE_PC ( "LOGIN" VARCHAR2(10 BYTE), "LOG_DATE_TIME" DATE, "LOG_USER" VARCHAR2(30 BYTE), "LOG_LAB" VARCHAR2(30 BYTE), "LOG_PC" VARCHAR2(30 BYTE), "LOG_STATUS" CHAR(1 BYTE);
LABORATORIES have all laboratories studying
LAB_PC have all computers in a particular laboratory.
FREE_PC contains the disconnection of a particular computer connection information in a particular LABORATORY. This table is populate text file using sql loader.
I want to show to the student how computers are available for use in special LABORATORY so that the student could decide which LAB they should go.
There is no relationship between FREE_PC and other tables. other paintings I create for the purpose of calculating or maybe I need this news.
Some examples of data is less.
Note:-LOG_STATUS = '1' means login and - LOG_STATUS = '0' means logout
insert into LABS values(1,'N419'); insert into LABS values (2,'N418'); insert into lab_pc values (1,1,'PC1'); insert into lab_pc values (2,1,'PC2'); insert into lab_pc values (3,1,'PC3'); insert into lab_pc values (4,1,'PC4'); insert into lab_pc values (5,2,'PC1'); insert into lab_pc values (6,2,'PC2'); insert into lab_pc values (7,2,'PC3'); insert into lab_pc values (8,2,'PC4'); Insert into FREE_PC values('LogIN','06-SEP-15','11s1248','N419','PC1','1'); --PC1 login Insert into FREE_PC values('LogOUT','06-SEP-15','11s1248','N419','PC1','0'); --PC1 logoff Insert into FREE_PC values('LogIN','06-SEP-15','11s1248','N419','PC1','1'); --PC1 login again Insert into FREE_PC values('LogIN','06-SEP-15','11s1248','N419','PC2','1'); --PC2login Insert into FREE_PC values('LogOUT','06-SEP-15','11s1248','N419','PC2','0'); --PC2logoff Insert into FREE_PC values('LogIN','06-SEP-15','11s1248','N418','PC3','1'); --PC3 login Insert into FREE_PC values('LogIN','06-SEP-15','11s1248','N418','PC4','1'); --PC4 login Insert into FREE_PC values('LogOUT','06-SEP-15','11s1248','N418','PC4','0'); --PC4 logoff Insert into FREE_PC values('LogIN','06-SEP-15','11s1248','N418','PC4','1'); --PC4 login COMMIT;
Now, I want to display the records like below
How to create this query?
Thank you.
Kind regards.
This is the query - the rest simulates just your tables - and I see nothing coded hard inside (except for log_status values)
Select lab_name, max (max_log_date_time) on total_available_pc, total_login_pc, total_pc, max_log_date_time)
from (select lab_name,
Max (max_log_date_time) max_log_date_time,
Count (*) total_pc,
Count (case lv when end 1 '1') total_login_pc,.
Count (case lv when '0' then 1 end) total_available_pc
from (select lp.lab_pc_id,
Max (l.lab_name) lab_name,
Max (FP.log_date_time) max_log_date_time,
LV NVL (Max (FP.log_status) Dungeon (dense_rank last order by fp.log_date_time), 0)
of lab_pc lp
inner join
laboratories l
on l.lab_id = lp.lab_id
left outer join
free_pc fp
On lp.pc_name = fp.log_pc
and l.lab_name = fp.log_lab
Lp.lab_pc_id group
)
Lab_name group
)
Concerning
Etbin
-
I have the following query:
Select nvl (iv.bed_num, fv.bed_num) as bed_num,
NVL (IV. Season, 2014) season.
NVL (IV.months, FV.months) months.
days_irrigated,
hours_irrigated,
times_fertilized,
amount_of_fert
from (select bed_num,
season,
TO_NUMBER (to_char(irrigation_date,'MM')) months,
Count (*) as days_irrigated,
Sum (Duration) as hours_irrigated
of irrigation_view
Bed_num group,
season,
TO_NUMBER (to_char (irrigation_date, 'MM'))) iv
full join
(select bed_num,
season,
TO_NUMBER (to_char(effort_date,'MM')) months,
Count (*) as times_fertilized,
flat as amount_of_fert
of fert_view
Bed_num group,
season,
TO_NUMBER (to_char (effort_date, 'MM'))) fv
On iv.bed_num = fv.bed_num
and iv.season = fv.season
and iv.months = fv.months;
The results are exactly what I need.
ex.
BED_NUM SEASON MONTHS DAYS_IRRIGATED HOURS_IRRIGATED TIMES_FERTILIZED AMOUNT_OF_FERT
---------- ---------- ---------- -------------- --------------- ---------------- --------------
10 2014 6 1 8.6
21 2014 5 1 22.8
24 2014 6 1 15.7
14 2014 5 1 2.85
8 2015 5 2 8
25 2015 5 2 8
26 2015 5 2 8
When I try the query results (using above query in a with clause) I get two results. If I put the condition 'season where = 2015' I retrieve all rows in 2015. "When I put the condition" where season = 2014 "no records are returned. When I leave off the where clause, all records are returned.
I also need to say all 2014 records are in the table fert_view and 2015 records from the irrigation_view.
Thanks for the help of Frank. I solved it. It seems that in my case for some reason any NVL() does not work as expected. I changed the NVL () COALESCE and it started working. If anyone knows why this is, please share your knowledge.
-
Dear all,
I have a sql like query below
SELECT min (G.TRANSACTION_DATE), D.REQUEST_NUMBER
MTL_TXN_REQUEST_HEADERS D,.
MTL_TXN_REQUEST_LINES, E.
MTL_MATERIAL_TRANSACTIONS G,.
Mtl_Transaction_Types I have
WHERE D.HEADER_ID = E.HEADER_ID
AND G.TRANSACTION_TYPE_ID = I.TRANSACTION_TYPE_ID
AND Upper (I.Transaction_Type_Name) = Upper ('TEC outcome')
AND E.LINE_ID = G.MOVE_ORDER_LINE_ID
AND D.MOVE_ORDER_TYPE = 5
TO_DATE (G.TRANSACTION_DATE) BETWEEN TO_DATE('01-JAN-2014') AND TO_DATE('31-DEC-2014')
D.REQUEST_NUMBER GROUP
I need to get the first number of the application and finally ask for number based on the date of the transaction, how can I get the number of name application and based on the date of the transaction for the same query, please help me.
above query is back under results
If the query should return a line with the number of application like 2383 based on minimum transaction date and another column based on the maximum transaction date, please help me.
so in the example above, it must return
FIRST REQUEST NUMBER LAST NUMBER
2383 1886
Thank you
select min(request_number) keep(dense_rank first order by transaction_date asc ) request_number_min , min(request_number) keep(dense_rank first order by transaction_date desc) request_number_max from ( select min(g.transaction_date) transaction_date , d.request_number from mtl_txn_request_headers d , mtl_txn_request_lines e , mtl_material_transactions g , mtl_transaction_types i where d.header_id = e.header_id and g.transaction_type_id = i.transaction_type_id and upper(i.transaction_type_name) = upper('WIP Issue') and e.line_id = g.move_order_line_id and d.move_order_type = 5 and to_date(g.transaction_date) between to_date('01-jan-2014') and to_date('31-dec-2014') group by d.request_number )
-
Need help with sql query performance
Dear all,
I have a sql like query below, I need to give the following query please help me identify which statement I should tune to have better performanece.
Select rownum LINE_NUM,
A.LINE_ID,
TO_CHAR (A.INVITMID),
TO_NUMBER (A.PICKQTY),
UNLOADINGPNT NULL,
RRNUM NULL,
WORKORDNUM NULL,
WORKORDDESC NULL,
A.PONUM,
DTR_DUMB NULL,
A.DESCRIPTION,
FROM_SUB NULL,
TO_SUB NULL,
NO SOURCE,
ASSET_NUMBER NULL,
A.RECEIPTNUM,
MOVEORD NULL,
FROM_LOC NULL,
TO_LOC NULL,
MSD_NUM NULL,
CONTAIN_LINE NULL,
A.UOM,
A.PO_RELEASE
de)
Select headerid Po.Po_Header_Id,
rcv1. Po_Line_Id LINE_ID,
rcv1.item_id INVITMID,
(NVL(Rcv1.Transact_Qty,0)-NVL(rcv2.transact_qty,0)) PICKQTY,
Po.Segment1 PONUM,
Rcv1.Receipt_Num RECEIPTNUM,
Rcv1.Item_Desc DESCRIPTION,
Rcv1.Transact_Uom GLU,
Rcv1.Po_release
Po_Headers_All in.,.
(Select rcv3. Po_Header_Id, RCV3.receipt_num, rcv3. Po_Line_Id, rcv3. Destination_Type_Code, rcv3. Item_Id, rcv3. Item_Desc, rcv3. Transact_Uom, SUM (rcv3. Transact_Qty) Transact_Qty, rcv3. PO_RELEASE OF)
SELECT A.Po_Header_Id,
C.RECEIPT_NUM receipt_num,
A.Po_Line_Id,
A.Destination_Type_Code,
B.Item_Id,
B.item_description Item_Desc,
A.UNIT_OF_MEASURE Transact_Uom,
A.QUANTITY Transact_Qty,
D.RELEASE_NUM PO_RELEASE
OF RCV_TRANSACTIONS,.
RCV_SHIPMENT_HEADERS C.
B RCV_SHIPMENT_LINES,
PO_RELEASES_ALL D
WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID
AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID
AND UPPER (A.Transaction_Type) = "to DELIVER".
AND higher (A.Destination_Type_Code) = "EXPENSES".
AND D.PO_RELEASE_ID = A.PO_RELEASE_ID
UNION ALL
SELECT A.Po_Header_Id,
C.RECEIPT_NUM receipt_num,
A.Po_Line_Id,
A.Destination_Type_Code,
B.Item_Id,
B.item_description Item_Desc,
A.UNIT_OF_MEASURE Transact_Uom,
A.QUANTITY Transact_Qty,
D.RELEASE_NUM PO_RELEASE
OF RCV_TRANSACTIONS,.
RCV_SHIPMENT_HEADERS C.
B RCV_SHIPMENT_LINES,
PO_RELEASES_ALL D
WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID
AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID
AND B.ITEM_ID IS NULL
AND UPPER (A.Transaction_Type) = "to DELIVER".
AND higher (A.Destination_Type_Code) = "WORKSHOP".
D.PO_RELEASE_ID AND = A.PO_RELEASE_ID) rcv3
GROUP BY rcv3. Po_Header_Id, RCV3.receipt_num, rcv3. Po_Line_Id, rcv3. Destination_Type_Code, rcv3. Item_Id, rcv3. Item_Desc, rcv3. Transact_Uom, rcv3. Rcv1 PO_RELEASE),
(SELECT A.PO_LINE_ID,
Sum (A.Quantity) transact_qty,
A.PO_HEADER_ID,
C.RECEIPT_NUM
OF RCV_TRANSACTIONS,.
RCV_SHIPMENT_HEADERS C.
B RCV_SHIPMENT_LINES,
PO_RELEASES_ALL D
WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID
AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID
AND UPPER (A.Transaction_Type) = "RETURN to the RECEPTION"
AND D.PO_RELEASE_ID = A.PO_RELEASE_ID
A.PO_LINE_ID, A.PO_HEADER_ID, C.RECEIPT_NUM GROUP) Rcv2
Where Po.Po_Header_Id = Rcv1.Po_Header_Id (+)
And Rcv1.Po_Line_Id = Rcv2.Po_Line_Id (+)
And Rcv1.Receipt_Num = Rcv2.Receipt_Num (+)
And Rcv1.Transact_Qty <>Nvl(Rcv2.Transact_Qty,999999999)
Group of po.po_header_id, rcv1.po_line_id, po.segment1, rcv1.receipt_num, rcv1.item_id, Rcv1.Item_Desc, rcv1. TRANSACT_UOM, rcv1. PO_RELEASE, (NVL(Rcv1.Transact_Qty,0)-NVL(RCV2.transact_qty,0))) has
Is my version of the database: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production
Please find the attached PLAN to EXPLAIN.
Thank you
May be essentially the same join (between a, b, c and d) twice instead of three times
sounds like it could be done with a single join (between a, b, c, and d), but you do not some columns than the columns of rcv1 rcv2
We can work on what we can see only
Select rownum line_num,
rcv1.po_line_id line_id,
TO_CHAR (rcv1.item_id) invitmid,
NVL(rcv1.transact_qty,0) - nvl(rcv2.transact_qty,0) pickqty,
unloadingpnt null,
rrnum null,
workordnum null,
workorddesc null,
Po. Ponum Segment1,
dtr_dumb null,
description of the rcv1.item_desc,
from_sub null,
to_sub null,
No source,
asset_number null,
rcv1.receipt_num receiptnum,
moveord null,
from_loc null,
to_loc null,
msd_num null,
contain_line null,
Glu rcv1.transact_uom,
rcv1.po_release
of po_headers_all in.
left outer join
(select a.po_header_id,
c.receipt_num,
a.po_line_id,
a.destination_type_code,
b.item_id,
b.item_description item_desc,
a.unit_of_measure transact_uom,
Sum (a.Quantity) transact_qty,
d.release_num po_release
from (select shipment_header_id,
shipment_line_id,
po_release_id,
po_header_id,
po_line_id,
destination_type_code,
unit_of_measure,
quantity
of rcv_transactions
where upper (a.transaction_type) = "to DELIVER".
and upper (a.destination_type_code) ('charge', 'WORKSHOP')
) a
inner join
rcv_shipment_lines b
On a.shipment_line_id = b.shipment_line_id
inner join
c rcv_shipment_headers
On a.shipment_header_id = c.shipment_header_id
left outer join
po_releases_all d
On a.po_release_id = d.po_release_id
where upper (a.destination_type_code) = "EXPENSES".
or (upper (a.destination_type_code) = 'WORKSHOP'
and b.item_id is null
)
A.po_header_id group,
c.receipt_num,
a.po_line_id,
a.destination_type_code,
b.item_id,
b.item_description,
a.unit_of_measure,
d.release_num
) rcv1
On po.po_header_id = rcv1.po_header_id
left outer join
(select a.po_line_id,
Sum (a.Quantity) transact_qty,
a.po_header_id,
c.receipt_num
from (select shipment_header_id,
shipment_line_id,
po_release_id,
po_header_id,
quantity
of rcv_transactions
where upper (a.transaction_type) = "RETURN to THE RECIPIENTS.
) a
inner join
rcv_shipment_lines b
On a.shipment_line_id = b.shipment_line_id
inner join
c rcv_shipment_headers
On a.shipment_header_id = c.shipment_header_id
left outer join
po_releases_all d
On a.po_release_id = d.po_release_id
A.po_line_id group,
a.po_header_id,
c.receipt_num
) rcv2
On rcv1.po_line_id = rcv2.po_line_id
and rcv1.receipt_num = rcv2.receipt_num
where rcv1.transact_qty! = nvl(rcv2.transact_qty,999999999)
Concerning
Etbin
-
Need help with the query to get the County
Hello
Oracle 10 g 2 10.2.0.3 - 64 bit
I want back the number of accounts with two different types of funds (say A and B). Some accounts hold only one of the two funds, and some support both. I want to get the counts like this:
- account held funds - has only
- accounts holding funds-B only
- accounts holding the Fund-A and B funds
Here is what I started with but need assistance to meet the requirement above:
select count(distinct acct.bkoff_acct_no ) accounts_holding_fund_A from xe_account acct, xec_tal_investment_mandate iman, xec_tal_asset_allocation alloc, xe_benchmark bmark, xe_benchmark_usage bu, xe_object_description xod, xec_asset_class cls where iman.mandate_status_cd='A' and cls.asset_class_cd = alloc.asset_class_cd and iman.mandate_id = alloc.mandate_id and acct.account_id = iman.object_id and iman.object_type_cd = 'ACCT' and iman.mandate_id = xod.object_id and xod.field_nm='XEC_TAL_INVESTMENT_MANDATE.COMMENT_TXT' and xod.language_cd = 'E' and acct.acct_status_cd = 'O' and bu.object_type_cd(+) = 'TMAA' and bu.object_id(+) = alloc.asset_allocation_id and bmark.benchmark_id(+) = bu.benchmark_id and alloc.resp_txt like '%fund-A%'
And suppose that the Fund-B has resp_txt like ' % of Fund-B»
Please suggest.
Concerning
Hello
Here is another way, it is easier to adapt to different jobs and different numbers of jobs:
WITH got_distinct_jobs AS
(
SELECT DISTINCT deptno, job
FROM scott.emp
WHERE job IN ("ANALYST", "CLERKS") - or what
)
got_job_list AS
(
SELECT LISTAGG (job, ",") THE Group (ORDER BY work) AS job_list
OF got_distinct_jobs
GROUP BY deptno
)
SELECT job_list
COUNT (*) AS num_departments
OF got_job_list
GROUP BY job_list
;
This shows all the combinations of the jobs listed in the WHERE clause of got_distinct_jobs. You don't need to change anything else in the query. There may be any number of jobs.
Output:
JOB_LIST NUM_DEPARTMENTS
------------------------------ ---------------
CLERK 2
ANALYST, CLERK 1
-
Need help with the query (transpose)
Hello experts, please help here - 11 g Oracle, attribute APA APB are fixed values and can be hard coded in query
Need to transpose these data. Max(decode.. perd les données en sortie à cause de la condition de Max.)
Entry:
Name | Attribute | Value
A1 APP 10
A1 PDB 11
A1 APA 20
A1 PDB 21
A2 BPA 13
A2 BPB 14
Expected results:
Name AttVal1 AttVal2
A1 10 11
13 14 A2
20 21 A1
Hello
Here's one way:
WITH relevant_columns AS
(
SELECT name
value
CASE
WHEN the attribute ("APA", "BPA") THEN 1
WHEN the attribute ("PDB", "BPB") THEN 2
END AS c_num
ROW_NUMBER () (PARTITION BY NAME, attirbute
Value of ORDER BY
) AS r_num
FROM table_x
)
SELECT name, attval1, attval2
OF relevant_columns
PIVOT (MAX (value)
FOR c_num IN (1 AS attval1
2 UNDER attval2
)
)
ORDER BY r_num, name
;
If you would care to post CREATE TABLE and INSERT statements for your sample data, and then I could test this.
Why do you want to
NAME ATTVAL1 ATTVAL2
---- ------- -------
A1 10 11
20 21 A1
in the results, rather than
NAME ATTVAL1 ATTVAL2
---- ------- -------
10 21 A1
20 11-A1
? You would be satisfied to one or the other?
Depending on your answer, you may need to modify the analytical ORDER BY clause in the ROW_NUMBER function.
Maybe you are looking for
-
I was thinking about a MAC Mini, In fact, what comes with the Mac Mini when it was purchased?
-
Password changed in gmail - now Thunderbird does not accept
Gmail last night asked me to change my password. I did it. This morning I changed in Thunderbird. He worked all day until about 17:00 suddenly is no longer will accept it. I get a message saying that the connection has failed. I can not receive or se
-
Why your listing on the page isn't launched everytime I open Firefox? Very annoying!
Why the card layout is launched whenever I open Firefox? He reads all of my plugins are up to date.It's driving me crazy!
-
HP Mini 110-1125LA: HP Mini 110-1125LA - how to reset the password for bios
Hello guys that I need your help to reset my bios password for HP Mini 110-1125LA. NUMBER: CNU94426HZ Thank you
-
126 a HP Imaging drum: introductory area imaging drum
Hello It's I want to know how many prints (performance) can get hp laserjet 126 a initiation Imaging drum?