How to select 0's when a select statement returns no rows
Hi,
I'm using the following query in one of the report.
SELECT mtrl.inventory_item_id
,mtrl.organization_id
,mtrl.to_subinventory_code
,NVL(SUM(mtrl.quantity),0)quantity
,NVL(SUM(mtrl.quantity_delivered),0)quantity_delivered
FROM mtl_txn_request_lines mtrl, mtl_system_items_b msi
WHERE mtrl.inventory_item_id = msi.inventory_item_id
AND mtrl.organization_id = msi.organization_id
AND mtrl.reference_type_code = 2
AND UPPER(mtrl.TO_subinventory_code) = NVL(UPPER(DECODE(:p_sub_inv,'ALL','',:p_sub_inv)),UPPER(mtrl.TO_subinventory_code))
AND TRUNC(mtrl.CREATION_date) BETWEEN NVL(TRUNC(TO_DATE(:p_from_date,'yyyy/mm/dd hh24:mi:ss')),TRUNC(mtrl.CREATION_DATE) )
AND NVL(TRUNC(TO_DATE(:p_to_date,'yyyy/mm/dd hh24:mi:ss')),TRUNC(mtrl.CREATION_DATE))
GROUP BY mtrl.inventory_item_id,
mtrl.organization_id,
mtrl.to_subinventory_code
For the perticular subinventory in that period there may not be data so the query returns no rows(ALL NULLs) in that case I need to get all 0's instead of NULL.
I tried with UNION but it results in extra row when there is data. I need 0's only when the above query returns no rows.
Any help in this regard is highly appreciated.
My report uotput looks like this:
Subinventory | Part Code |Part Description |Ordered Qty | Received Qty
Mentone St | BATT | non serialised item | |
Mentone St | SONY | spare parts MIN MAX | 30| 0
In the above report
subinventory, Part Code, Part description are in one repeating frame and Ordered and received qty are in other repeating frame.
for a perticular part code there may not be ordered or received quantities. in that case report is showing null(blank) but I want to print ZERO there.
If I use NVL in the query it'll effect only when the query fetches some rows.
Similar Messages
-
How to: SELECT statement returns the result in XML format.
That's it... I want to execute a query that returns all rowset in XML format. How can I do it?
Using Oracle 8.0 and up.
Tnx.FOR XML is a proprietary Microsoft Hack... It is not supported by Oracle in any release. Oracle 9.2.x includes the SQLXML operators, which are part of the SQL:2003 standard and are the agreed upon (Mircosoft, IBM, Oracle etc) way of generating XML from relational data....
See http://download-west.oracle.com/docs/cd/B12037_01/appdev.101/b10790/xdb03usg.htm#sthref251 -
How a select statement generates redo logs
Can some one explain how a select statement generates redo logs
NaveenRedo with a select statement happens when dirty blocks get written to the database, and are then "cleaned up" when next read of that data block. This could happen when a large DML statement does not commit before the DB Writer needs to write modified blocks to disk. The next time the blocks are read by a select statement they get modified, hence REDO is generated by the select statement.
Read the following for more and better understandings.
http://www.dbspecialists.com/specialists/specialist2003-10.htm
Jaffar -
How to select more than one row in a JTable swing adf adf?
how to select more than one row in a JTable swing adf adf?
// Allow selection to span one contiguous set of rows, visible columns, or block of cells
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
// Allow multiple selections of rows, visible columns, or cell blocks
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); -
How to select data from 3rd row of Excel to insert into Sql server table using ssis
Hi,
Iam having Excel files with headers in first two rows , i want two skip that two rows and select data from 3rd row to insert into Sql Server table using ssis.3rd row is having column names.CUSTOMER DETAILS
REGION
COL1 COL2 COL3 COL4 COL5 COL6 COL7
COL8 COL9 COL10 COL11
1 XXX yyyy zzzz
2 XXX yyyy zzzzz
3 XXX yyyy zzzzz
4 XXX yyyy zzzzz
First two rows having cells merged and with headings in excel , i want two skip the first two rows and select the data from 3rd row and insert into sql server using ssis
Set range within Excel command as per below
See
http://www.joellipman.com/articles/microsoft/sql-server/ssis/646-ssis-skip-rows-in-excel-source-file.html
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
IBots - How to send an alert when NO records are returned
It seems straightforward enough to send an alert when a query produces results, but I want to do the opposite. For instance, I only want to get an email at 7am when an object has no data in it for the current day. I do not want to know every day that records are returned.
how would i set up such an iBot such that it fires every day, and only emails me when no records are returned form the query?1) Create a small report called "No Records" and create an unscheduled iBot for this report.
2) You have your iBot created for the original report, yes? Okay. If you don't want any notification when records are produced for this iBot, then in the Destinations tab, make sure all the boxes are unchecked for "Specific Devices."
3) Now in the Advanced tab, click on the "Add Action" button under the "Execute these actions when no records are returned by the conditional request" section.
4) Click on "iBot."
5) Navigate to the "No Records" ibot created in step one.
Now, when the original ibot fires and there are records, nothing will be sent because you unchecked all ways to get notification. But when there are no records, this event will fire the chained ibot you linked to in the Advanced tab. This iBot will have as an attachment, your report that says "No Records." -
How to select all instead of row ?
Hi all,
I got the following query which intend to alert or display via mail to the user the qty on hand versus the min_max qty when user is writing on the mtl_onhand_quantities table.
However, due to the command moqq.rowid = :rowid in the table mtl_onhand_quantities had only give me the last row id that update in the table with the associated subinventory. The nature of mtl_onhand_quantities is consists of all the subinventory transact of the particular inventory item. What I need is to sum of the currenct qty on hand of the inventory item of all subinventory. How can I do that ?
select msi.concatenated_segments, sum(moqq.transaction_quantity),msi.min_minmax_quantity
into &item, &onhand, &min_qty
from mtl_system_items_kfv msi, mtl_onhand_quantities moqq
where msi.organization_id = 8
and msi.inventory_item_id = moqq.inventory_item_id
and msi.min_minmax_quantity >
( select sum(moq.transaction_quantity) from mtl_onhand_quantities moq
where moq.inventory_item_id = msi.inventory_item_id
and moqq.rowid = :rowid
group by moq.inventory_item_id )
group by msi.concatenated_segments, msi.min_minmax_quantityselect msi.concatenated_segments, sum(moqq.transaction_quantity),msi.min_minmax_quantity
into &item, &onhand, &min_qty
from mtl_system_items_kfv msi, mtl_onhand_quantities moqq
where msi.organization_id = 8
and msi.inventory_item_id = moqq.inventory_item_id
and moqq.inventory_item_id = (select inventory_item_id from mtl_onhand_quantities moqq2
where moqq2.rowid = :rowid)
group by msi.concatenated_segments, msi.min_minmax_quantity having sum(moqq.transaction_quantity) < msi.min_minmax_quantity -
How to select record in mulitple rows based on another field's values?
Hi,
sorry about the bad title, really not sure how to explain this
Have the following data:
Cost_center Activity_type
1005009401 CLBR0
1005009401 CLBR1
1005009401 TLBR0
1005009401 TLBR1
1005009401 VEH00
1005009402 CLBR3
1005009402 CLBR4
1005009402 TLBR5
1005009402 TLBR6
1005009402 VEH07
1005009901 CE000
1005009901 CLBR0
1005009901 CLBR1
1005009901 TLBR0
1005009901 TLBR1
1005009901 VEH01 I need to return a list of cost centers (with its associated activity types) that do not have activity types of CLBR0 and CLBR1 and TLBR0 and TLBR1 and VEH01. So in the above data, cost center 1005009401 and 1005009402 would be returned.
Any thoughts?
Thanks.
Edited by: dgouin on Aug 29, 2012 11:42 AM - added more sample data.
Edited by: dgouin on Aug 29, 2012 11:45 AM
Edited by: dgouin on Aug 29, 2012 11:49 AMSorta kludgey, but functional:
WITH ccs AS
SELECT '1005009401' AS CC, 'CLBR0' AS activity_type FROM dual
UNION ALL
SELECT '1005009401' AS CC, 'CLBR1' AS activity_type FROM dual
UNION ALL
SELECT '1005009401' AS CC, 'TLBR0' AS activity_type FROM dual
UNION ALL
SELECT '1005009401' AS CC, 'TLBR1' AS activity_type FROM dual
UNION ALL
SELECT '1005009401' AS CC, 'VEH00' AS activity_type FROM dual
UNION ALL
SELECT '1005009901' AS CC, 'CE000' AS activity_type FROM dual
UNION ALL
SELECT '1005009901' AS CC, 'CLBR0' AS activity_type FROM dual
UNION ALL
SELECT '1005009901' AS CC, 'CLBR1' AS activity_type FROM dual
UNION ALL
SELECT '1005009901' AS CC, 'TLBR0' AS activity_type FROM dual
UNION ALL
SELECT '1005009901' AS CC, 'TLBR1' AS activity_type FROM dual
UNION ALL
SELECT '1005009901' AS CC, 'VEH01' AS activity_type FROM dual
SELECT cc, activity_type
FROM ccs c1
WHERE 5 != (SELECT COUNT(DISTINCT activity_type)
FROM ccs c2
WHERE activity_type IN ('CLBR0','CLBR1','TLBR0','TLBR1','VEH01')
AND c1.cc = c2.cc
CC ACTIVITY_TYPE
1005009401 CLBR0
1005009401 CLBR1
1005009401 TLBR0
1005009401 TLBR1
1005009401 VEH00 -
How to select State based on the country
Hi All,
I have a requirement,i have to dynamically populate the value in State based on the Country chosen in the drop down.
Can you please tell me how to achieve this functionality.
Thanks & Regards,
Malkit SinghHi, Malkit
There is already a cotext mapping for Sate.
Take a look at this.
http://scn.sap.com/message/13816883#13816883
In your case, use these data types.
element Country :CountryCode;
element State :RegionCode;
Regards,
Fred. -
How a SELECT statement can return the results in XML format
That's it... I want to execute a query that returns all rowset in XML format. How can I do it?
I have Oracle 9i
Thanks
Jaime9i, or 9iR2 ?. If 9i I would recommend upgrading to 9iR2 and then using the SQL/XML operators.
-
Can select statement return 'No Record found' for each of invalid inputs.
Let say when you select a invalid booking number, oracle will
return nothing, but do u know how to make oracle return a record
saying it's invalid?
Example, the following sql normally return 3 records cause
only 3 numbers are valid, is there anyway to make it return
4 records and indicate those invalid ones are invalid somehow.
select booking_service, BOOKING_NO from tb_booking where booking_no in ('valid1','INVALID', 'valid2', 'valid3')Along the same lines you could use object types to achieve the same result. This would remove the need for a temporary table, and also allow you to create the list of ids to check with PL/SQL without i/o. As an example
CREATE TYPE booking_id_typ IS OBJECT (id NUMBER);
CREATE TYPE booking_id_list_typ IS TABLE OF booking_id_typ;
CREATE TABLE bookings (booking_id booking_id_typ);
(you could have booking_id as type NUMBER, but for consistency with subsequent SQL I've used booking_id_typ)
INSERT INTO bookings VALUES (booking_id_typ(10));
INSERT INTO bookings VALUES (booking_id_typ(20));
INSERT INTO bookings VALUES (booking_id_typ(40));
INSERT INTO bookings VALUES (booking_id_typ(50));
The following SQL statement creates a collection on the fly, rather than using an IN clause.
SELECT i.id booking_id, DECODE(b.booking_id.id, NULL, 'Invalid', 'Ok') status
FROM bookings b,
SELECT id
FROM TABLE(booking_id_list_typ(booking_id_typ(10), booking_id_typ(20), booking_id_typ(30), booking_id_typ(40)))
) i
WHERE i.id = b.booking_id.id (+)
BOOKING_ID STATUS
10 Ok
20 Ok
30 Invalid
40 Ok
This SQL statement works in 9i. I get the following error in 8i, but you might be able to CAST the TABLE to booking_id_list_typ, or depending on your application, create a PL/SQL variable of type booking_id_list_typ and CAST that instead.
ERROR at line 5:
ORA-22905: cannot access rows from a non-nested table item -
Select statement returns different results from 9i and 10g
Hi all,
Would appreciate if someone could help to solve this puzzle here:
I have the exact the statements running on Oracle 9i and 10g, why do they return different results?
Select unique(GroupDesc) , GroupSeq from Module where ModuleId in (Select ModuleId from User_Access where UserId='admin') and Status='A'
In Oracle 9i:
Both columns returned as follows...
GroupDesc | GroupSeq
In Oracle 10g:
Only one column returned, the column with unique keyword was missing...
GroupSeq
Could anyone enlighten me?yes, the table structure... actually the CREATE TABLE statement...
with some sample data (INSERT INTO)
and the actual queries (both of them - copy-paste them from each separate environment)
you can use tags around the statements this will format it to a fixed font - making it easier to read
Edited by: Alex Nuijten on Feb 20, 2009 10:05 AM -
How to select range for dater in BAPI_DOCUMENT_GETLIST2
Hi All
I have following problem with this FM BAPI_DOCUMENT_GETLIST2. I was looking a lot on different forums but I cannot find answer which corresponds to my problem.
I have in DMS saved files for our customer and Iu2019m using object linking as
Objlinkselection-DOKOB = KNA1
Objlinkselection- OBJKY = KUNNR.
It works well, but it takes all doc from beginning. Iu2019d like to make selection that final table documentdata is in range of defined range date with values 20111201 to 20111231
Now I have to select all documents for this customer and after that I have to make loop to get correct files. And it takes a huge time and in a few years it will collaps
LOOP AT LT_DOCUMENTDATA INTO <FS> WHERE CREATEDATE IN rn_DATE.
ENDLOOP.
Any hint how to make this faster?
Just to avoid any misunderstandings, I'm not asking on loop speed but how to select by FM less rows based on create date.
Thank a lot fo any hint
PetrHello Deepak,
Thanks a lot for your answer. I have read this thread already, but it is not realy what I need. But anyway in the night I have found solution.
I will go through table DRAW where I will get just DOCNUMS for current date range.
Than I will select correct rows from table DRAD for selected KUNNR.
Final set od DOCNUMS I will use as selection criteria for BAPI.
It should help, now I go to try it.
Thansk anyway
Petr -
Select Statement - How do i convert rows to columns
Hi, i need your help please.
i have three options: A, B, C in table T_OPTIONS (not more and not less, it is always 3)
no i can assign articles to this option.
Article 1, 2, 3, 4
in the table it looks like this
ARTICLE_ID T_OPTIONS
1 A
1 B
2 C
3 A
3 C
But now i want to have a select statement which convert rows to columns, it has to look like this
ARTICLE A B C
1 x x
2 x
3 x x
Can you help me!?
Edited by: Dila on 02.08.2012 01:52
Edited by: Dila on 02.08.2012 01:53Dila wrote:
Hi, i need your help please.
i have three options: A, B, C in table T_OPTIONS (not more and not less, it is always 3)
no i can assign articles to this option.
Article 1, 2, 3, 4
in the table it looks like this
ARTICLE_ID T_OPTIONS
1 A
1 B
2 C
3 A
3 C
But now i want to have a select statement which convert rows to columns, it has to look like this
ARTICLE A B C
1 x x
2 x
3 x x
Can you help me!?
Edited by: Dila on 02.08.2012 01:52
Edited by: Dila on 02.08.2012 01:53Read {message:id=9360002} and {message:id=9360005}
SQL> ed
Wrote file afiedt.buf
1 with sample_data as
2 (
3 select 1 ARTICLE_ID,'A' T_OPTIONS from dual union all
4 select 1, 'B' from dual union all
5 select 2, 'C' from dual union all
6 select 3, 'A' from dual union all
7 select 3, 'C' from dual
8 )
9 select article_id,
10 decode(sum(case
11 when t_options = 'A' then 1
12 else 0
13 end), 0, null,
14 'X') A,
15 decode(sum(case
16 when t_options = 'B' then 1
17 else 0
18 end), 0, null,
19 'X') B,
20 decode(sum(case
21 when t_options = 'C' then 1
22 else 0
23 end), 0, null,
24 'X') C
25 from sample_data
26* group by article_id
SQL> /
ARTICLE_ID A B C
1 X X
2 X
3 X X -
SELECT statement (urgent!)
Could you please help me with the following statement?
The FROM clause is incorrect but i don't know what i should write.
Thank you in advance.
Maria.
DECLARE
A NUMBER;
CURSOR C1 IS
SELECT TABLE_NAME, COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE NULLABLE='N' AND TABLE_NAME LIKE 'P_%';
BEGIN
FOR i IN C1
LOOP
SELECT COUNT(*) INTO A
FROM i.TABLE_NAME
WHERE i.COLUMN_NAME IS NOT NULL;
END LOOP;
END;
/FOR-LOOP
Whereas the number of iterations through a WHILE loop is unknown until the loop completes, the number of iterations through a FOR loop is known before the loop is entered. FOR loops iterate over a specified range of integers. The range is part of an iteration scheme, which is enclosed by the keywords FOR and LOOP. A double dot (..) serves as the range operator. The syntax follows:
FOR counter IN [REVERSE] lower_bound..higher_bound LOOP
sequence_of_statements
END LOOP;
The range is evaluated when the FOR loop is first entered and is never re-evaluated.
As the next example shows, the sequence of statements is executed once for each integer in the range. After each iteration, the loop counter is incremented.
FOR i IN 1..3 LOOP -- assign the values 1,2,3 to i
sequence_of_statements -- executes three times
END LOOP;
The following example shows that if the lower bound equals the higher bound, the sequence of statements is executed once:
FOR i IN 3..3 LOOP -- assign the value 3 to i
sequence_of_statements -- executes one time
END LOOP;
By default, iteration proceeds upward from the lower bound to the higher bound. However, as the example below shows, if you use the keyword REVERSE, iteration proceeds downward from the higher bound to the lower bound. After each iteration, the loop counter is decremented. Nevertheless, you write the range bounds in ascending (not descending) order.
FOR i IN REVERSE 1..3 LOOP -- assign the values 3,2,1 to i
sequence_of_statements -- executes three times
END LOOP;
Inside a FOR loop, the loop counter can be referenced like a constant but cannot be assigned values, as the following example shows:
FOR ctr IN 1..10 LOOP
IF NOT finished THEN
INSERT INTO ... VALUES (ctr, ...); -- legal
factor := ctr * 2; -- legal
ELSE
ctr := 10; -- not allowed
END IF;
END LOOP;
Iteration Schemes
The bounds of a loop range can be literals, variables, or expressions but must evaluate to numbers. Otherwise, PL/SQL raises the predefined exception VALUE_ERROR. The lower bound need not be 1, as the examples below show. However, the loop counter increment (or decrement) must be 1.
j IN -5..5
k IN REVERSE first..last
step IN 0..TRUNC(high/low) * 2
Internally, PL/SQL assigns the values of the bounds to temporary PLS_INTEGER variables, and, if necessary, rounds the values to the nearest integer. The magnitude range of a PLS_INTEGER is -2**31 .. 2**31. So, if a bound evaluates to a number outside that range, you get a numeric overflow error when PL/SQL attempts the assignment, as the following example shows:
DECLARE
hi NUMBER := 2**32;
BEGIN
FOR j IN 1..hi LOOP -- causes a 'numeric overflow' error
END LOOP;
END;
Some languages provide a STEP clause, which lets you specify a different increment (5 instead of 1 for example). PL/SQL has no such structure, but you can easily build one. Inside the FOR loop, simply multiply each reference to the loop counter by the new increment. In the following example, you assign today's date to elements 5, 10, and 15 of an index-by table:
DECLARE
TYPE DateList IS TABLE OF DATE INDEX BY BINARY_INTEGER;
dates DateList;
k CONSTANT INTEGER := 5; -- set new increment
BEGIN
FOR j IN 1..3 LOOP
dates(j*k) := SYSDATE; -- multiply loop counter by increment
END LOOP;
END;
Dynamic Ranges
PL/SQL lets you determine the loop range dynamically at run time, as the following example shows:
SELECT COUNT(empno) INTO emp_count FROM emp;
FOR i IN 1..emp_count LOOP
END LOOP;
The value of emp_count is unknown at compile time; the SELECT statement returns the value at run time.
What happens if the lower bound of a loop range evaluates to a larger integer than the upper bound? As the next example shows, the sequence of statements within the loop is not executed and control passes to the next statement:
-- limit becomes 1
FOR i IN 2..limit LOOP
sequence_of_statements -- executes zero times
END LOOP;
-- control passes here
Scope Rules
The loop counter is defined only within the loop. You cannot reference it outside the loop. After the loop is exited, the loop counter is undefined, as the following example shows:
FOR ctr IN 1..10 LOOP
END LOOP;
sum := ctr - 1; -- not allowed
You need not explicitly declare the loop counter because it is implicitly declared as a local variable of type INTEGER. The next example shows that the local declaration hides any global declaration:
DECLARE
ctr INTEGER;
BEGIN
FOR ctr IN 1..25 LOOP
IF ctr > 10 THEN ... -- refers to loop counter
END LOOP;
END;
To reference the global variable in this example, you must use a label and dot notation, as follows:
<<main>>
DECLARE
ctr INTEGER;
BEGIN
FOR ctr IN 1..25 LOOP
IF main.ctr > 10 THEN -- refers to global variable
END IF;
END LOOP;
END main;
The same scope rules apply to nested FOR loops. Consider the example below. Both loop counters have the same name. So, to reference the outer loop counter from the inner loop, you must use a label and dot notation, as follows:
<<outer>>
FOR step IN 1..25 LOOP
FOR step IN 1..10 LOOP
IF outer.step > 15 THEN ...
END LOOP;
END LOOP outer;
Using the EXIT Statement
The EXIT statement lets a FOR loop complete prematurely. For example, the following loop normally executes ten times, but as soon as the FETCH statement fails to return a row, the loop completes no matter how many times it has executed:
FOR j IN 1..10 LOOP
FETCH c1 INTO emp_rec;
EXIT WHEN c1%NOTFOUND;
END LOOP;
Suppose you must exit from a nested FOR loop prematurely. You can complete not only the current loop, but any enclosing loop. Simply label the enclosing loop that you want to complete. Then, use the label in an EXIT statement to specify which FOR loop to exit, as follows:
<<outer>>
FOR i IN 1..5 LOOP
FOR j IN 1..10 LOOP
FETCH c1 INTO emp_rec;
EXIT outer WHEN c1%NOTFOUND; -- exit both FOR loops
END LOOP;
END LOOP outer;
-- control passes here
Joel P�rez
Maybe you are looking for
-
Welcome to the HP Windows 8 Release Preview
Welcome to the HP Windows® 8 Release Preview Welcome to the HP Windows® 8 Release Preview discussion board. This board has been set up for consumers to discuss Windows® 8 Release Preview as it relates to their HP products. Windows® 8 is the new opera
-
I'm using the 61.77 Nvidia drivers with my old Geforce256 (32mb) 3D Prophet card. However, I'm going to buy a new Nvidia card in the $80 range, probably an FX5200 256 mb. Don't laugh, I'm just not an avid gamer. My question is, can I just install t
-
Hello, I have the following code on my Student class: public static List<Student> getAllStudents() { EntityManager em = SingletonDAO.getInstance().getEntityManager(); Query q = em.createQuery( "SELECT c FROM students c" ); List<Student> students = q.
-
How to chance the color of the JTree lines ?
Dear friends, how can I change te color of te line in a JTree? I tryied that: UIManager.getDefaults().put("Tree.line", Color.black);but it doesn�t works...
-
Ipod forze and can't restore it
when i have tried to update the ipod with 2006-06-28 update it says that there was an firmware error and that he can't write on the disk, then on the ipod appear the folder with the esclamation point, and when i try to restore it, it says again firmw