OPEN SQL - use of subquery to get max date
Hello,
I am trying to use a subquery to get the latest record, but cannot get the syntax correct.
select single from hrp1001 into mydata
where objid = '122334'
where begda = ( select max ( begda ) from hrp1001 where objid = '122334' )
but cannot get it right.
I am just trying to get the record with the max(date).
Thanks
Hello,
To get a better performance, I suggest you the following:
select single * from hrp1001 into corresponding fields of mydata
where objid = '122334'
order by begda descending.
Regards,
Similar Messages
-
Unable to open windows using boot camp. Get message "The bless tool was unable to set the current boot disk." I am using an Imac , Lion operating system, and Windows 7. It worked a few days ago. Any thoughts, Thank you.
Note that nowhere in the Boot Camp instructions does it tell you to use Disk Utility to format the Windows partition. The Boot Camp Assistant program creates the partition & sets the +partition scheme info+ of the disk as appropriate for the Windows installer but the Windows installer itself is responsible for formatting the new partition with the appropriate +file system scheme+ (NTFS for Windows 7).
If you follow the instructions in the Boot Camp Installation & Setup Guide to the letter you should have no problems installing Windows. -
In mdx how to get max date for all employees is it posible shall we use group by in mdx
in mdx how to get max date for all employees is it posible shall we use group by in mdx
example
empno ename date
1 hari 12-01-1982
1 hari 13-06-2000
by using above data i want to get max dataHi Hari3109,
According to your description, you want to get the max date for the employees, right?
In your scenario, do you want to get the max date for all the employees or for each employee? In MDX, we have the Max function to achieve your requirement. You can refer to Naveen's link or the link below to see the details.
http://www.sqldbpros.com/2013/08/get-the-max-date-from-a-cube-using-mdx/
If this is not what you want, please provide us more information about the structure of you cube, so that we can make further analysis.
Regards,
Charlie Liao
TechNet Community Support -
How to get max date in variable using dynamic query
Hi,
the following code gets all dates from sourcetable i want only max date , so i thought max function can be added and it will work
but still i have to create a table for one value(scalar) can get it in any other effeciant way.
declare
TYPE date_string IS TABLE OF VARCHAR(1000);
date_obj date_string;
BEGIN
EXECUTE IMMEDIATE 'SELECT to_char('''||day1||'-'||month1||'-'||year1||''') FROM '||source_schema||'.'|| sourcetable ||'' bulk collect INTO date_obj;
FOR indx IN date_obj.FIRST..date_obj.LAST loop
dbms_output.put_line(
date_obj(indx));
END loop;
DBMS_OUTPUT.PUT_LINE('Sample output');
END;
yours sincerely944768 wrote:
the following code gets all dates from sourcetableNo it doesn't. What is the datatype of day1, month1 and year1? They cannot be DATE datatypes otherwise your TO_CHAR would fail with all that concatenation going on. And your TO_CHAR is returning a VARCHAR2 datatype... so you cannot say that it is getting all dates... because there are no DATE datatypes returned. It's getting a lot of strings, but certainly not DATE's.
i want only max date , so i thought max function can be added and it will work You can use the MAX function on a DATE datatype, but not on strings (at least not in the way you intend it to work).
Converting it to a DATE before doing the MAX will allow you to get the maximum date (assuming the date format is correct)
EXECUTE IMMEDIATE 'SELECT max(to_date(to_char('''||day1||'-'||month1||'-'||year1||'''),''DD-MM-YYYY'')) FROM '||source_schema||'.'|| sourcetable ||'' bulk collect INTO date_obj;Then you will find have the other issues...
a) you are then going to be fetching your DATEs in to a collection of VARCHAR strings. (Not even VARCHAR2, very poor). This should be DATE datatype
b) you are bulk collecting into a collection, when you are using MAX which will return a single value in your example
And you really should address the design issues:
c) why are day, month and year, not being stored in the database as a single DATE datatype in the first place
d) why does your code not know the name of the table it's querying requiring the use of very poor dynamic SQL techniques.
e) why are you loading data in a collection in expensive PGA memory... what can't you do in SQL that requires you to collect the data into memory first? -
when I select a pdf file within a webpage and click to download instead of getting the "download" window and the file automatically opening after download using adobe reader I get a blank webpage (in the same tab) and in the bottom left corner it says"done". No file is downloaded and if I click "back" I just return to the webpage containing the link.
Aha! The properties for the plugin file nppdf32.dll for Adobe Reader indicate the file is for Adobe Acrobat. This misidentification is then used by SeaMonkey as a label in the helper application list.
Adobe Reader 11.0.3.37: nppdf32.dll has version 11.0.3.37. This is the version in SeaMonkey's plugins folder.
Adobe Acrobat 7.0.8.218: nppdf32.dll has version 7.0.9.50.
As for having both Adobe Reader and Adobe Acrobat on the same PC, this generally works providing Reader is installed AFTER Acrobat. Reader is freeware, and I want to keep it current. Acrobat is purchaseware, and I cannot afford to keep buying upgrades or newer versions. -
SQL Help getting Max() date....
Hi gurus, Here is my situation:
Here is the sample data:
Policy_Id Policy-Exp_Dt COl_1
123_____10/30/2008 __ 333
123_____ 09/25/2008___445
123_____ 08/30/2008___443
Here i have to get the Policy-Exp_Dt from the second row; In other words for all rows which have a similar Policy_Id i should first find out the max(Policy-Exp_Dt) and get the next smallest available date to that.
I have tried using
select Policy_Id, COl_1, Policy_Exp_Dt from
table_1
where Policy-Exp_Dt = (select max(Policy-Exp_Dt)-1 from
table_1 a
where a.policy_id = table_1.policy_id)
but here i am getting the value = 10/30/2008 - 1 which is 10/29/2008 but i need the value 9/25/2008 which is the next available date less than the max() date.
Please Advise.
Edited by: user521009 on Jan 9, 2009 1:56 PMI thought to use lag - maybe not as appropriate a ranking functions.
Also we have a case where there is only One record for a policy - exclude this I guess!
drop table Max_but_One;
create table Max_but_One
Policy_Id integer not null,
Policy_Expires date not null,
Policy_Note varchar2(20) not null
-- Standard data
insert into Max_but_One values (123, to_date('30-08-2008','dd-mm-yyyy'), 'First note');
insert into Max_but_One values (123, to_date('25-09-2008','dd-mm-yyyy'), 'Second note');
insert into Max_but_One values (123, to_date('30-10-2008','dd-mm-yyyy'), 'Third note');
-- Standard data again for double check
insert into Max_but_One values (223, to_date('01-11-2008','dd-mm-yyyy'), 'First note');
insert into Max_but_One values (223, to_date('02-11-2008','dd-mm-yyyy'), 'Second note');
-- Only one record for a policy!
insert into Max_but_One values (323, to_date('01-12-2008','dd-mm-yyyy'), 'First note');
-- Two or more records for a policy both having the same date which is prior to the max
insert into Max_but_One values (423, to_date('10-12-2008','dd-mm-yyyy'), 'First note');
insert into Max_but_One values (423, to_date('10-12-2008','dd-mm-yyyy'), 'Second note');
insert into Max_but_One values (423, to_date('11-12-2008','dd-mm-yyyy'), 'Third note');
break on Policy_ID
select Policy_Id, Policy_Expires, Policy_Note
from Max_but_One
order by Policy_Id, Policy_Expires
POLICY_ID POLICY_EX POLICY_NOTE
123 30-AUG-08 First note
25-SEP-08 Second note
30-OCT-08 Third note
223 01-NOV-08 First note
02-NOV-08 Second note
323 01-DEC-08 First note
423 10-DEC-08 First note
10-DEC-08 Second note
11-DEC-08 Third note
-- Max Policy_Expires
select t.*
from (select Policy_Id, Policy_Expires, Policy_Note
,max(Policy_Expires) over (partition by Policy_Id) as Max_Policy_Expires
from Max_but_One
) t
where t.Policy_Expires = t.Max_Policy_Expires
POLICY_ID POLICY_EX POLICY_NOTE MAX_POLIC
123 30-OCT-08 Third note 30-OCT-08
223 02-NOV-08 Second note 02-NOV-08
323 01-DEC-08 First note 01-DEC-08
423 11-DEC-08 Third note 11-DEC-08
-- Using LAG
select t.Policy_Id
,t.Prior_Policy_Expires as Policy_Expires
,t.Prior_Policy_Note as Policy_Note
from (select Policy_Id, Policy_Expires, Policy_Note
,max(Policy_Expires) over (partition by Policy_Id) as Max_Policy_Expires
,lag(Policy_Expires) over (partition by Policy_Id order by Policy_Expires) as Prior_Policy_Expires
,lag(Policy_Note) over (partition by Policy_Id order by Policy_Expires) as Prior_Policy_Note
from Max_but_One
) t
where t.Policy_Expires = t.Max_Policy_Expires
POLICY_ID POLICY_EX POLICY_NOTE
123 25-SEP-08 Second note
223 01-NOV-08 First note
323
423 10-DEC-08 Second note
-- To exclude the single record policy then add and t.Prior_Policy_Expires is not null
select t.Policy_Id
,t.Prior_Policy_Expires as Policy_Expires
,t.Prior_Policy_Note as Policy_Note
from (select Policy_Id, Policy_Expires, Policy_Note
,max(Policy_Expires) over (partition by Policy_Id) as Max_Policy_Expires
,lag(Policy_Expires) over (partition by Policy_Id order by Policy_Expires) as Prior_Policy_Expires
,lag(Policy_Note) over (partition by Policy_Id order by Policy_Expires) as Prior_Policy_Note
from Max_but_One
) t
where t.Policy_Expires = t.Max_Policy_Expires
and t.Prior_Policy_Expires is not null
POLICY_ID POLICY_EX POLICY_NOTE
123 25-SEP-08 Second note
223 01-NOV-08 First note
423 10-DEC-08 Second note
-- 423 is randomly picked here - could equally have got {423, 10-DEC-08, First note} -
Help - Need to Get Max(Date) from An Unrelated Table in PowerPivot
I have two tables in my Power Pivot model:
Table A (a many table)
Asset
SerialNumber
User
CTUT111
YC112233
Bob
CTUT222
ZZ221144
Susy
CTUT222
ZZ221144
Larry
CTUT333
AB332244
Bob
Table B (a many table, a CSV import from another system)
Asset
SerialNumber
CheckIn_Date
CTUT111
YC112233
6/15/2014
CTUT111
YC112233
6/20/2014
CTUT222
ZZ221144
6/18/2014
CTUT333
AB332244
6/20/2014
I know it appears odd that Table B would have two entries for the same Asset (CTUT111), but it does.
I know I could write a SQL query that gave me the MAX(CheckIn_Date), but I’m not staging these datasets in a SQL server first.
I’m pulling directly from CSV tables.
I don’t want to have bridge tables either, if possible.
So, I’m left with a many-to-many situation in Power Pivot.
I want to add a Calculated Column to Table A, called ‘Latest CheckIn Date’ – which I get from Table B, matching on Serial Number.
So, when I’m done, I would expect the value for Asset=CTUT111 to be 6/20/2014 (since it’s the MAX of a value of dates)
I’m trying this pattern from Marco Russo in which you don’t need to relate two tables to exchange information, but it doesn’t appear to be working.
=CALCULATE (
MAX ( VALUES ( TableB[CheckIn_Date] ) ),
FILTER (
TableB,
'TableA'[SerialNumber]
= TableB[SerialNumber]
I’ve also tried to use LOOKUPVALUE, but I can’t seem to figure it out.
Would appreciate the help.
Thanks in advance.
-EricHi Eric,
Please, try this one formula:
=CALCULATE (
MAXX ( VALUES( TableB[CheckIn_Date] ); TableB[CheckIn_Date] );
FILTER (
TableB;
'TableA'[SerialNumber] = TableB[SerialNumber]
P.S. In my locale I use ";". According to your locale you should use ",".
Truly yours,
Paul -
Open SQL statment for Update flag based on Date
Dear all,
I am trying to write an Open SQL statement to update a flag in a table. Table Ztable1 with fields Sr.No, Flag, Datefrom, DateTo. i would like to update Flag entry in the table only if today falls in between Datefrom & Dateto. I can satisfy the above requirement using the following ABAP code.
DATA: lv_timestamp TYPE timestamp,
lv_today LIKE adr2-valid_from,
tz TYPE timezone.
CONVERT DATE sy-datlo TIME sy-timlo INTO TIME STAMP lv_timestamp
TIME ZONE tz.
lv_today = lv_timestamp.
update ztable1 set flag = 'X' where lv_today BETWEEN datefrom and dateto.
But the issue is that, DateFrom & DateTo contains space aswell Dates. Datefrom can be space if it is start of Time (01010001) and also DateTo can be space if it is End of time (31129999). Which means that if DateFrom is space, then it should treated as 01010001, simlarly DateTo is space, then it should be 31129999. How can i write the if else cases within where clauses.
I know Decode statement in Native sql programming, but that won't fit for Opensql in ABAP. Also, because of huge entries in database, i cannot read entries, manupulate & then update.
How can i enhance the same above Update statement to cater this need.
Please advise.
Thanks a lot in advance.
Greetings, SatishHi,
first fetch records in to internal table.
ranges: r_range for sy-datum.
loop at itab into wa.
if wa-validfrom is initial.
wa-validfrom = (here u pass valid from date).
elseif wa-validto is initial
wa-validto = 99991231.
endif.
r_range-low = wa-validfrom
r_range-high = wa-validto
*check here current date is falling in interval. if its fall between ranges update flas in work area and *modify you internal table
if sy-datum in r_range.
wa-flag = 'x'.
modify itab from wa.
endif.
refresh r_range.
clear wa-flag.
endloop.
*--Finally update your ztable
modify ztable from table itab.
Regards,
Peranandam -
Trying to get max date grouped by type
hello,
i have a request table with:
request name
request start date
The requests run daily so i have multiple records. In answers i want to display request name and the max(request start date), grouped by request name. Is it possible to due this in answers only? if i must use the repository how do i do it? I'm new to building subject areas.You can do this in answers only, If you dont have access to RPD.
Sol1: Create a report with two columns request name,request start date. Open pivot table and add request start date to measures and apply Aggregation of Max on date.
sol2: Create a report with two columns request name,request start date, Change the Fx of request start date to max(request start date by request name) . This way table view also show request max date by request names. -
When using Power Management i get 'no data'
I'm using all sorts of reports in SCCM and all are working except the power management reports. I get the following message: 'no data'
The following options are turned on:The CM client is installed on every workstation and the resource explorer is populated. Is it possible to force the report instead of waiting +/- 30 days?
It is still unclear to me if you environment is healthy.
You say the query above only shows you 1 computer but every computer has the client installed. How many computers do you see listed in “Count operating system versions”?
For the computer show in the resource explorer screenshot, do you get any results for “Power Management - Computer activity by computer”
Feb 7 2015?
Until the data is collected from all of your computers and until you have several days’ worth of data, some reports will not full work. There is no way to hurry it up.
Garth Jones | My blogs: Enhansoft and
Old Blog site | Twitter:
@GarthMJ
1. Count Operating System Versions:
Microsoft
Windows 7 Enterprise
932
Microsoft
Windows 8.1 Enterprise
1
Microsoft
Windows Server 2008 R2 Enterprise
1
Microsoft
Windows XP Professional
13
2. For the computer in the screenshot (and other workstations) i get "no Data". When i click onclick for detailed information i get some data like
the Power Settings Plan. -
How to get max(date) with BI Answers?
Hi,
I have a fact table with costs of projects and several dimension tables. The data in the fact table is stored day-based and is related to a time dimension. Additionally there is a relation between the fact table and the project dimension. A project has several dimension attributes like "current"
Now I want to create the following query in BI Answers:
Get the costs to a project where the "current" attribute was set to Y and show the related date.
My thoughts were, that I'm looking in the project dimension where the "current" attribute is set to Y and do a join on the fact table. Therefore I get several dates, because there are more than one day where the project status was set to Y. How can I get the last, highest date?
GreetingsHi,
Maybe you could order by date descending and show the Top N (=1).
Good Luck,
Daan Bakboord -
Using analytic function to get the data
Hi
Version is 11g
My table has this data
NAME SALARY LAST_UPDA
a 1000 01-JAN-07
a 2000 01-JAN-09
a 2500 01-JUN-10
b 2000 01-AUG-10
c 5000 01-JUN-07
c 6000 08-JAN-09
c 4500 01-FEB-10
I want to pick the salary and name of the person when it was last updated (max(last_update_date)).
Couple of ways of doing this I think are
SELECT distinct name,
TRUNC(
AVG(salary) KEEP (DENSE_RANK LAST
ORDER BY TO_CHAR(last_update_Date,'YYYY') )
OVER (PARTITION BY NAME)
) t
FROM kmdebug;
OR
SELECT * FROM KMDEBUG
WHERE (LAST_UPDATE_DATE, NAME) IN (SELECT MAX(LAST_UPDATE_DATE), NAME
FROM KMDEBUG
GROUP BY NAME);
They give the desired result.
NAME SALARY LAST_UPDA
a 2500 01-JUN-10
b 2000 01-AUG-10
c 4500 01-FEB-10
But the problem with the first version is DISTINCT. I want to get result set without using DISTINCT
The problem with second version is, it could be inefficient way of doing it espeically when KMDEBUG table would be big.
Thank you
MSKA couple of thoughts.
1. 11g is not a version number.
SELECT * FROM v$version;2. Read the FAQ and learn how to use tags to format your listing so others can read it. (blue circular icon to the right)
3. Post DDL to create your table and DML to load your sample data.
Then, perhaps, someone can try your query and consider how to get you what you want.
And, when you make requests like "without using DISTINCT" you need to explain why. Because otherwise this just looks like someone trying to get us to do their homework for them. -
How to get max date in child table
I have tow tables Departments as the master table and Employees as the child table
The employees table (the child) has employee_hiring_date Field .
I want to get the maximum hiring date in the employee table for every department to make validation over it.
How to make this.It's more or less described in the doc I gave you.
OK, lets do it with your sample.
I assume you have accessors defined to navigate between the master and the detail (in both directions). The accessors on entity level are named
"Employees1" to get the employees of a department
"Departments1" to get the department of an employee
1. In the Departments entity you add a transient attribute "MaxSalary" as type Number, select 'Expression' as 'Value Type' and type "Employees1.max("Salary")" in the value field. Make sure to uncheck the 'Persistent' attribute in the dialog. This will mark the attribute as transient.
2. in the entity Employees open the 'Business Rules' and select the 'Saraly' attribute. Add a rule by clicking the green plus. 'Rule Type' is 'Compare', 'Operator' is 'LessOrEqualTo' and in the 'Compare With' drop down select 'Expression'. In the Dialog enter "Departments1.MaxSalary". Switchto the 'FailureHandling' tab and type in a message you like to see (e.g. 'The Salary is too high!") and save your work. The magic is done in the expression "Departments1.MaxSalary" which selects the department of the employee and gets the value of the attribute 'MaxSalary' which is an other Groovy expression which gets the max salary of all employees in the department.
3. To get the 'MaxSalary' attribute from the entity to the view object, you can open the VO editor and select the 'Attributes' section. Click the arrow down right of green plus and select 'Add Attribute from Entity, shuffle the MaxSalary attribute you see in the Department entity to the right and save your work.
If you now test the app in the Tester try changing the salary of an employee to a value higher then max of the department and you should see your error message.
Timo -
Getting max date and max time column plz help
hi
i have a table with suppose 7 columns with date and time column seperate
i want to design a query which retrieve the current or the maxmum date with the max of time in that date.
columns a ,b,c, date, time
22-05-07 20
23-05-07 50
24-05-07 40
25-05-07 30
22-05-07 20
ans " suppose current date is 25 "
a,b,c,25-05-07,40try like this..
SQL> with rt as
2 (select 1 col1,'22-05-07' dt, 20 tm from dual union all
3 select 2,'23-05-07', 50 from dual union all
4 select 3,'24-05-07', 40 from dual union all
5 select 4,'25-05-07', 30 from dual union all
6 select 5,'25-05-07', 45 from dual union all
7 select 6,'22-05-07', 20 from dual)
8 select col1,dt,tm from
9 (select col1,
10 dt,
11 max(tm) over(partition by to_date(dt,'DD-MM-RR') order by col1 desc) tm,
12 row_number() over(partition by to_date(dt,'DD-MM-RR') order by col1 desc) rn
13 from rt where to_date(dt,'DD-MM-RR') in (select max(to_date(dt,'DD-MM-RR')) dt from rt))
14 where rn = 1;
COL1 DT TM
5 25-05-07 45
SQL> -
Help to get max date of the group
Hi, How I can get the max(date1) for the group on the basis of Code1,Code2,Code3.
Code1 Code2 Code3 Date1 RTCODE
A A1 A2 1/1/2012 SER
A A1 A2 1/1/2013 SER
A A1 A2 1/1/2015 TER
B B1 B2 1/1/2011 JTS
B B1 B2 1/1/2012 JTR
C C1 C2 1/1/2010 HYR
C C1 C2 1/1/2011 JST
Expected results from query should be:
Code1 Code2 Code3 Date1 RTCODE
A A1 A2 1/1/2015 TER
B B1 B2 1/1/2012 JTR
C C1 C2 1/1/2011 JSTHi,
You can try this:
CREATE TABLE #T
CODE1 VARCHAR(5),
CODE2 VARCHAR(5),
CODE3 VARCHAR(5),
DATE1 DATE,
RTCODE VARCHAR(5)
--DROP TABLE #T
INSERT INTO #T
VALUES
('A','A1','A2','20120101','SER'),
('A','A1','A2','20130101','SER'),
('A','A1','A2','20150101','TER'),
('B','B1','B2','20110101','JTS'),
('B','B1','B2','20120101','JTR'),
('C','C1','C2','20100101','HYR'),
('C','C1','C2','20110101','JST');
--TRUNCATE TABLE #T
WITH TEST AS
SELECT CODE1, CODE2, CODE3, MAX(DATE1) AS DT_MAX
FROM #T
GROUP BY CODE1, CODE2, CODE3
SELECT T.CODE1,T.CODE2, T.CODE3, T.DT_MAX,#T.RTCODE
FROM TEST AS T
INNER JOIN #T ON T.CODE1=#T.CODE1
AND T.CODE2=#T.CODE2
AND T.CODE3=#T.CODE3
AND T.DT_MAX=#T.DATE1
ORDER BY T.CODE1,T.CODE2,T.CODE3
Regards,
Reshma
Please Vote as Helpful if an answer is helpful and/or Please mark Proposed as Answer or Mark As Answer when question is answered
Maybe you are looking for
-
TS1538 my itunes cannot detect iphone because of itunes library.itl
my itunes cannot detect iphone because of itunes library.itl
-
Subcontrac vendor delivers finished goods to customer. (Third party?)
The business process is as follow: We provide components to the subcontract vendor and they will delivery the finished goods directly to the customer. How to acheive this scenario in SAP? Can there be a statistical GR like third party processing? (an
-
Missing songs and videos on my Apple TV
I've synced my laptop to my TV multiple times over the last few weeks, but many of my music videos and songs don't make the transfer to my Apple TV. For instance about 2,000 of my purchased somgs from Itunes and most of my purchased music videos don'
-
Hi, I need to use an Interop DLL in my C# web application. I have successfully registered the managed DLL using regsvr32 and the generated COM Interop DLL using regasm. The CLSID is available under WOW6432 in the registry. But when I run the applicat
-
Hi, My production database servers run on HP-XP storage, raid 5. Currently the redo logs are striped on 16 disks, as raw files. I use 5 groups, 2 members in each group. I wanted to hear your opinion, if two members are still necessary with todays sto