Complex SQL help
HY there!
I have a question of work and I need to change, but my eyes and the brain is a little tired.
So, here is what I got so far:
"
Select
S6 NVL (t.cost_key, ln.attribute1).
Bev_datum To_Char (NVL (Max (t.trx_date), MAX (mmt.transaction_date)), 'DD-MON-YYYY'),
NVL (Max (t.trx_quantity), SUM (mmt.primary_quantity)) menny,
MMT.transaction_uom me,
MMT.currency_code devn,
MMT.currency_conversion_rate arfolyam,
MMT.actual_cost bev_egys,
MMT.actual_cost / decode (nvl (mmt.currency_conversion_rate, 0), 0, 1,)
elk_dev_egys MMT.currency_conversion_rate),
MMT.actual_cost * nvl (MAX (t.trx_quantity), bem SUM (mmt.primary_quantity))
MMT.actual_cost + sum (teny_ktg1) /nvl (MAX (t.trx_quantity), SUM (mmt.primary_quantity)) t_onk
Of
mtl_material_transactions mmt,
mtl_transaction_lot_numbers tln,
mtl_lot_numbers ln,
oaes_cogs_transactions t,
(
Select e1.cost_key teny_s61,
substr (G1. Segment2, 6, 1) ktg_tip1.
T1.trx_quantity menny1,
T1.bev_egys bev_egys1,
Sum (E1.cost_base_amount) teny_ktg1
Of
oaes_cogs_cost_elements e1,
gl_code_combinations g1,
(select distinct tc1.cost_key, tc1.trx_quantity, mmt1.actual_cost bev_egys
of oaes_cogs_transactions tc1,
mtl_material_transactions mmt1
where
TC1.trx_id = mmt1.transaction_id
and tc1.trx_type = 'RÉCEPTION') t1
where e1.cost_ccid = g1.code_combination_id
and e1.element_type = 'INVOICE '.
and t1.cost_key = e1.cost_key
and substr (g1.segment2, 1, 5) = "26211 '
Group of e1.cost_key, substr(g1.segment2,6,1), t1.trx_quantity, t1.bev_egys
)
where mmt.transaction_id = tln.transaction_id
and t.cost_key = teny_s61
and mmt.organization_id = tln.organization_id
and tln.organization_id = ln.organization_id
and mmt.inventory_item_id = tln.inventory_item_id
and tln.inventory_item_id = ln.inventory_item_id
and tln.lot_number = ln.lot_number
and mmt.transaction_type_id in (18: 41)
and t.trx_id (+) = mmt.transaction_id
AND t.trx_type (+) = "RECEPTION".
AND t.inventory_item_id = mmt.inventory_item_id
/ * test purpose to ensure that each value is correct
and nvl (t.cost_key, ln.attribute1) = '9731_120-0064-2_9603.
*/
GROUP OF t.cost_key, mmt.actual_cost,
LN.attribute1, mmt.transaction_uom, mmt.currency_code,
MMT.currency_conversion_rate, mmt.actual_cost
"
The following line may be NULL - > mmt.actual_cost + sum (teny_ktg1) /nvl (MAX (t.trx_quantity), SUM (mmt.primary_quantity))
If there is NOTHING I want to return to the next value-> mmt.actual_cost
I tried if then else and case when, but I got error so far.
Thanks in advance for your help
Zsolt
Have you tried NVL?
MMT.actual_cost + NVL (sum (teny_ktg1) /nvl (MAX (t.trx_quantity), SUM (mmt.primary_quantity)), 0)
Tags: Database
Similar Questions
-
Help needed on complex sql resolution
Hi all
I have a table such as indicated and wanted to insert these records in both tables and get the number of each type...
I asssume we can get the account once the algorithm sucssefully inserts the date in the tables.CREATE TABLE RETURN_DEV ( RCODE VARCHAR2(4 BYTE), RNUMBER VARCHAR2(16 BYTE), RDESC VARCHAR2(40 BYTE), RDATE_TIME VARCHAR2(16 BYTE), RQUANTITY VARCHAR2(20 BYTE) ) Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0987', '919341035404586', 'POS:00003:RETR', '20100112112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0910', '929341035404586', 'POS:00003:REMR', '20100102112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0914', '939341035404586', 'POS:00003:REMR', '20100111112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0997', '357991027440360', 'POS:00003: WEXC', '20100102112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0997', '347991027440360', 'POS:00003: WEXC', '20100102112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0987', '949341035404586', 'POS:00003:SAXP', '20100512112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0956', '949341035404586', 'POS:00003:RETR', '20100212112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0968', '949341035404586', 'POS:00003:SAXP', '20100312112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0967', '997991027440360', 'POS:00003: USXP', '20060102112324', '-1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0967', '997991027440360', 'POS:00003: USXP', '20070102112324', '1'); Insert into RETURN_DEV (RCODE, RNUMBER, RDESC, RDATE_TIME, RQUANTITY) Values ('0967', '997991027440360', 'POS:00003: USXP', '20080102112324', '-1'); COMMIT; Now my Algorithm go like this step 1: Load 'RET' type into Retail_Table and Count 'RET' type If the RCODE is not in [0910, 0914, 0997] and the RDESC begins with "POS:00003" and RQUANTITY =+1 load these as 'RET' type Step2: Load 'REM' type type into Retail_Table and Count 'REM' type If the RCODE is in [0910, 0914] and RQUANTITY = +1, load these as 'REM' type Step3: Load 'WEX' type into Retail_Table and Count 'WEX' type If the RCODE = 0997 and RQUANTITY = +1, load these as 'WEX' type For me main problem starts here.... Step4: Load'RET' INTO type and 'RETXP' into retail_exceptions, and update the count of 'RET' Type and count 'RETXP' type If the RCODE is not in [0910, 0914, 0997] and the RDESC begins with "POS:00003" and if there is more than one record fitting that criteria in the file for the RNUMBER and the newest record chronologically (RDATE_TIME) has RQUANTITY = +1, then load the latest record as Retail Returns and load all other records into retail_exceptions table. Step5: Load Retail Returns Exceptions as 'RETXP', update the count of Retail Returns and add the count of exceptions If the RCODE is not in [0910, 0914, 0997] and the RDESC begins with "POS:00003" and if there is more than one record fitting that criteria for this RNUMBER and the newest record chronologically (RDATE_TIME) has RQUANTITY = -1, then load all records into to the Retail exceptions table
Thanks in advance...Both the table has the same structure as shown.. CREATE TABLE Retail_Table ( RCODE VARCHAR2(4 BYTE), RNUMBER VARCHAR2(16 BYTE), RDESC VARCHAR2(40 BYTE), RDATE_TIME VARCHAR2(16 BYTE), RTYPE VARCHAR2(20 BYTE) ) CREATE TABLE Retail_Table_Exceptions ( RCODE VARCHAR2(4 BYTE), RNUMBER VARCHAR2(16 BYTE), RDESC VARCHAR2(40 BYTE), RDATE_TIME VARCHAR2(16 BYTE), RTYPE VARCHAR2(20 BYTE) )
Published by: Rede on October 19, 2010 16:20Hello
Sorry, I don't understand still.
Rede says:
Thanks Frank.I thought to explain clearly, but it became huge, then I'll be more clear and precise...I expect the output as shown below
Output Sample data of Retail Table.... RDESC RQUANTITY RNUMBER TRANSACTION_TYPE POS:00003: USXP -1 997991027440360 RET POS:00003: WEXC 1 347991027440360 WEX POS:00003: WEXC 1 357991027440360 WEX POS:00003:REMR 1 929341035404586 REM POS:00003:REMR 1 939341035404586 REM out put sample data of Retail_Exception Table RDESC RQUANTITY RNUMBER TRANSACTION_TYPE POS:00003: USXP -1 997991027440360 RETXP POS:00003: USXP 1 997991027440360 RETXP POS:00003:RETR 1 949341035404586 RETXP POS:00003:SAXP 1 949341035404586 RETXP
CREATE orders TABLE for the retail and retail_exception do not have a rquantity column. If they have this column?
Are the other columns (rcode and rdate_time) of retail and retail_exception supposed to be NULL at this point?What does each line in these tables?
Why there are 9 rows in both tables combined, instead of 11?What i want is individaul counts of all trasaction types.If we can get the counts at time of DML querry it will be good. But i want to keep them in a local variable thats the reason i want to do it in a seperate sql querry. as shown below Transaction_Type Count RETXP 4 WEX 2 RET 2
Please explain how you get these results.
Why is the number 2 when transaction_type = "RET"? Why not 1?
Why don't you want a line to transaction_type = "REM"?These are all numeric values only. since i am getting these values from a external table i kept them as Varchar's. But i will change them accordingly.
What i am trying to do is i grouping the items on the different conditions given below 1. When RCODE In ('0997') And Rquantity = '1' Then 'WEX' 2. When RCODE In ('0910', '0914') And Rquantity = '1' Then 'REM' 3. When RCODE Not In ('0997', '0910', '0914') And RDesc Like 'POS:00003%' And Count ( * ) Over (Partition By RNUMBER) = 1 Then 'RET' its easy till above...but if the third condition has more than 1 record and if the top most record has 4. Rquantity = '-1' then categorise all the records 'RETXP' of that type ( that satisfies the third condition) else if top most record 5. Rquantity = '1' then first record should be categorised as 'RET' and the remaining of that type as 'RETXP'. top most is the record that is sorted by date for a given RNumber.
What is the "first record"? Is it the same as the 'highest '?
Please give specific examples of how you get the desired results of the sample data that you posted.
For example, how do you get the output line:
RDESC RQUANTITY RNUMBER TRANSACTION_TYPE --------------- --------- --------------- ---------------- POS:00003: USXP -1 997991027440360 RET
This line of output appears to be the 'top most' line for rnumber = '997991027440360':
RCODE RNUMBER RDESC RDATE_TIME RQUANTITY ----- ---------------- ------------------ ---------------- --------- 0967 997991027440360 POS:00003: USXP 20060102112324 -1 0967 997991027440360 POS:00003: USXP 20070102112324 1 0967 997991027440360 POS:00003: USXP 20080102112324 -1
Rule 4 does not apply to this rnumber? Isn't the line with rdate_time = '20060102112324' the line ' top more? It is not rquantity = '-1'?
You can do the simple CASE expression (and probably faster) some of the functions of calculation only once, in a subquery, like this:
WITH got_analytics AS ( SELECT rd.* , COUNT (*) OVER ( PARTITION BY rnumber) AS rnumber_cnt , FIRST_VALUE (rquantity) OVER ( PARTITION BY rnumber ORDER BY rdate_time ) AS top_rquantity , ROW_NUMBER () OVER ( PARTITION BY rnumber ORDER BY rdate_time ) AS seq_number FROM return_dev rd ) SELECT rdesc , rquantity , rnumber , CASE WHEN rcode IN ('0997') AND rquantity = '1' THEN 'WEX' WHEN rcode IN ('0910', '0914') AND rquantity = '1' THEN 'REM' WHEN rcode IN ('0997', '0910', '0914') AND rdesc NOT LIKE 'POS:00003%' THEN NULL WHEN rnumber_cnt = 1 THEN 'RET' WHEN top_rquantity = '-1' THEN 'RETXP' WHEN top_rquantity != '1' THEN NULL WHEN seq_number = 1 THEN 'RET' ELSE 'RETXP' END AS transaction_type FROM got_analytics ;
I know that's not what you want, but I think that it is a step in the right direction.
-
Hello
I have a MyCounter table that is used to get the sequence numbers after N.
For purposes of simplicity, let's just table MyCounter has 2 fields - maxNumber and currentNumber.
maxNumber is the maximum sequence number that can be allocated.
currentNumber is the last (current) sequence number.
Assuming maxNumber is 1000, currentNumber is 500. If N is 10, the sequence should I get numbers: From = To = 510, 501
I am new to Oracle. I had a first blow of dagger to write the SQL to update, but I am not convinced that it works.
Can anyone give me suggestions please? Thank you...
---------
DECLARE
v_origValue NUMBER (10,0): = 0;
v_allocated NUMBER (10,0): = 0;
V_quantity NUMBER (10,0): = 0;
BEGIN
V_quantity: = 10; -assuming that 10 is a constant at this stage
UPDATE MyCounter
SET v_allocated = box
When (currentNumber + v_quantity < = maxNumber) then restart
else (maxNumber-currentNumber)
end,
v_origValue = (currentNumber),
currentNumber = (currentNumber + v_allocated)
WHERE the currentNumber < maxNumber;
SELECT v_origValue + 1 as 'from', v_origValue + v_allocated 'to '.
FROM DUAL;
END;
---------
Kind regards
JudyHere is a small example of what you could do.
SQL> create table t(type varchar2(10),max_val number, curr_val number) 2 / Table created. SQL> insert into t values('type1',100,0) 2 / 1 row created. SQL> insert into t values('type2',1000,500) 2 / 1 row created. SQL> create or replace type my_seq as table of number 2 / Type created. SQL> create or replace procedure get_sequence(ptype in varchar2, pcount in number, pseq out my_seq) 2 as 3 lcurr_val t.curr_val%type; 4 lmax_val t.max_val%type; 5 max_seq_exception exception; 6 begin 7 update t 8 set curr_val = curr_val + pcount 9 where type = ptype 10 returning curr_val, max_val into lcurr_val, lmax_val; 11 12 if lcurr_val > lmax_val 13 then 14 rollback; 15 raise max_seq_exception; 16 else 17 select lcurr_val - (level-1) 18 bulk collect into pseq 19 from dual 20 connect by level <= pcount 21 order by level desc; 22 23 commit; 24 end if; 25 exception 26 when max_seq_exception then 27 raise_application_error(-20001,'Max sequence exceeded'); 28 end; 29 / Procedure created. SQL> declare 2 lseq my_seq; 3 begin 4 get_sequence('type1',10,lseq); 5 for i in 1..lseq.count 6 loop 7 dbms_output.put_line(lseq(i)); 8 end loop; 9 end; 10 / 1 2 3 4 5 6 7 8 9 10 PL/SQL procedure successfully completed. SQL> select * from t 2 / TYPE MAX_VAL CURR_VAL ---------- ---------- ---------- type1 100 10 type2 1000 500 SQL> declare 2 lseq my_seq; 3 begin 4 get_sequence('type1',100,lseq); 5 for i in 1..lseq.count 6 loop 7 dbms_output.put_line(lseq(i)); 8 end loop; 9 end; 10 / declare * ERROR at line 1: ORA-20001: Max sequence exceeded ORA-06512: at "SYSADM.GET_SEQUENCE", line 27 ORA-06512: at line 4
But do not forget in a multiuser environment, there are many chances for you to get screwed!. Sequence is the best option in my opinion.
Published by: Karthick_Arp on November 30, 2008 21:14
-
Need help: complex SQL statement
Select segment3, 0 "other income."
, 0 "Sales Services".
, 0 "staff costs".
, 0 "other staff costs".
, 0 'Deputy '.
, 0 'trip '.
, 0 "collaborator".
, 0 'training '.
, 0 'capital '.
, nvl (round (sum (nvl(PERIOD_ACTIVITY_A,0) + nvl(PERIOD_ACTIVITY_E,0) + nvl(PRDOPEN_A,0) + nvl(PRDOPEN_E,0)), 0), 0) "Overhead"
, 0 "financial expenses".
of PAN. SNP_OP_DETAIL@OFA
where segment3 = x.new_cc and period_name = p_period
and segment2 in (select acc_t from the CRP_REPORT_ACCT_DETAIL_T where group_id = 10)
Group of segment3
Please, I do not understand select segment3, 0 'other income'... There is no column called 0, what is the 0s in the select statement?
Secondly, I don't understand PAN. SNP_OP_DETAIL@OFAMeans says:
Select segment3, 0 "other income."
, 0 "Sales Services".
, 0 "staff costs".
, 0 "other staff costs".
, 0 'Deputy '.
, 0 'trip '.
, 0 "collaborator".
, 0 'training '.
, 0 'capital '.
, nvl (round (sum (nvl(PERIOD_ACTIVITY_A,0) + nvl(PERIOD_ACTIVITY_E,0) + nvl(PRDOPEN_A,0) + nvl(PRDOPEN_E,0)), 0), 0) "Overhead"
, 0 "financial expenses".
of PAN. SNP_OP_DETAIL@OFA
where segment3 = x.new_cc and period_name = p_period
and segment2 in (select acc_t from the CRP_REPORT_ACCT_DETAIL_T where group_id = 10)
Group of segment3Please, I do not understand select segment3, 0 'other income'... There is no column called 0, what is the 0s in the select statement?
0 'other income' means to select a literal value of 0 (zero) with an alias of "other income". double quotes preserve the space and upper/lower case in the alias. This alias (in space) will be difficult to use because of the so called space later.
Secondly, I don't understand PAN. SNP_OP_DETAIL@OFA
PAN is probably a schema, SNP_OP_DETAIL a table or view and @OFA there is a link of database indicating the table in a different database
-
TARGET RESULT COMPLEX SQL QUERY
Hello
I'm writing a sql, for the scenario below, but don't get it.
There are two Table TEMP_AAA and TEMP_BBB,
Under the table, we have values in descending order C_ID,
Now to give a value in D_ID,.
We must find the value of C_ID
and if this C_ID value is contained in the B_ID,
then we should display the value of allowance A_ID,
The complexity here is,
When you come down (in fact different levels) lines of C_ID,
already consumed values should not be used.
In this example,.
for D_ID ABC says
We have 1234 as C_ID and
B_ID is 12345 which contains C_ID
A_ID coresponding is 111 and 222.
Next step for D_ID (same level): DEF since the C_ID is the same as the process described above
We have A_ID 111 and 222.
Then for D_ID GHI.
We have 12 as C_ID and
B_ID is 12345,12345,12045, which contains the C_ID
A_ID coresponding allocation is 111,222,333,
Now since 111 and 222 is already used in the ABC/DEF D_ID we would not have considered.
A_ID would be 333
Then for D_ID MNO.
We have NULL as C_ID, which means all B_ID default matches (special account)
B_ID is 12345,12345,12045,10045, which contains C_ID (default)
A_ID coresponding allocation is 111,222,333,444
Now given that 111,222,333 is already used in D_ID ABC/DEF/GHI/JKL we would not have considered.
A_ID would be 444.
Here is the Script and exit
EXPECTED RESULTSCREATE TABLE TEMP_AAA( A_ID VARCHAR2(10), B_ID VARCHAR2(10)); CREATE TABLE TEMP_BBB( C_ID VARCHAR2(10), D_ID VARCHAR2(10)); INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('111','12345'); INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('222','12345'); INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('333','12000'); INSERT INTO TEMP_AAA(A_ID,B_ID) VALUES('444','10000'); INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('1234','ABC'); INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('1234','DEF'); INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('12','GHI'); INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES('12','JKL'); INSERT INTO TEMP_BBB(C_ID,D_ID) VALUES(NULL,'MNO');
---------------
D_ID RESULT A_ID --------------- ABC 111 ABC 222 DEF 111 DEF 222 GHI 333 JKL 333 MNO 444
Hello
Thanks for posting the CREATE TABLE and INSERT statements; This helps a lot.
Interesting problem!
For each allocation a_id, you have to find the number of times where a c_id equaled and choose only the lines where it is the 1st time.
This sounds like a job for the analytic RANK function:WITH got_c_id_rank AS ( SELECT b.d_id , a.a_id , RANK () OVER ( PARTITION BY a_id ORDER BY c_id DESC NULLS LAST ) AS c_id_rank FROM temp_aaa a JOIN temp_bbb b ON a.b_id LIKE b.c_id || '%' ) SELECT d_id , a_id AS result_a_id FROM got_c_id_rank WHERE c_id_rank = 1 ORDER BY d_id , result_a_id ;
-
Problem SQL HELP during the installation of aviation the interactive dvd course!
try to download interactive dvd Aviation course and be sktc kingschool SQL error does not exist... Check the State of SQL express instance and I install the classes I get a SsRestart occurred must close and send error report... Any help would be great! Help!
I suggest that you contact the manufacturer of the interactive DVD software to help get the program installed.
They would be better able to help with your problem.
Thank you
Marilyn
-
SQL Help: Add flag for several tables
Hello
I have an existing SQL as follows:
Select the storeId field,
(select case when count (1) > 0 0 otherwise then 1 end)
of tb_bbb
where tb_bbb.id = tb_base.baseId) AS conInd
of tb_base;
I would now add the table tb_ccc for the value of conInd. I try the following SQL:
Select the storeId field,
(select case when count (1) > 0 0 otherwise then 1 end)
Of
(select tb_bbb.id where tb_bbb.id = tb_base.baseId)
Union
Select tb_ccc.id where tb_ccc.id = tb_base.basdId) AS conInd
of tb_base;
However, it does not work. Any idea on this SQL? Thanks for your help!
Hello
Here is another way, which may be more effective because it uses EXISTS (which can quit smoking as soon as it finds a match) instead of COUNT (who continues to go, so he can tell you if there are 999 or 1000 matches, even if you don't care to).
SELECT storeid
CASE
WHEN THERE IS)
SELECT 1
OF tb_bbb
WHERE bbb_id = m.baseid
) 1 THEN
WHEN THERE IS)
SELECT 1
OF tb_ccc
WHERE ccc_id = m.baseid
) 1 THEN
0 OTHERWISE
END AS conlnd
OF tb_base m
;
-
Hi all
I need correct column value serNo to the right as a result. It suppose to be the sequence number for each storeID. Any suggestion on the SQL?
create table test1 (storeID number (5), number serNo (5));
insert into table test1 values (100, 1);
insert into table test1 values (200, 1);
insert into table test1 values (100, 1);
insert into table test1 values (200, 1);
insert into table test1 values (100, 1);
insert into table test1 values (100, 1);
I want to have the following results in test1 after conversion:
1 100
2 100
3 100
4 100
1 200
2 200
Thank you
Hello
942572 wrote:
Hi all
I need correct column value serNo to the right as a result. It suppose to be the sequence number for each storeID. Any suggestion on the SQL?
create table test1 (storeID number (5), number serNo (5));
insert into table test1 values (100, 1);
insert into table test1 values (200, 1);
insert into table test1 values (100, 1);
insert into table test1 values (200, 1);
insert into table test1 values (100, 1);
insert into table test1 values (100, 1);
I want to have the following results in test1 after conversion:
1 100
2 100
3 100
4 100
1 200
2 200
Thank you
Use the ROW_NUMBER analytic function to generate the new sernos, like this:
MERGE INTO dst test1
WITH THE HELP OF)
SELECT ROWID AS r_id
ROW_NUMBER () OVER (PARTITION BY storeid
ORDER BY NULL
) AS serno
OF test1
) src
ON (summer time. ROWID = src.r_id)
WHEN MATCHED THEN UPDATE
SET dst.serno = src.serno
;
Really, your table must have a primary key. Because it's not working, I had to ROWID allows you to uniquely identify the lines.
-
SQL Help: How to collect summary number in group by?
HelloI have the following table:
create table tb_class_info (classNbr number (5), number (2) of ClassType, classTeacherNbr number (4));
insert into tb_class_info values (101, 1, 12);
insert into tb_class_info values (001, 2, 12);
insert into tb_class_info_values (001, 2, 13);
insert into tb_class_info_values (002, 2, 12);
insert into tb_class_info_values (002, 2, 12);
I would like to get statistics on the cnt, cnt for classType = 2 classTeacherNbr classNbr. I expect to have the following results:
classNbr classType2Cnt classTeacherCnt
101 0 0
001 2 2
002 2 1
The following SQL code I use:
SELECT classNbr,
SUM (CASE when classType = 2 THEN 1 ELSE END 0) as classTypeCnt,
SUM (CASE when classType = 2 SO unique classTeacherNbr ELSE 0 END) as classTeacherCnt
OF tb_class_info
GROUP BY classNbr;
However, I have the ' ORA-00905: lack of keyword ' error. Any suggestion on this subject?
Thanks for your help!
Select classnbr
sum (case when classtype = 2 then 1 end)
, count (distinct case when classtype = 2 then teachernbr end)
of tb_class_into
Classnbr group;
-
I'm pretty new in pl/sql, I want to update a column with the next number.
Here's an example-
Table: political
Policy_no Sequence Company_name 101 0 ABC PTY LTD. 101 0 XYZ PTY LTD 103 0 Test PTY LTD 104 0 Test101 PTY LTD 101 0 Pearl PTY LTD 104 0 MyZore pty ltd I would get the date in the following format
After Update - table
Policy_no Sequence Company_name 101 1 ABC PTY LTD. 101 2 XYZ PTY LTD 101 3 Pearl PTY LTD 103 1 Test PTY LTD 104 1 Test101 PTY LTD 104 2 MyZore pty ltd Basically, I want to update the column sequence based on the Policy_No group.
Can someone please help.
>
Basically, I want to update the column sequence based on the Policy_No group.
>
This query will show you how to generate the values you need.
>
Select empno, deptno, row_number() over (partition by deptno) myRow arrested by deptno from EMP
EMPNO, DEPTNO, MYROW
7782,10,1
7839,10,2
7934,10,3
7566,20,1
7902,20,2
7876,20,3
7369,20,4
7788,20,5
7521,30,1
7844,30,2
7499,30,3
7900,30,4
7698,30,5
7654,30,6
>
According to the number of rows in the table, you might be better to create a new table using DEC or a query like this.
-
Multiple Table join without loss of data - SQL help
Hello
I have the following tables 3 employee as indicated in the scripts below, please help in writing SQL that connects all these 3 tables and displays the output as shown below
Required output
EMP ID Loc Value 1 Value 2 Value 3
1 A 3 1 6
1 B 4 9 null
Value null null 1 Wh 8
2 Z 4 9 null
3 R 0 null 1
4 Y 1 null null
5 O 7 7 null
Value null null 5 Wh 5
6 M 7 null 7
6 B null null 8
/ * Create Tables * /.
Create table Emp1 (Empid number, Loc Varchar2 (10), Value_1 number);
Create the Emp2 Table (Empid number, Loc Varchar2 (10), Value_2 number);
Create table emp3 (empid number, loc varchar2 (10), Value_3);
/ * Insert Scripts * /.
Insert Into Emp1 Values (1, 'A', 3);
Insert Into Emp1 Values (2, 'Z', 4);
Insert Into Emp1 Values (3, 'R', 0);
Insert Into Emp1 Values (4, 'Y', 1);
Insert Into Emp1 Values (5, 'o', 7);
Insert Into Emp1 Values (6, am', 7);
Insert Into Emp2 Values (1, 'A', 1);
Insert Into Emp2 Values (1, 'B', 4);
Insert Into Emp2 Values (1, 'Wh', 8);
Insert Into Emp2 Values (2, 'Z', 9);
Insert Into Emp2 Values (5, 'o', 7);
Insert Into Emp2 Values (5, 'Wh', 5);
Insert Into Emp3 Values (1, 'A', 6);
Insert Into Emp3 Values (1, 'B', 9);
Insert Into Emp3 Values (3, 'R', 1);
Insert Into Emp3 Values (6, am', 7);
Insert Into Emp3 Values (6, 'B', 8);
Commit;
Thank you
VarunLike this?
SQL> select nvl(e.empid, e3.empid) empid 2 , nvl(e.loc, e3.loc) loc 3 , e.value_1 4 , e.value_2 5 , e3.value_3 6 from ( 7 select nvl(e1.empid, e2.empid) empid 8 , nvl(e1.loc, e2.loc) loc 9 , e1.value_1 10 , e2.value_2 11 from emp1 e1 12 full join emp2 e2 13 on e1.empid = e2.empid 14 and e1.loc = e2.loc 15 ) e 16 full join emp3 e3 17 on e.empid = e3.empid 18 and e.loc = e3.loc 19 order 20 by empid 21 , loc 22 / EMPID LOC VALUE_1 VALUE_2 VALUE_3 ---------- ---------- ---------- ---------- ---------- 1 A 3 1 6 1 B 4 9 1 Wh 8 2 Z 4 9 3 R 0 1 4 Y 1 5 O 7 7 5 Wh 5 6 B 8 6 M 7 7 10 rows selected. SQL>
-
SQL help. Identify changes to a field.
Greetings!
PS/SQL is not an option for me. I need help to use SQL, if possible for the following scenario.
Oracle 10G.
Table: JOB_DATA
EMPLID, DATE_EFF, DEPTID, JOBCODE
100, 01/11/2012, 34567, MNG
100, 01/10/2012, 34567, SUP
100, 01/09/2012, 28967, MNG
100, 15/08/2012, 28967, SUP
100,6/30/2012,15879, MNG
I need to get the following records only, in other words, every time that changes in the Department ID.
100, 01/10/2012, 34567, SUP
100, 15/08/2012, 28967, SUP
100,6/30/2012,15879, MNG
Thanks in advance.It looks like you want something like
SELECT * FROM (SELECT j.*, lag(deptid) over (partition by emplid order by date_eff) prior_deptid FROM job_data j) WHERE prior_deptid IS NULL OR prior_deptid != deptid
Justin
-
Hello
I have the following table
I need to write the sql query to get the following resultsEmployeeID | ProjectID | Tasks EMP 01 | PID 01 | 5 EMP 01 | PID 01 | 1 EMP 01 | PID 02 | 8 EMP 01 | PID 03 | 9 EMP 02 | PID 01 | 7 EMP 02 | PID 02 | 4 EMP 02 | PID 02 | 3 EMP 03 | PID 04 | 1 EMP 04 | PID 04 | 8 EMP 04 | PID 04 | 7
Any help?EmployeeID | ProjectID | EmpTasks | ProjectTasks EMP 01 | PID 01 | 23 | 6 EMP 01 | PID 01 | 23 | 6 EMP 01 | PID 02 | 23 | 8 EMP 01 | PID 03 | 23 | 9 EMP 02 | PID 01 | 14 | 7 EMP 02 | PID 02 | 14 | 7 EMP 02 | PID 02 | 14 | 7 EMP 03 | PID 04 | 1 | 1 EMP 04 | PID 04 | 15 | 15 EMP 04 | PID 04 | 15 | 15
Thank youThis should do it:
WITH t AS (SELECT 'EMP 01' EmployeeID, 'PID 01' ProjectID, 5 Tasks FROM dual UNION ALL SELECT 'EMP 01' EmployeeID, 'PID 01' ProjectID, 1 Tasks FROM dual UNION ALL SELECT 'EMP 01' EmployeeID, 'PID 02' ProjectID, 8 Tasks FROM dual UNION ALL SELECT 'EMP 01' EmployeeID, 'PID 03' ProjectID, 9 Tasks FROM dual UNION ALL SELECT 'EMP 02' EmployeeID, 'PID 01' ProjectID, 7 Tasks FROM dual UNION ALL SELECT 'EMP 02' EmployeeID, 'PID 02' ProjectID, 4 Tasks FROM dual UNION ALL SELECT 'EMP 02' EmployeeID, 'PID 02' ProjectID, 3 Tasks FROM dual UNION ALL SELECT 'EMP 03' EmployeeID, 'PID 04' ProjectID, 1 Tasks FROM dual UNION ALL SELECT 'EMP 04' EmployeeID, 'PID 04' ProjectID, 8 Tasks FROM dual UNION ALL SELECT 'EMP 04' EmployeeID, 'PID 04' ProjectID, 7 Tasks FROM dual) SELECT EmployeeID, ProjectID, SUM(Tasks) OVER (PARTITION BY EmployeeID) EmpTasks, SUM(Tasks) OVER (PARTITION BY EmployeeID, ProjectID) ProjectTasks FROM t
-
REGEXP_LIKE sql help...
I have a first_name column in my table of students with the following characteristics:
REDA
Kris
Tanvi
Shweta
Rajendra
Kapil
Vipin
Sandeep
Now I'm trying to use the regexp_like function to find the names that begin the letter s and end with the letter p, but I am not able to achieve that I provided here my request please help:
Select first_name
the student
where regexp_like (name, ' ^ sp$ ');
Concerning
RahulMac_Freak_Rahul wrote:
where regexp_like (name, ' ^ sp$ ');Describe your regular expression is to find the string "sp". You do not include buildings for characters that could go p and between s. Here is an example that may help:
SQL> WITH student AS 2 ( 3 SELECT 'raj' AS first_name FROM dual UNION ALL 4 SELECT 'kris' AS first_name FROM dual UNION ALL 5 SELECT 'tanvi' AS first_name FROM dual UNION ALL 6 SELECT 'shweta' AS first_name FROM dual UNION ALL 7 SELECT 'rajendra' AS first_name FROM dual UNION ALL 8 SELECT 'kapil' AS first_name FROM dual UNION ALL 9 SELECT 'vipin' AS first_name FROM dual UNION ALL 10 SELECT 'sandeep' AS first_name FROM dual 11 ) 12 SELECT first_name 13 FROM student 14 WHERE REGEXP_LIKE(first_name,'^s.*p$') 15 ; FIRST_NA -------- sandeep
-
Hi all
I need help on getting the sql code.
Here is how the example data are arranged in my data table called for example.
favorite colors of name age
John doe 15 Red: Blue: Green
Notice how the data in the column "favorite colors", defined the values selected from a list of several.
I need the sql code so that it can display like this.
favorite colors of name age
John doe 15 Red
John doe 15 blue
John doe 15 green
Thank youHi bigmac704,
There may be other ways to do it, but one way is to use apex_util.string_to_table with a function in the pipeline:
(1) create a type to define the line of your favorite color of the person:
CREATE OR REPLACE TYPE PERSON_COLOR as object (name varchar2(30), age number(3, 0), fav_color varchar2(10));
(2) create a type to define a "picture" of your person-favorite color lines:
CREATE OR REPLACE TYPE PERSON_COLOR_TBL AS TABLE OF person_color
(3) create the following function in the pipeline:
CREATE OR REPLACE FUNCTION get_person_colors RETURN person_color_tbl PIPELINED IS out_rec person_color := person_color(null, null, null); l_vc_arr2 APEX_APPLICATION_GLOBAL.VC_ARR2; BEGIN FOR r IN (SELECT * FROM person_colors) LOOP out_rec.name := r.name; out_rec.age := r.age; l_vc_arr2 := APEX_UTIL.STRING_TO_TABLE(r.fav_colors); // for each color, pipe a row FOR i IN 1..l_vc_arr2.count LOOP out_rec.fav_color := l_vc_arr2(i); pipe row(out_rec); END LOOP; END LOOP; RETURN; END;
Now you can use this function in the pipeline, just as you would use a table:
SELECT * FROM TABLE(get_person_colors); NAME AGE FAV_COLOR john doe 15 red john doe 15 blue john doe 15 green SELECT * FROM TABLE(get_person_colors) WHERE name = 'john doe'; NAME AGE FAV_COLOR john doe 15 red john doe 15 blue john doe 15 green SELECT * FROM TABLE(get_person_colors) WHERE name = 'jim smith'; no data found
Hope this helps,
JohnIf you find this information useful, please do not forget to mark the 'useful' or 'correct' post so that others benefit as well.
Maybe you are looking for
-
Printer Driver HP LaserJet 6 p file
I'll try to find a driver file for my HP LaserJet 6 p printer, it worked great until I upgraded to MAC OS Sierra Thank you for your help
-
Hello world I recently came across a malware that installed itself in all of my web browsers "POLYPI." It's on every page and as my default search engine. I did my best to get rid of, but seem to always have pop ups and open new tabs all of a sudden
-
Hello. I bought a Mac book pro MBP13.3"2.5GHz/2X2GB/500GB/SD two years ago (November 1, 2013). The first time, the computer froze. Therefore, I called the apple (here in Greece) support several times and finally they asked me to send the Macbook to t
-
Aspire D255E the network proxy settings cannot be detected
I have asked about this before, but can't find my original message. My netbook was working fine until suddenly I lost connectivity. A pop-up window on the logo of connection in the status bar - bars with a red X on them - says the network proxy sett
-
My wireless laptop have double the speed of the internet than my Wired desktop pc
I just bought the Linksys E2000 and configure properly with a single office (wired connection), adapter of a phone (wired connection) and two laptops (both connected wireless). Everything works very well - the phone works and all computers can acces