Query for all hours in a month?
Hello,
I was hoping someone might be able to assist. I need to query for all the hours in a user specified date range (generally a month). For example,
a query such as:
select all hr_end from dual where start_date between '01-feb-2010' and '28-feb-2010'
that returns:
feb 01 2010 01:00
feb 01 2010 02:00
feb 01 2010 03:00
feb 28 2010 23:00
feb 28 2010 24:00
Ideally, I want to be able to run this query within a WITH clause such that I can reference the temporary table/ list of hours in a subsequent select statement quickly. Something like:
With
all_hrs_in_month AS
select .....
where start_date between '01-mar-2010' and '31-mar-2010'
I've looked around for similar questions, but have only found ones for all the 'days' in a month using level, connect by, row_num, for which I'm honestly not too familiar with and wasn't clear as to how I could modify to my needs.
Greatly appreciate your help with this request.
- j
Edited by: user12942939 on Apr 5, 2010 12:04 PM
Hi,
Welcome to the forum!
WITH got_parameters AS
SELECT TO_DATE ('01-mar-2010 00:00', 'dd-mon-yyyy hh24:mi') AS start_date
, TO_DATE ('31-mar-2010 23:00', 'dd-mon-yyyy hh24:mi') AS end_date
FROM dual
, all_hrs AS
SELECT start_date + ( (LEVEL - 1)
/ 24
) AS hr
FROM got_parameters
CONNECT BY LEVEL <= 1 + ( 24
* (end_date - start_date)
SELECT TO_CHAR (hr, 'DD-Mon-YYYY HH24:MI') AS h
FROM all_hrs
ORDER BY hr
;Output:
H
01-Mar-2010 00:00
01-Mar-2010 01:00
01-Mar-2010 02:00
01-Mar-2010 03:00
31-Mar-2010 22:00
31-Mar-2010 23:00Notice that start_date and end_date don't have to span then entire month; they don't even have to be in the same month.
If you'd rather specify just one parameter (such as a single string containing the month and year):
WITH got_parameters AS
SELECT TRUNC ( TO_DATE (:p_month, 'mon-yyyy')
, 'MONTH'
) AS start_date
, TRUNC ( ADD_MONTHS ( TO_DATE (:p_month, 'mon-yyyy')
, 1
, 'MONTH'
) AS end_date
FROM dual
, all_hrs AS
SELECT start_date + ( (LEVEL - 1)
/ 24
) AS hr
FROM got_parameters
CONNECT BY LEVEL <= ( 24 -- NOTE: Not adding 1 here
* (end_date - start_date)
SELECT TO_CHAR (hr, 'DD-Mon-YYYY HH24:MI') AS h
FROM all_hrs
ORDER BY hr
How It Works
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= x
;Produces a result set consiting of the integers 1, 2, 3, ..., x.
There's nothing magical about the dual table; you can use any table AS LONG AS THE TABLE HAS ONLY ONE ROW .
The other examples you saw probably just added this to a starting date, to get successive days, since, in Oracle date arithmetic, dt+n is a DATE n days after dt.
Your case is slightly more complicated, because you want to add hours, not days. Since an hour is 1/24 of a day, we multiply by 24 to find how many integers to genereate, and divide by 24 when adding that number to the base date.
Edited by: Frank Kulash on Apr 5, 2010 3:07 PM
Similar Messages
-
Not allowing to query for all blocks.
hi,
i have a new question.
I'm not able to query in all the fields of all blocks.
I'm able to query only in all fields of master-block. But not able to query in fields of other blocks.
I have set 'query all records' in the property palette to 'Yes'.
What should i do do allow it query & execute in all fields of all blocks?
Thank you.Pl do not post duplicates - Not allowing to query for all blocks.
-
Performance optimization on select query for all entries
Hi All,
I want to optimize the select query in my Program.
The select query is taking lot of time to search the records for the given condition in the where clause
and more interestingly there are no records fetched from the database as the where condition does not matches.
It is taking more than 30 min to search the record and the result is no record found.
Below is my select query. I have also created the secondary Index for the same.
In My opinion FOR ALL ENTRIES is taking lot of time. Because there are more than 1200 records in internal table t_ajot
select banfn bnfpo bsart txz01 matnr Werks lgort matkl reswk menge meins flief ekorg
INTO CORRESPONDING FIELDS OF TABLE t_req
FROM eban
FOR ALL ENTRIES IN t_ajot
WHERE matkl >= t_ajot-matkl_low
AND matkl <= t_ajot-matkl_high
AND werks = t_ajot-werks
AND loekz = ' '
AND badat IN s_badat
AND bsart = 'NB'.
Please suggest.Hi,
that,
FOR ALL ENTRIES IN t_ajot
WHERE matkl >= t_ajot-matkl_low
AND matkl <= t_ajot-matkl_high
AND werks = t_ajot-werks
AND loekz = ' '
AND badat IN s_badat
AND bsart = 'NB'.
looks strange.
However:
How does your index look like?
What executoin plan do you get?
How do the statistics look like?
Whats the content of the variables t_ajot-... and s_badata?
Kind regards,
Hermann -
How to write select query for all the user tables in database
Can any one tell me how to select the columns from all the user tables in a database
Here I had 3columns as input...
1.phone no
2.memberid
3.sub no.
I have to select call time,record,agn from all the tables in a database...all database tables have the same column names but some may have additional columns..
Eg: select call time, record,agn from ah_t_table where phone no= 6186759765,memberid=j34563298
Query has to execute not only for this table but for all user tables in the database..all tables will start with ah_t
I am trying for this query since 30days...
Help me please....any kind of help is appreciated.....Hi,
user13113704 wrote:
... i need to include the symbol (') for the numbers(values) to get selected..
eg: phone no= '6284056879'To include a single-quote in a string literal, use 2 or them in a row, as shown below.
Starting in Oracle 10, you can also use Q-notation:
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements003.htm#i42617
...and also can you tell me how to execute the output of this script. What front end are you using? If it's SQL*Plus, then you can SPOOL the query to a file, and then execute that file, like this:
-- Suppress SQL*Plus features that interfere with raw output
SET FEEDBACK OFF
SET PAGESIZE 0
-- Run preliminary query to generate main query
SPOOL c:\my_sql_dir\all_ah_t.sql
SELECT 'select call time, record, agn from '
|| owner
|| '.'
|| table_name
|| ' where phone_no = ''6186759765'' and memberid = j34563298'
|| CASE
WHEN ROW_NUMBER () OVER ( ORDER BY owner DESC
, table_name DESC
) = 1
THEN ';'
ELSE ' UNION ALL'
END AS txt
FROM all_tables
WHERE SUBSTR (table_name, 1, 4) = 'AH_T'
ORDER BY owner
, table_name
SPOOL OFF
-- Restore SQL*Plus features that interfere with raw output (if desired)
SET FEEDBACK ON
SET PAGESIZE 50
-- Run main query:
@c:\my_sql_dir\all_ah_t.sql
so that i form a temporary view for this script as a table(or store the result in a temp table) and my problem will be solved..Sorry, I don't understand. What is a "temporary view"? -
Set PO query for all users in POWL screen
Dear All,
In Purchase order work list (POWL) need to define a new query and set it for all the users.
Request to please let us know how we can create and set that query in POWL for all users?
thanks,
mahesh.Dear All,
In Purchase order work list (POWL) need to define a new query and set it for all the users.
Request to please let us know how we can create and set that query in POWL for all users?
thanks,
mahesh. -
Select query 'for all entries'
Hello Friends,
SELECT emp_id emp_name
INTO corresponding fields of table itab_emp
FROM employee
for all entries in itab_dept
WHERE emp_id = itab_dept_emp_id.
In the above select query we are using 'for all entries' for the internal table itab_dept.What will happen if the join fails?Will we get any data in the output table?
What is the prerequisites for using 'for all entries'.
Please advice me on this.
Regards
Ashish.Hi
In this case all the records available in employee table are extracted into internal table itab_emp.
It is safe to check whether itab_dept is initial or not.
If it is initial then stop the select query.
Check the following program u will get an idea.
Also try this program by removing comment to 'REFRESH IT_LFA1'.
REPORT ZBM_PG .
TABLES: LFA1, EKKO.
DATA:
IT_LFA1 TYPE TABLE OF LFA1 WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 10,
IT_LFA1 TYPE TABLE OF LFA1 WITH HEADER LINE,
IT_EKKO TYPE TABLE OF EKKO WITH HEADER LINE.
SELECT * FROM LFA1 INTO TABLE IT_LFA1 WHERE LIFNR EQ '0000001000' OR LIFNR EQ '0000001500'.
SELECT * FROM LFA1 INTO TABLE IT_LFA1 UP TO 1 ROWS.
refresh it_lfa1.
SELECT * FROM EKKO INTO TABLE IT_EKKO FOR ALL ENTRIES IN IT_LFA1 WHERE LIFNR = IT_LFA1-LIFNR.
LOOP AT IT_EKKO.
WRITE: / IT_EKKO-LIFNR, IT_EKKO-EBELN.
ENDLOOP.
Reward me if it is useful -
Need a single query for all IF conditions
I have the following requirements which looks at two parameter p_batch and p_frequency and frames a select to query from the Employers table.
Can I achieve all these in a single select statement? If so how to do it?
Please help.
Thank you.
IF p_batch = 1 and p_frequency = 2 then
SELECT er.employer_id
FROM employers er
WHERE SUBSTR (er.name, 1, 1) BETWEEN 'A' AND 'M'
AND er.freq_flag = 2
ORDER BY er.name asc;
ELSIF p_batch = 2 and p_frequency = 2 then
Open cursor for processing employer starting with N to Z
SELECT er.employer_id
FROM employers er
WHERE SUBSTR (er.name, 1, 1) BETWEEN 'N' AND 'Z'
AND er.freq_flag = 2
ORDER BY er.name asc;
ELSIF p_batch = 1 and p_frequency = 1 then
Open cursor for processing employer starting with A to M for all ER
SELECT er.employer_id
FROM employers er
WHERE SUBSTR (er.name, 1, 1) BETWEEN 'A' AND 'M'
ORDER BY er.name asc;
ELSIF p_batch = 2 and p_frequency = 1 then
Open cursor for processing employer starting with N to Z for all ER
SELECT er.employer_id
FROM wweb.ptei_employers er
WHERE SUBSTR (er.name, 1, 1) BETWEEN 'N' AND 'Z'
ORDER BY er.name asc;
ELSE
-- Return as no need to send any mails
END IF;Hi,
Sure, you can do that in a single statement. Here's one way:
SELECT employer_id
FROM employers
WHERE SUBSTR (name, 1, 1) BETWEEN CASE p_batch
WHEN 1 THEN 'A'
WHEN 2 THEN 'N'
END
AND CASE p_batch
WHEN 1 THEN 'M'
WHEN 2 THEN 'Z'
END
AND ( ( p_frequency = 2
AND freq_flag = 2
OR p_frequency = 1
ORDER BY name
; -
SQL Query for all Function Name attached to Responsibility
I have a requiremnet where in the data needs to be fetched giving all details of User Name, Responsibility Name, Function_user_name at responsibility level.
For e.g when a user logs in ORacle application and gets in to a responsibility and uses short cut key (Cntrl + L), the application lists out all the form functions, i need those form funtion for all users at responsibility level.
follwoing is the SQL i developed but its not listing the form function when we use (cntrl + L).
SELECT fur.user_name, fur.description, fur.responsibility_name,
mnu.user_function_name, mnu.function_name --, mnu.TYPE, mnu.PARAMETERS
FROM (
SELECT /*+ ordered use_nl(ffft) */
DISTINCT fm.menu_id, ffft.user_function_name, fff.function_name,
fff.TYPE, fff.PARAMETERS
FROM (select distinct menu_id
from apps.fnd_responsibility_tl frt,
applsys.fnd_responsibility fr
where 1=1
and frt.responsibility_id = fr.responsibility_id
and frt.application_id = fr.application_id
and frt.responsibility_name = 'SLCHR Admin Asst' ) frm,
apps.fnd_menus fm,
apps.fnd_form_functions fff,
apps.fnd_form_functions_tl ffft
WHERE 1 = 1
--AND fm.menu_id IN (1004979, 1009084)
AND frm.menu_id = fm.menu_id
AND fff.function_id = ffft.function_id
AND fm.menu_id IN (SELECT me.menu_id
FROM apps.fnd_menu_entries me
START WITH me.function_id = fff.function_id
CONNECT BY PRIOR me.menu_id = me.sub_menu_id)
) mnu,
(SELECT fusr.user_name, fusr.description, frv.responsibility_name, frv.menu_id
FROM apps.fnd_responsibility_vl frv,
apps.fnd_user_resp_groups_direct frg,
apps.fnd_user fusr
WHERE 1 = 1
--and fusr.user_name = 'JLEWIS03'
AND frv.responsibility_name = 'SLCHR Admin Asst'
AND frv.end_date IS NULL
AND fusr.user_id = frg.user_id
AND fusr.end_date IS NULL
AND frv.responsibility_id = frg.responsibility_id
) fur
WHERE 1 = 1
AND fur.menu_id = mnu.menu_id
Please helpPlease see these docs.
Checking Functions Associated with a User Menu or a Responsibility [ID 948512.1]
HOW TO GENERATE MENU TREE FOR A MENU ATTACHED TO A RESPONSIBILITY IN ORACLE APPLICATIONS 11i ? [ID 312014.1]
Thanks,
Hussein -
Is it possible to query for all marketing doc hits to one account?
We have been struggling to create one single query that pulls all entries made to a particular revenue account by all marketing documents (in 2007A, PL42).
For example, an item may be used on an A/R invoice, A/R, credit memo, A/P invoice, or A/P credit memo in any given month. We've been using four different queries to pull basic data from each one, then blending them together in Excel. This seems silly, and I'm hoping we can blend the code in one query instead of running four different ones. Thoughts?
One of the four queries we've been using:
SELECT T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription], T1.[FreeTxt], T1.[Quantity], T1.[Price], T1.[LineTotal], T1.[AcctCode], T2.[Segment_0], T2.[AcctName]
FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OACT T2 ON T1.AcctCode = T2.AcctCode
WHERE T2.[Segment_0] ='319000' AND T0.[DocDate] BETWEEN '[%0]' AND '[%1]'
ORDER BY T1.[ItemCode]
It would be even cooler if we could choose two segments to review (i.e. also '318000').
Kind Regards,
Russell ClaytonWhen trying to link AP docs as well, I am only retrieving AR. Any suggestions on this, and how to properly order by T2.Segment_0 would be greatly appreciated. Thanks!
SELECT T2.Segment_0, T2.AcctName, T1.[ObjType], T0.DocNum, T0.DocDate, T0.CardName, T1.ItemCode, T1.Dscription, T1.FreeTxt, T1.Quantity, T1.Price, T1.LineTotal
FROM dbo.OINV T0 INNER JOIN dbo.INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN dbo.OACT T2 ON T1.AcctCode = T2.AcctCode
WHERE T0.DocDate BETWEEN '[%0]'
AND '[%1]'
UNION ALL
SELECT T2.Segment_0, T2.AcctName, T1.[ObjType], T0.DocNum, T0.DocDate, T0.CardName, T1.ItemCode, T1.Dscription, T1.FreeTxt, T1.Quantity, T1.Price, T1.LineTotal
FROM dbo.ORIN T0 INNER JOIN dbo.RIN1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN dbo.OACT T2 ON T1.AcctCode = T2.AcctCode
WHERE T0.DocDate BETWEEN '[%0]'
AND '[%1]'
SELECT T2.Segment_0, T2.AcctName, T1.[ObjType], T0.DocNum, T0.DocDate, T0.CardName, T1.ItemCode, T1.Dscription, T1.FreeTxt, T1.Quantity, T1.Price, T1.LineTotal
FROM dbo.OPCH T0 INNER JOIN dbo.PCH1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN dbo.OACT T2 ON T1.AcctCode = T2.AcctCode
WHERE T0.DocDate BETWEEN '[%0]'
AND '[%1]'
SELECT T2.Segment_0, T2.AcctName, T1.[ObjType], T0.DocNum, T0.DocDate, T0.CardName, T1.ItemCode, T1.Dscription, T1.FreeTxt, T1.Quantity, T1.Price, T1.LineTotal
FROM dbo.ORPC T0 INNER JOIN dbo.OPC1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN dbo.OACT T2 ON T1.AcctCode = T2.AcctCode
WHERE T0.DocDate BETWEEN '[%0]'
AND '[%1]'
ORDER BY T2.Segment_0 ASC -
Query for all open sales order with a date range
Hi Experts!
I wrote this query that will look up all open sales order of a particular customer and how much was served:
SELECT T0.[DocNum] 'OS #', T0.[DocDate] 'Posting Date', T0.[CardName] 'Customer', T1.[Dscription] 'Item Description',T4.[SalUnitMsr] 'Sales UOM', T4.[InvntryUom] 'Invty UOM', T1.[UseBaseUn] 'Use of Invty UOM' , T1.[Quantity], T1.[Quantity]-T1.[OpenQty] 'Served Qty', T1.[OpenQty] 'Unserved Qty' FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN OCTG T2 ON T0.GroupNum = T2.GroupNum INNER JOIN OSLP T3 ON T0.SlpCode = T3.SlpCode INNER JOIN OITM T4 ON T1.ItemCode = T4.ItemCode WHERE T0.[DocStatus] = 'O' and T0.[CardName] =[%0]
However, I don't know how to input a parameter for the date range in a query. Can anyone help with this issue?
Your input will be highly appreciated.
Warm regards,
JenHiiiiiii
Try This.....
SELECT T0.DocNum 'OS #', T0.DocDate 'Posting Date', T0.CardName 'Customer', T1.Dscription 'Item Description',T4.SalUnitMsr 'Sales UOM', T4.InvntryUom 'Invty UOM', T1.UseBaseUn 'Use of Invty UOM' , T1.Quantity, T1.Quantity, T1.OpenQty 'Served Qty', T1.OpenQty 'Unserved Qty' FROM ORDR T0 INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry INNER JOIN OCTG T2 ON T0.GroupNum = T2.GroupNum INNER JOIN OSLP T3 ON T0.SlpCode = T3.SlpCode INNER JOIN OITM T4 ON T1.ItemCode = T4.ItemCode WHERE T0.DocStatus = 'O' and T0.CardName ='[%0]' and (T0.DocDate>='[%1]' and T0.DocDate<='[%2]')
Regards, -
Missing field for MDX query for All Members
Hello,
I'm using MDX query with SSRS for report and I ran into this issue:
Query 1:
select non empty ([Product].[Category].allmembers*[Date].[Calendar].[Calendar Year].allmembers ) on 1,
([Measures].[Internet Sales Amount]) on 0
from [Adventure Works]
go
Query 2
select non empty ([Product].[Category].[All]*[Date].[Calendar].[Calendar Year].allmembers ) on 1,
([Measures].[Internet Sales Amount]) on 0
from [Adventure Works]
In case I run query 2 in SSDT (query designer), it doesn't return first column whereas if I run above two queries in SSMS, I get similar columns/fields.
How it can be resolved in SSRS to return all columns for query 1 and 2.
Thanks,
P
mark it as answer if it answered your question :)Hello,
I'm using MDX query with SSRS for report and I ran into this issue:
Query 1:
select non empty ([Product].[Category].allmembers*[Date].[Calendar].[Calendar Year].allmembers ) on 1,
([Measures].[Internet Sales Amount]) on 0
from [Adventure Works]
go
Query 2
select non empty ([Product].[Category].[All]*[Date].[Calendar].[Calendar Year].allmembers ) on 1,
([Measures].[Internet Sales Amount]) on 0
from [Adventure Works]
In case I run query 2 in SSDT (query designer), it doesn't return first column whereas if I run above two queries in SSMS, I get similar columns/fields.
How it can be resolved in SSRS to return all columns for query 1 and 2.
Thanks,
P -
Query for all computers that do NOT have either the 32-bit or 64-bit version of a product
I've found plenty of people asking how they can create a query that will list all of the computers that have either the 32-bit or 64-bit version of an application. I want to do the opposite. I want a collection where all of the members do NOT have
the latest versions of an application that comes in both 32-bit and 64-bit.
Correct me if I'm wrong, but wouldn't a NOT IN sub-select query be the only way to get a list of computers that do NOT have an application installed? I can create a NOT IN that works with either 32-bit or 64-bit ADD_REMOVE_PROGRAMS, but never both.Yes, for CM07 you will need to use sub-select. (CM12 you can use "exclude collections").
I think you need to use an OR, between your two sub-select statements, for your requirement.
edit: actually, maybe that should be an AND that you need. How about you try each, and let us know?
Don
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer", where applicable.
This helps the community, keeps the forums tidy, and recognises useful contributions. Thanks!) -
HI
I want a sql query i.e., all employees list which are having lessthan maximum salary of manager in same departmnet;
my table is like this
employees
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
if need the department table
departments:
DEPARTMENT_ID
NOT NULL NUMBER(4)
DEPARTMENT_NAME
NOT NULL VARCHAR2(30)
MANAGER_ID
NUMBER(6)
LOCATION_ID
NUMBER(4)Try this:
select
employees.last_name || ', ' || employees.first_name “Employee”,
employees.salary “Emp Salary”,
sub.salary “Mgr Salary”,
employees.department_id “Department”
from
employees,
( select
mgr.department_id dept,
max(mgr.salary) salary
from
employees,
employees mgr
where
employees.mgr = mgr.employee_id
group by
mgr.department_id) sub
where
employees.department_id = sub.dept
and employees.salary < sub.salary
Jeff -
Query for file version less than 10.0 does not return expected data
I'm trying to build a query for all PCs that have a version of Iexplore.exe in c:\windows\program files\ that is less than 10.0 . When I run the query it returns no data. When I change 10.0 in the query to 9.9 it returns files with version 10.xxx in
the results. Its as if it is seeing 10.0 as 1.0. Is this expected ?
select SMS_R_System.Name, SMS_R_System.ADSiteName, SMS_G_System_SoftwareFile.FileName, SMS_G_System_SoftwareFile.FileVersion from SMS_R_System inner join SMS_G_System_SoftwareFile on SMS_G_System_SoftwareFile.ResourceID = SMS_R_System.ResourceId where
SMS_G_System_SoftwareFile.FileName = "iexplore.exe" and SMS_G_System_SoftwareFile.FilePath = "C:\\program files\\internet explorer\\" and SMS_G_System_SoftwareFile.FileVersion < "9.9"It is because the values are not integer, they are a string. And therefore 1 is smaller then 9.
http://www.enhansoft.com/ -
[Oracle 8i] Query for N rows by column value?
I was just wondering if what I want to do is possible within my query (rather than programmatically)...
I want to return the N most recent records for each unique value in a particular column.
Here's a sample table:
CREATE TABLE orders
( order_no numeric(10)
part_no varchar(5)
close_date date
order_qty numeric(10)
scrap_qty numeric(10)
CONSTRAINT order_pk PRIMARY KEY (order_no)
);And some sample data....
INSERT INTO orders VALUES
(0000012345,'ABC-1',TO_DATE('01-01-2010','mm-dd-yyyy'),10,1);
INSERT INTO orders VALUES
(0000013498,'ABC-1',TO_DATE('01-05-2010','mm-dd-yyyy'),12,2);
INSERT INTO orders VALUES
(0000033452,'ABC-1',TO_DATE('01-10-2010','mm-dd-yyyy'),5,0);
INSERT INTO orders VALUES
(0000001468,'ABC-1',TO_DATE('01-15-2010','mm-dd-yyyy'),15,1);
INSERT INTO orders VALUES
(0000022349,'BR723',TO_DATE('01-03-2010','mm-dd-yyyy'),8,1);
INSERT INTO orders VALUES
(0000069581,'BR723',TO_DATE('01-05-2010','mm-dd-yyyy'),5,0);
INSERT INTO orders VALUES
(0000436721,'BR723',TO_DATE('01-10-2010','mm-dd-yyyy'),14,1);
INSERT INTO orders VALUES
(0000213446,'A5001',TO_DATE('01-06-2010','mm-dd-yyyy'),5,1);
INSERT INTO orders VALUES
(0000327987,'A5001',TO_DATE('01-08-2010','mm-dd-yyyy'),5,0);
INSERT INTO orders VALUES
(0000041353,'A5001',TO_DATE('01-14-2010','mm-dd-yyyy'),12,1);
INSERT INTO orders VALUES
(0000011241,'A5001',TO_DATE('01-15-2010','mm-dd-yyyy'),5,1);In this example, what I want to return are the 2 most recent orders (by close_date) for each part number.
Here is a table with the results I want to get, based on the scenario above:
order_no part_no close_date order_qty scrap_qty
0000001468 'ABC-1' '01-15-2010' 15 1
0000033452 'ABC-1' '01-10-2010' 5 0
0000436721 'BR723' '01-10-2010' 14 1
0000069581 'BR723' '01-05-2010' 5 0
0000011241 'A5001' '01-15-2010' 5 1
0000041353 'A5001' '01-14-2010' 12 1Is it possible to write a query to get these results, or am I going to have to query for all available data, and find the 2 most recent rows programmatically?
Thanks in advance!Hi,
user11033437 wrote:
I'm going to test that out right now. I think if it works, I may need to use dense_rank() rather than rank(), because it is possible that two orders for the same part number could have the same close date, and according to what I've looked up on the rank() and dense_rank() functions, rank() can give non-consecutive results if the values are the same.What's wrong with non-consecutive values?
Use RANK, DENSE_RANK or ROW_NUMBER depending on what you want.
For example; say a certain part has been ordered 8 times:
3 times with close_date January 29, 2010 (all at exactly the same time),
4 times with close_date January 28, 2010 (all at exactly the same time), and
1 time with close_date January 27, 2010.
If you ask for the last 2 rows:
RANK will give you the 3 rows from January 29. (All 3 have an equal claim to being in the top 2.)
DENSE_RANK will give you the 7 rows from January 28-29 (the last two values , regardless of how many rows have them).
ROW_NUMBER will give you 2 rows from January 29. (Which 2? It's arbitrary unless you add a tie-breaker to the ORDER BY clause.)
All these functions are available in Oracle 8.1.
Maybe you are looking for
-
Librfc32.dll exception for ECC 6.0
Hi, My customer just upgraded from SAP R/3 4 to ECC 6.0. All the RFC calls now raise an exception in the librfc32.dll. Here is the error from the event viewer: Faulting application consoleapplication1.vshost.exe, version 8.0.50727.42, faulting module
-
OWB repository assistant stucks 72 percent finished
Hello could anybody give suggestions what is the problem when I try to install my OWB repository and the installation always halts when theres 72 percent finished. I have Oracle 9.2.0.1.0 database and the OWB version is 9.2.0.2.8, operating system is
-
Acrobat 9 won't recognize scanner
I have a HP 8550A OfficeJet. Adobe Acrobat will not recognize the scanner (Scanner not found error) but Adobe Photoshop CS5 is able to scan without any problem. Any suggestions? I am running Windows Vista
-
Hai, I finished my masters in information systems and planning to start my career in SAP BW. I just want to have an idea about the different areas available in BW. Thanks
-
I used Software Update to install security update. Now Mail can't open.
I used Software Update to install security update. Now Mail can't open. I get a message saying that Mail can't run with my system. Computer is imac 2.8 GHz Intel Core 2 Duo running 10.6.8 Thanks.