Query for months & quarter in date functions
Hi All
Could some on assist me how to get Last Year 1st,2nd,,, months and Current Year 1st,2nd,3rd,,, months and same way Quarterly please.
I have date column Trade_date (dd/Mon/yyyy), Trade_price in Trade table
DB:Oracle 11g
Any assist would be appreciated
All you have done is repeat your unclear question with more words.
Here is how to get all the months for prior year and this year
SQL> alter session set nls_date_format = 'Mon-YYYY';
Session altered.
SQL> select
2 add_months(this_year, - 12) last_year,
3 this_year
4 from
5 (
6 select
7 add_months(trunc(sysdate,'y'), level - 1) this_year
8 from
9 dual
10 connect by
11 level <= 12
12 );
LAST_YEA THIS_YEA
Jan-2010 Jan-2011
Feb-2010 Feb-2011
Mar-2010 Mar-2011
Apr-2010 Apr-2011
May-2010 May-2011
Jun-2010 Jun-2011
Jul-2010 Jul-2011
Aug-2010 Aug-2011
Sep-2010 Sep-2011
Oct-2010 Oct-2011
Nov-2010 Nov-2011
Dec-2010 Dec-2011
12 rows selected.And quarters
SQL> select
2 add_months(this_year, - 12) last_year,
3 this_year
4 from
5 (
6 select
7 add_months(trunc(sysdate,'y'), (level - 1) * 3) this_year
8 from
9 dual
10 connect by
11 level <= 4
12 );
LAST_YEA THIS_YEA
Jan-2010 Jan-2011
Apr-2010 Apr-2011
Jul-2010 Jul-2011
Oct-2010 Oct-2011
SQL> alter session set nls_date_format = 'dd/mm/yyyy';
Session altered.
SQL> select
2 add_months(this_year, - 12) last_year,
3 this_year
4 from
5 (
6 select
7 add_months(trunc(sysdate,'y'), (level - 1) * 3) this_year
8 from
9 dual
10 connect by
11 level <= 4
12 );
LAST_YEAR THIS_YEAR
01/01/2010 01/01/2011
01/04/2010 01/04/2011
01/07/2010 01/07/2011
01/10/2010 01/10/2011As you can see from the second example, the display format of dates is controlled by the session,
>
he date column is trade_date (dd/mm/yyyy)
>
So this is still incorrect - if it is a date the format is not dd/mm/yyyy and, it is still not clear how this relates to your other questions.
Similar Messages
-
Query For Xquery Implementation of Date Function
Hi All,
Objective: In array Item, I have to check for device entry Dates.
First device entry date would be the date corresponding to device seq no =0
Latest entry date would be the date corresponding to max device seq no.
Diff date should be differnce between first entry date and latest entry dates in days.
Output should be like
<FirstEntrydate></FirstEntrydate>
<LatestEntryDate></LatestEntryDate>
<Diffdate></Diffdate>
<Maxdevice Seq no></MaxdeviceSeqno>
the XML to transform is :
<?xml version="1.0"?>
<ns0:contractDeviceHistory xmlns:ns0="http://www.lhsgroup.com/ws_v3">
<ns0:item>
*<ns0:deviceEntryDate>*
*<ns0:date>2015-01-09T09:30:22</ns0:date>*
*</ns0:deviceEntryDate>*
<ns0:deviceId>3</ns0:deviceId>
<ns0:deviceModDate>
<ns0:date>2013-01-21T09:30:22</ns0:date>
</ns0:deviceModDate>
<ns0:devicePort>devicePort_1</ns0:devicePort>
*<ns0:deviceSeqno>2</ns0:deviceSeqno>*
<ns0:deviceSmNum>deviceSmNum_1</ns0:deviceSmNum>
<ns0:deviceStatus>deviceStatus_1</ns0:deviceStatus>
<ns0:deviceUserLastMod>deviceUserLastMod_1</ns0:deviceUserLastMod>
<ns0:deviceValidFrom>
<ns0:date>2013-01-06T09:30:22</ns0:date>
</ns0:deviceValidFrom>
<ns0:hlcode>32</ns0:hlcode>
<ns0:hlcodePub>hlcodePub_1</ns0:hlcodePub>
<ns0:reason>64</ns0:reason>
</ns0:item>
<ns0:item>
*<ns0:deviceEntryDate>*
*<ns0:date>2014-01-09T09:30:22</ns0:date>*
*</ns0:deviceEntryDate>*
<ns0:deviceId>3</ns0:deviceId>
<ns0:deviceModDate>
<ns0:date>2013-01-21T09:30:22</ns0:date>
</ns0:deviceModDate>
<ns0:devicePort>devicePort_1</ns0:devicePort>
*<ns0:deviceSeqno>1</ns0:deviceSeqno>*
<ns0:deviceSmNum>deviceSmNum_1</ns0:deviceSmNum>
<ns0:deviceStatus>deviceStatus_1</ns0:deviceStatus>
<ns0:deviceUserLastMod>deviceUserLastMod_1</ns0:deviceUserLastMod>
<ns0:deviceValidFrom>
<ns0:date>2013-01-06T09:30:22</ns0:date>
</ns0:deviceValidFrom>
<ns0:hlcode>32</ns0:hlcode>
<ns0:hlcodePub>hlcodePub_1</ns0:hlcodePub>
<ns0:reason>64</ns0:reason>
</ns0:item>
<ns0:item>
*<ns0:deviceEntryDate>*
*<ns0:date>2012-01-09T09:30:22</ns0:date>*
*</ns0:deviceEntryDate>*
<ns0:deviceId>3</ns0:deviceId>
<ns0:deviceModDate>
<ns0:date>2012-01-21T09:30:22</ns0:date>
</ns0:deviceModDate>
<ns0:devicePort>devicePort_1</ns0:devicePort>
*<ns0:deviceSeqno>0.0</ns0:deviceSeqno>*
<ns0:deviceSmNum>deviceSmNum_1</ns0:deviceSmNum>
<ns0:deviceStatus>deviceStatus_1</ns0:deviceStatus>
<ns0:deviceUserLastMod>deviceUserLastMod_1</ns0:deviceUserLastMod>
<ns0:deviceValidFrom>
<ns0:date>2013-01-06T09:30:22</ns0:date>
</ns0:deviceValidFrom>
<ns0:hlcode>32</ns0:hlcode>
<ns0:hlcodePub>hlcodePub_1</ns0:hlcodePub>
<ns0:reason>64</ns0:reason>
</ns0:item>
</ns0:contractDeviceHistory>
The Incomplete Xquery i wrote is this, which is not working. Plz suggest
declare function xf:SIMReplacementInfoResBSCSToSiebel($contractDeviceHistory1 as element())
as element(ns0:SIMReplacementResponse) {
<ns0:SIMReplacementResponse>
<ns0:SIMReplacement>
for $item in $contractDeviceHistory1/ns1:item
group $item as $group by
$item/ns1:deviceSeqno as $key1,
$item/ns1:deviceEntryDate/ns1:date as $key2
order by $key1 descending
return
<ns0:SIMReplacement
MaxDeviceSeqNo= "{ fn:max($key1) }"
FirstEntry_date = "{if(($key1) = '0.0')then($key2) else (0)}"
LatestEntry_date="{if(($key1)= fn:max($key1))then($key2) else ($key2)}"
/> }
<ns0:Status>Success</ns0:Status>
</ns0:SIMReplacement>
</ns0:SIMReplacementResponse>
declare variable $contractDeviceHistory1 as element() external;
xf:SIMReplacementInfoResBSCSToSiebel($contractDeviceHistory1)
Regards
Manu
India.Post Author: jsiegmund
CA Forum: Data Connectivity and SQL
Not really, that function is a start, but to calculate the true ammount of work days there are a lot more variables to take into account. For instance, easter depends on the easter sunday which isn't a fixed date. In one year it may be in may, in the next it's in april. So defining a list of holidays (as done in the function in your example) isn't going to cut it. Therefore I mentioned implementing the function in Crystal Reports is a hard job which I wouldn't gladly take on. In the meantime, I've come upon an other solution using SQL Server 2005. It's possible to use assembly's in SQL Server, so I compiled the function as an assembly and loaded that into SQL Server. I needed information from the database for my reports anyway, so now I'm able to do the calculations in the views in the database, instead of leaving that to Crystal. Somewhat of a different solution, but it works for now. It would be great if future versions of Crystal would support the loading of assembly files though -
Need a query for monthly Report
Hello All,
I need a query for monthly report,
comp_code
emp_id
dept_id
work_day
100
A100
MECH
01/01/2013
100
A100
MECH
02/01/2013
100
A100
MECH
03/01/2013
100
A100
MECH
04/01/2013
100
A100
MECH
05/02/2013
100
A100
MECH
08/02/2013
100
A100
MECH
09/02/2013
100
A100
MECH
10/02/2013
100
A100
MECH
12/05/2013
100
A100
MECH
13/05/2013
100
A101
CIV
01/04/2013
100
A101
CIV
02/04/2013
100
A101
CIV
03/04/2013
100
A101
CIV
04/04/2013
100
A101
CIV
06/04/2013
100
A101
CIV
06/06/2013
100
A101
CIV
07/06/2013
100
A101
CIV
08/06/2013
100
A101
CIV
09/06/2013
100
A101
CIV
10/06/2013
100
A101
CIV
11/12/2013
100
A101
CIV
12/12/2013
100
A101
CIV
13/12/2013
100
A101
CIV
14/12/2013
Dear friends this the sample table of my report.In which table has contain list of employees with their working days(actual table has contain almost 5laks of records).
suppose user choose the date between 01/01/2013 and 31/12/2013 then the result should be like this.
comp_code
emp_id
dept_id
month
Total_work
100
A100
MECH
JANUARY
4
100
A100
MECH
FEBRUARY
2
100
A100
MECH
MARCH
0
100
A100
MECH
APRIL
0
100
A100
MECH
MAY
2
100
A100
MECH
JUNE
0
100
A100
MECH
JULY
0
100
A100
MECH
AUGUST
0
100
A100
MECH
SEPTEMBER
0
100
A100
MECH
OCTOBER
0
100
A100
MECH
NOVEMBER
0
100
A100
MECH
DECEMBER
0
100
A101
CIV
JANUARY
0
100
A101
CIV
FEBRUARY
0
100
A101
CIV
MARCH
0
100
A101
CIV
APRIL
5
100
A101
CIV
MAY
0
100
A101
CIV
JUNE
5
100
A101
CIV
JULY
0
100
A101
CIV
AUGUST
0
100
A101
CIV
SEPTEMBER
0
100
A101
CIV
OCTOBER
0
100
A101
CIV
NOVEMBER
0
100
A101
CIV
DECEMBER
4Hi,
If you want the output to include months where no work was done (with 0 in the total_work column) then you need to outer-join a "table" that has one row per month, and make it a partitioned outer join:
WITH got_end_points AS
SELECT TRUNC (MIN (work_day), 'MONTH') AS first_month
, TRUNC (MAX (work_day), 'MONTH') AS last_month
FROM table_x
, all_months AS
SELECT ADD_MONTHS (first_month, LEVEL - 1) AS a_month
, ADD_MONTHS (first_month, LEVEL) AS next_month
FROM got_end_points
CONNECT BY LEVEL <= 1 + MONTHS_BETWEEN (last_month, first_month)
SELECT t.comp_code
, t.emp_id
, t.dept_id
, m.a_month
, COUNT (t.work_day) AS total_work
FROM all_months m
LEFT OUTER JOIN table_x t PARTITION BY (t.comp_code, t.emp_id, t.ept_id)
ON t.work_day >= a.a_month
AND t.work_day < a.next_month
GROUP BY t.comp_code
, t.emp_id
, t.dept_id
, m.a_month
ORDER BY t.comp_code
, t.emp_id
, t.dept_id
, m.a_month
As posted, this include every month that is actually in the table. You can change the first sub-query if you want to enter first and last months.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Point out where the query above is giving the wrong results, and explain, using specific examples, how you get the correct results from the given data in those places. If you changed the query at all, post your code.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
Query for month start and end date
Hi,
I want to pick every month Start date and End Date. could anybody suggest what is the query for this.
I need output
Start Date End Date
01/01/2011 31/01/2011
01/02/2011 28/02/2011
01/03/2011 31/03/2011
01/04/2011 30/04/2011
01/05/2011 .......Hi,
SQL> select sysdate from dual;
SYSDATE
24-FEB-11
SQL> select last_day(sysdate) from dual;
LAST_DAY(
28-FEB-11
SQL> select last_day(sysdate),last_day(add_months(sysdate,-1))+1 from dual;
LAST_DAY( LAST_DAY(
28-FEB-11 01-FEB-11
SQL> select last_day(sysdate),last_day(add_months(sysdate,-2))+1 from dual;
LAST_DAY( LAST_DAY(
28-FEB-11 01-JAN-11Try to refer to Oracle documentation and try to experiments the functions, you will get results on your hand.
- Pavan Kumar N
Edited by: Pavan Kumar on Feb 24, 2011 12:51 PM
Edited by: Pavan Kumar on Feb 24, 2011 12:52 PM -
Want the Average of sales for months up to date, with empty months included
How do I create an MDX that gives me the average of , f.ex., Sales
Note that some months may not have sales, but the month should still be counted.
The result I want to get out is a listing of Years with average of Sales for each year. Where current year only calculates up to current month.
Lets say that todays month is 2014-05 and we have the following figures for this year.
Month Sales
2014-01 10
2014-02 20
2014-03 Null
2014-04 15
2014-05 10
The calculation should be Sum(Sales) divided by 5 months: 55/5 = 11
If I use:
Avg( Descendants([Date].[Calendar].[Month].CurrentMemeber
, [Date].[Calendar].[Month])
, [Measures].[Internet Sales]
It will exclude 2014-03, which is wrong!
If I use CoalesceEmpty() to fill in the Nulls:
Avg( Descendants([Date].[Calendar].[Month].CurrentMemeber
, [Date].[Calendar].[Month])
, CoalesceEmpty([Measures].[Internet Sales],0)
It will fill in 0 for all months 2014 (up to 2014-12), which is also wrong!
It feels like a really simple calculation, but I can't make it work.
ThanksI finally had to fix it by checking the number of months to get it right.
This solved my problem and gave me the correct averages on year and month level.
Create Member CurrentCube.[Measures].[COMPLAINT COUNT AVG CALC] As Null
,Format_String = ";;;\N\/\A",VISIBLE = 1;
// For Year level - Show average of year = Sum/NumOfMonth. If this year then up to this month else divide by 12
SCOPE([COMP CREATION DATE].[YEAR].[YEAR], [Measures].[COMPLAINT COUNT AVG CALC]);
This = IIF([COMP CREATION DATE].[IS THIS YEAR].&[Y],
DIVIDE(SUM([COMPLAINT Count]), Format(now(), "MM")),
DIVIDE(SUM([COMPLAINT Count]), 12)
Format_String(This)="# ### ##0";
NON_EMPTY_BEHAVIOR(This) = { [Measures].[COMPLAINT Count] };
END SCOPE;
// For month level - Show average of month = Sum/NumOfDaysInMonth
SCOPE( [COMP CREATION DATE].[Year-Month-Date].[YEAR MONTHNO], [Measures].[COMPLAINT COUNT AVG CALC]);
This = IIF(not isempty([Measures].[COMPLAINT Count]),
Avg(
Descendants(
[COMP CREATION DATE].[Year-Month-Date].CurrentMember,
[COMP CREATION DATE].[Year-Month-Date].[DATE ID]
CoalesceEmpty([Measures].[COMPLAINT Count],0)
,Null
Format_String(This)="# ### ##0";
NON_EMPTY_BEHAVIOR(This) = { [Measures].[COMPLAINT Count] };
END SCOPE;
mStenport -
MDX query for to get the data from two cubes
Hi
Can you tell me how to create MDX query to get the values from two cubes. (One hierarchy from first cube and one hierarchy from second cube)
Can you give me one example.
Regards,
Madhu.
SudhanHi Sudhan,
According to your description, you want to retrieve data from two different cubes, right? The short answer is yes. To query multiple cubes from a single MDX statement use the LOOKUPCUBE function (you can't specify multiple cubes in your FROM statement).
The LOOKUPCUBE function will only work on cubes that utilize the same source database as the cube on which the MDX statement is running. For the detail information about it, please refer to the link below to see the blog.
Retrieving Data From Multiple Cubes in an MDX Query Using the Lookupcube Function
Regards,
Charlie Liao
TechNet Community Support -
I need to make a query that checks to see if the start_date is less than sysdate and the end_date is greater than sysdate
SELECT * FROM table WHERE OFFICE_START_DATE < SYSDATE AND OFFICE_END_DATE > SYSDATE
What is the easiest way of doing this?
Thanksuser9179751 wrote:
It wasn't working for me initially but I had to format the date to validate.
SELECT * FROM TABLE WHERE OFFICE_START_DATE < TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS')
AND OFFICE_END_DATE > TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS');As skvaish said, it looks as if your columns are character (varchar2, most likely) and not dates. And if so, your solution is still buggy. You should be doing
select *
from mytable
where to_date(office_start_date) < sysdate
and to_date(office_end_date) > sysdate;If you insist on thinking that a chracter string that looks like a date is a date, what do you make of this:
First, create a table with a char column used as a date:
SQL> drop table mydemo;
Table dropped.
SQL> create table mydemo (
2 alfa_date varchar2(11),
3 rite_date date
4 );
Table created.Now insert some data
SQL> --
SQL> insert into mydemo values ('01-JAN-2010', to_date('01-JAN-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('01-FEB-2010', to_date('01-FEB-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('01-APR-2010', to_date('01-APR-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('02-JAN-2010', to_date('02-JAN-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('02-FEB-2010', to_date('02-FEB-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('02-APR-2010', to_date('02-APR-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('03-JAN-2010', to_date('03-JAN-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('03-FEB-2010', to_date('03-FEB-2010','DD-MON-YYYY'));
1 row created.
SQL> insert into mydemo values ('03-APR-2010', to_date('03-APR-2010','DD-MON-YYYY'));
1 row created.
SQL> commit;
Commit complete.Now, select, and order on the alfa-date;
SQL> select * from mydemo order by alfa_date;
ALFA_DATE RITE_DATE
01-APR-2010 01-APR-10
01-FEB-2010 01-FEB-10
01-JAN-2010 01-JAN-10
02-APR-2010 02-APR-10
02-FEB-2010 02-FEB-10
02-JAN-2010 02-JAN-10
03-APR-2010 03-APR-10
03-FEB-2010 03-FEB-10
03-JAN-2010 03-JAN-10
9 rows selected.Are they in date sequence?
No they are not.
Why not?
Now select, and order on the properly defined date column.
SQL> select * from mydemo order by rite_date;
ALFA_DATE RITE_DATE
01-JAN-2010 01-JAN-10
02-JAN-2010 02-JAN-10
03-JAN-2010 03-JAN-10
01-FEB-2010 01-FEB-10
02-FEB-2010 02-FEB-10
03-FEB-2010 03-FEB-10
01-APR-2010 01-APR-10
02-APR-2010 02-APR-10
03-APR-2010 03-APR-10
9 rows selected.
SQL> spo off -
Query for monthly and QTR change
Post Author: sugumar
CA Forum: General Feedback
monthly change= previous month amount - current month amt
Quarterly Change= Previous Quarter amt - current quarter amtPost Author: amr_foci
CA Forum: General Feedback
if i got you right,, you can make two queries one for the current month and one for the previous month, thats will give you two measures , Q1.current_month and Q2.previous_month
substarct them to get the month change
and you can use merge dimensions for any dimensions
good luck -
Query For Large Amount of Data
Hello All,
I apologize in advance if I am not posting this in the right section. I am fairly new to APEX and database designing. My goal is to create an inquiry screen for a database of people.
I am running APEX 4.2 on 11g. The information is store in 3 tables; Names, Demographics, Address. Each table had a PIN ID column that ties them all together. Each table has almost a million rows in them.
Currently I have it set up that the person types in the name they want to search and it gets passed into a hidden page item on the next page where there is a report with a select statement based on the page item. Everything works right now however it is slow. I am having a 5-10 second delay before the results come up.
My question is, is there a better way to set up these tables. What is the best way to make this faster?
I'm sorry if this is a vague question but any help, or point in the right direction will be greatly appreciated
Thank You !976533 wrote:
Hello All,Welcome to the forum: please read the FAQ and forum sticky threads (if you haven't done so already), and update your forum profile with a real handle instead of "976533".
When you have a problem you'll get a faster, more effective response by including as much relevant information as possible upfront. This should include:
<li>Full APEX version
<li>Full DB/version/edition/host OS
<li>Web server architecture (EPG, OHS or APEX listener/host OS)
<li>Browser(s) and version(s) used
<li>Theme
<li>Template(s)
<li>Region/item type(s) (making particular distinction as to whether a "report" is a standard report, an interactive report, or in fact an "updateable report" (i.e. a tabular form)
With APEX we're also fortunate to have a great resource in apex.oracle.com where we can reproduce and share problems. Reproducing things there is the best way to troubleshoot most issues, especially those relating to layout and visual formatting. If you expect a detailed answer then it's appropriate for you to take on a significant part of the effort by getting as far as possible with an example of the problem on apex.oracle.com before asking for assistance with specific issues, which we can then see at first hand.
I apologize in advance if I am not posting this in the right section. I am fairly new to APEX and database designing. My goal is to create an inquiry screen for a database of people.It might be more appropriate to the {forum:id=75} forum, so you should look at the following entries on their FAQ as well:
<li>{message:id=9360002}
<li>{message:id=9360003}
I am running APEX 4.2 on 11g. The information is store in 3 tables; Names, Demographics, Address. Each table had a PIN ID column that ties them all together. Each table has almost a million rows in them.
Currently I have it set up that the person types in the name they want to search and it gets passed into a hidden page item on the next page where there is a report with a select statement based on the page item. Everything works right now however it is slow. I am having a 5-10 second delay before the results come up.
My question is, is there a better way to set up these tables. What is the best way to make this faster? Are there suitable indexes on the tables?
Does the report query use them?
As described above, either: reproduce the problem on apex.oracle.com; or post DDL to allow us to recreate the tables and indexes, and the SQL from your report. -
Query for Business Partner Master data
Respected Sir/Madam,
I want a Query in Bp Master Data----> "Name" Should Allow Only Capital Letters.Can anyone please helpme out from this.Hi,
Apply FMS to BP Name field.
Set below query in FMS and tick auto refresh with "When Exiting Altered Column" and select field Quantity and select "Refresh Regularly" .
Select UCASE($[OCRD.CardName)
Regards,
Clint -
Query for below scenario using analytical functions
Hi,
Below is the data format which I have.
COL-1 COL-2
1-Jan-13 5
2-Jan-13
3-Jan-13
4-Jan-13
5-Jan-13 10
6-Jan-13
7-Jan-13
8-Jan-13
9-Jan-13 15
10-Jan-13
Expected
COL-1 COL-2
1-Jan-13 5
2-Jan-13 5
3-Jan-13 5
4-Jan-13 5
5-Jan-13 10
6-Jan-13 10
7-Jan-13 10
8-Jan-13 10
9-Jan-13 15
10-Jan-13 15;
Thanks in advance
Edited by: unique on May 14, 2013 2:23 PMunique wrote:
Hi,
Below is the data format which I have.
COL-1 COL-2
1-Jan-13 5
2-Jan-13
3-Jan-13
4-Jan-13
5-Jan-13 10
6-Jan-13
7-Jan-13
8-Jan-13
9-Jan-13 15
10-Jan-13
Expected
COL-1 COL-2
1-Jan-13 5
2-Jan-13 5
3-Jan-13 5
4-Jan-13 5
5-Jan-13 10
6-Jan-13 10
7-Jan-13 10
8-Jan-13 10
9-Jan-13 15
10-Jan-13 15;
Thanks in advance
Edited by: unique on May 14, 2013 2:23 PMAny rationale behind your output?
How about you try to explain WHY the output is supposed to be as you ask for.
I can think of more than 1 way to do this, each of which would be wrong if I make the wrong assumption about what you are actually trying to do.
Cheers, -
Query for selecting distinct by date
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
I have a simple table that has an ID, Title, and Date.
I need to select the most recent ID (without duplicates). I was looking at using a UNION but couldn't quite get it to work.
P3XGL2FV96 Test 1 11-AUG-10 03.38.06.000000000 PM
1VGJ74PSNW Test 2 11-AUG-10 04.02.22.000000000 PM
1VGJ74PSNW Test 2 12-AUG-10 09.49.09.000000000 AMHi,
You could achieve it like this :
CREATE TABLE MY_TABLE
+(+
ID VARCHAR2(10),
TITLE VARCHAR2(10),
MY_DATE TIMESTAMP
+);+
insert into my_table values ('P3XGL2FV96', 'Test 1', '11-AUG-10 03.38.06.000000000 PM');
insert into my_table values ('1VGJ74PSNW', 'Test 2', '12-AUG-10 09.49.09.000000000 AM');
insert into my_table values ('1VGJ74PSNW', 'Test 2', '11-AUG-10 04.02.22.000000000 PM');
commit;
SELECT id, title, my_date
FROM (SELECT id,
title,
my_date,
RANK () OVER (PARTITION BY id ORDER BY my_date DESC) rk
FROM my_table)
WHERE rk = 1;
ID |TITLE |MY_DATE
----------|----------|---------------------------------------------------------------------------
+1VGJ74PSNW|Test 2 |12-AUG-10 09.49.09.000000 AM+
P3XGL2FV96|Test 1 |11-AUG-10 03.38.06.000000 PM
Remark : if one id have twice the same date they will be both ranked first
Kind regards,
Ludovic
Edited by: ludovic.sz on Aug 12, 2010 8:38 AM -
Query for getting combination of data
hi all,
i am using db10g.
i have a table of fields a_company,a_code,a_name,a_port,a_t_port,a_rate,a_volume
i have a records like
MM,AA,A-NAME,EFG,EFGH,100,20
MM,AA,A-NAME,EFG,EFGH,200,10
MM,BB,B-NAME,HIJ,HIJK,100,20
MM,BB,B-NAME,HIJ,HIJK,200,30
MM,CC,C-NAME,DEF,DEFG,500,20i have to select all columns but the fields if below below combination repeates(having more than 1 combination)
a_company,a_code,a_port,a_t_port
how can form a query?
in other words i am expecting the result like below among above said input.
MM,AA,A-NAME,EFG,EFGH,100,20
MM,AA,A-NAME,EFG,EFGH,200,10
MM,BB,B-NAME,HIJ,HIJK,100,20
MM,BB,B-NAME,HIJ,HIJK,200,30Thanks..With only one table scan:
SQL> create table a_table (a_company,a_code,a_name,a_port,a_t_port,a_rate,a_volume)
2 as
3 select 'MM','AA','A-NAME','EFG','EFGH',100,20 from dual union all
4 select 'MM','AA','A-NAME','EFG','EFGH',200,10 from dual union all
5 select 'MM','BB','B-NAME','HIJ','HIJK',100,20 from dual union all
6 select 'MM','BB','B-NAME','HIJ','HIJK',200,30 from dual union all
7 select 'MM','CC','C-NAME','DEF','DEFG',500,20 from dual
8 /
Table created.
SQL> exec dbms_stats.gather_table_stats(user,'a_table')
PL/SQL procedure successfully completed.
SQL> set autotrace on explain
SQL> select a_company
2 , a_code
3 , a_name
4 , a_port
5 , a_t_port
6 , a_rate
7 , a_volume
8 from ( select a.a_company
9 , a.a_code
10 , a.a_name
11 , a.a_port
12 , a.a_t_port
13 , a.a_rate
14 , a.a_volume
15 , count(*) over (partition by a_company,a_code,a_port,a_t_port) cnt
16 from a_table a
17 )
18 where cnt > 1
19 /
A_ A_ A_NAME A_P A_T_ A_RATE A_VOLUME
MM AA A-NAME EFG EFGH 100 20
MM AA A-NAME EFG EFGH 200 10
MM BB B-NAME HIJ HIJK 100 20
MM BB B-NAME HIJ HIJK 200 30
4 rows selected.
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=4 Card=5 Bytes=330)
1 0 VIEW (Cost=4 Card=5 Bytes=330)
2 1 WINDOW (SORT) (Cost=4 Card=5 Bytes=140)
3 2 TABLE ACCESS (FULL) OF 'A_TABLE' (Cost=2 Card=5 Bytes=140)Regards,
Rob. -
Query for spatial data with a GeometryCollection fails
There are exact 538 CurvePolygons (only exterior rings at this
sample). All of them are valid geometries and equal in dimension
and so on. Now I connect them to a GeometryCollection and query
for other relating spatial data in some tables. It seems that
the use of around (not exact!) 200 CurvePolygon in one
GeometryCollection works fine but the adding of more
CurvePolygon result in an error with the Spatial Index (I could
add the ORA- error numbers if I have some data in my test tables
again next days).
Is there anybody else having trouble with these mysterious
problem? Maybe there is a border by the number of points in
GeometryCollection?
(More details, programming code could be delivered)
(working with Java 1.3.1, oracle.sdoapi.*, Oracle 8.1.7.)Hi Lutz,
Could you provide more info or samples of what is going wrong?
Also, could you try making sure the geometry you are passing in
as the query window is valid (i.e. instead of passing it in as a
query window, pass it into sdo_geom.validate_geometry).
Thanks,
Dan -
Query for select CLOB data field
Hi All,
I want to know how to write a SQL query for select CLOB tyoe data from a tablw. when I am trying to use the simple SQL it gives an messageLOB types requires OCI8 mode and currently uasing OCI7 ode.
Also I am not aware of oci mode?
If its working for OCI8 mode how I should changer it in to OCI8 mode? (I am using 10g rel 2)
Thans and regards
BuddhikeHi ,
i don't want to use loop (sy-tabix) ..
any particular reason for this ?
as u cannot automatically generate serial number, u have to go for loop....endloop.
Thanks
Karthik
Maybe you are looking for
-
No logical space left to create more user strings
So, what I tried to do was to put over 700,000 strings into an array. That seems impossible according to this error. Error 1 Unexpected error writing metadata to file -- 'No logical space left to create more user strings.' What do you recommend me to
-
Migrating Sybase Anywhere 8.0 to Oracle 10g
I was asking Oracle help to develop a plugin whereby my company could use it as a tool to help us to migrate to Oracle 10g from Sybase Anywhere 8.0. However I was refer to here by someone from Oracle. Anyone have experience on this? Thanks Steven
-
SYCLO INSTALLATION PRE-REQUISITES and Procedure
Hello guys, i am now starting a new mobility enhancement on SYCLO. Since i am new to SYCLO i googled some of its concepts and my primary concern is to configure Syclo in our Enterprise Landscape. so i want to know about the pre-requi
-
Export scripts automation windows 2003 server
i want some automatic export scripts that run a specific time on daily basis My platform is windows My Database is oracle 10g Please help
-
Intel iMac and new digital cameras
Hi, Thinking of purchasing a Sony digital camera with the built in HD (vs. buying those that use a DVD/CD). The HD type can hold up to 5hrs. of video vs. the CD version's 1 hr. However, it's newer technology so don't know much about the pros/cons. Do