Selecting max value using over()
Hi i'm unable to select the max rating from the below query
could you please help me out in this
with T as
(select 100001 ID , 'SLNG' RATING , 'AA-' RAT_SYBL , 1 TYPE FROM DUAL UNION ALL
SELECT 100001 , 'SLNG' , 'AA--' , 2 FROM DUAL
SELECT ID,RATING,CASE WHEN (RATING='SLNG')
THEN MAX(RAT_SYBL) OVER (PARTITION BY ID)
ELSE NULL
END AS SEL_RATING ,TYPE
FROM T I'm getting the 2 rows from the above but i need only one row to be displayed
Thank you
i need only one row to be displayed Maybe:
SQL> with t as (
select 100001 id, 'SLNG' rating, 'AA-' rat_sybl, 1 type from dual union all
select 100001, 'SLNG', 'AA--', 2 from dual
select id, rating, sel_rating, type
from (select id,
rating,
rat_sybl,
case when (rating = 'SLNG') then max (rat_sybl) over (partition by id) else null end as sel_rating,
type
from t)
where rat_sybl = sel_rating
ID RATING SEL_RATING TYPE
100001 SLNG AA-- 2
1 row selected.?
Similar Messages
-
Finding the max value using partition by
Hi
I've the following requirement where i need to select the max value from the data
WITH T AS
(SELECT 48003 ID ,'SPR' RTNG_TP_CD , 'INS' RL_CD ,'A-' RAT_CD FROM DUAL UNION ALL
SELECT 48003 , 'SLNG' ,'INS','A-' FROM DUAL)
SELECT distinct ID, RTNG_TP_CD,RL_CD,MAX(RAT_CD) over (partition by id) RT_CD
FROM T
For a single id if there are 2 RTNG_TP_CD then select the max(rat_cd) by displayin rtng_tp_cd =SLNG
Expected Output
48003 , SLNG , INS , A-Thank YouWITH T AS
(SELECT 48003 ID ,'SPR' RTNG_TP_CD , 'INS' RL_CD ,'A-' RAT_CD FROM DUAL UNION ALL
SELECT 48003 , 'SLNG' ,'INS','A-' FROM DUAL)
SELECT distinct ID,decode((select count(*) from t where id=t.id),1,RTNG_TP_CD, 'SLNG') RTNG_TP_CD,RL_CD,MAX(RAT_CD) over (partition by id) RT_CD
FROM T -
Problem in passing selection screen values using CALL TRANSACTION.
Hi All
I am facing problem in transfering selection screen values to the called transaction. I am trying to pass the path of the transaction filer but to no avail. The variable for filepath is not empty.
Below is my code:
DATA: lt_bdcdata TYPE TABLE OF bdcdata,
wa_bdcdata TYPE bdcdata,
opt TYPE ctu_params.
CLEAR wa_bdcdata.
wa_bdcdata-program = 'RFBASM00'.
wa_bdcdata-dynpro = '1000'.
wa_bdcdata-dynbegin = 'X'.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_CURSOR'.
wa_bdcdata-fval = 'RFPDO1-FEBUMSF'.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'RFPDO1-FEBUMSF'.
wa_bdcdata-fval = gv_filepath.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_OKCODE'.
wa_bdcdata-fval = 'PASS'.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
opt-dismode = 'E'.
opt-updmode = 'S'.
CALL TRANSACTION 'FF_5' USING lt_bdcdata OPTIONS FROM opt.
Please help.
HarshHi Harsh,
I think you have entered wrong main program for tcode 'FF_5' and wrong screen field for the file name. Use the below code instead of yours.
DATA: lt_bdcdata TYPE TABLE OF bdcdata,
wa_bdcdata TYPE bdcdata,
opt TYPE ctu_params,
gv_filepath type char128 value 'C:\testfile.txt'.
CLEAR wa_bdcdata.
wa_bdcdata-program = 'RFEBKA00'.
wa_bdcdata-dynpro = '1000'.
wa_bdcdata-dynbegin = 'X'.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'BDC_CURSOR'.
wa_bdcdata-fval = 'UMSFILE'.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = 'UMSFILE'.
wa_bdcdata-fval = gv_filepath.
APPEND wa_bdcdata TO lt_bdcdata.
CLEAR wa_bdcdata.
*wa_bdcdata-fnam = 'BDC_OKCODE'.
*wa_bdcdata-fval = 'PASS'.
*APPEND wa_bdcdata TO lt_bdcdata.
*CLEAR wa_bdcdata.
opt-dismode = 'A'.
opt-updmode = 'S'.
CALL TRANSACTION 'FF_5' USING lt_bdcdata OPTIONS FROM opt.
Thanks.
Regards,
Jey -
Selection screen value use as a header SQ01
Can anybody please tell me how to pick the values from the selection screen and use them as a header in SAP Query sq01
Arnulfo,
For this first you nedd to decalre the selection screens in infoset level.Means
1. Goto SQ02 -
Press Extras button--> Selections->Create>Popup will get display>Name(Ex: S_matnr))->Enter and select radio button Select options or parameters->Give description and Short text (it will display as label name for selection screen elemnts)
<b>---- understood</b>
against format for (if selectt options)----
MARA-matnr
against LIKE (If parameters) -
MARA-matnr.
<b>----you mean the data type of the one i will be displaying in selection screen??</b>
Under START-of-selection event in infoset.
Now in your code you can catch the values for s_matnr . and pass these values to final internal table.Heder data should be in first record.
<b>----i dont get this.. please be specific:o(</b>
In extras declare the header record fields and
Now use Record processing event.
read the records of internal table and pass those header data to extra fields.
Move these extras fields to field groups and select those in SQ01 for output.
Don't forget to reward if useful.....
and read the final inter -
Passing select-options values using call transaction method
Hi Experts,
I have a scenario in which i have three fields BUKRS,WERKS and MATKL in an internal table i_tab and I have MATNR as a selection-option.
After this I have to use call transaction <tcode> for all the line items in the internal table and the MATNR select-option values I have to pass directly for each line of i_tab using call transaction method.
TYPES: BEGIN OF t_tab,
bukrs TYPE bukrs,
werks TYPE werks_d,
matkl TYPE matkl,
END OF t_tab.
DATA: w_tab TYPE t_tab,
i_tab TYPE STANDARD TABLE OF t_tab.
SELECT-OPTIONS: s_matnr FOR marc-matnr.
Now I am putting a loop at i_tab and have to use CALL TRANSACTION <TCODE> for each line with the SELECT-OPTIONS for MATNR.
Please tell me whether we can pass multiple ranges for MATNR using call transcation method.
for example there can be multiple single values/multiple ranges/excluded ranges for MATNR. so please suggest me how tho achieve this sceanrio using CALL transaction method of BDC.
Thanks a lot.
Regards,
KrishanHi Krishan,
For the Call transaction TCODE there is extension ....OPTIONS from OPT. Just Check it out. I think it is possible like this.
... OPTIONS FROM opt
*Effect*
This addition gives you control using the values of the components of the structure opt, which must be of the Dictionary type CTU_PARAMS. The components have the following meaning:
DISMODE
Processing mode (comparable with the MODE addition)
UPDMODE
Update mode (comparable with the UPDATE addition)
CATTMODE
CATT mode (controlling a CATT procedure)
The CATT mode can have the following values:
' ' No CATT procedure active
'N' CATT procedure without single screen control
'A' CATT procedure with single screen control
DEFSIZE
Use standard window size
RACOMMIT
COMMIT WORK does not end CATT procedure
NOBINPT
No batch input mode, that s SY-BINPT = SPACE.
NOBIEND
No batch input mode after BDC data has been read
The components DEFSIZE , RACOMMIT, NOBINPT, NOBIEND always take the following values:
'X' Yes
' ' No
If the OPTIONS addition is omitted, the following settings are valid for the control parameters:
DISMODE from the MODE addition
UPDMODE
from the UPDATE addition
CATTMODE
No CATT procedure active
DEFSIZE
Do not use standard window size
RACOMMIT
COMMIT WORK ends procedure successfully
NOBINPT
Batch input mode, that is SY-BINPT =X.
NOBIEND
Batch input mode also active after BDC data has been read
Regards,
Swapna. -
Selecting Max Value from Huge Table
Dear Proffessionals
I have a huge table (20,000,000+ records) with the following columns:
[Time], [User], [Value]
The values in [Value] column can recur for a single User at a Time e.g.
2015-01-01, Me, X
2015-01-01, Me, Y
2015-01-01, Me, X
2015-01-02, Me, Z
2015-01-02, Me, X
2015-01-02, Me, Z
For each day, and for every user I want to have the maximum recurring value :
2015-01-01, Me, X
2015-01-02, Me, Z
to be inserted into another table.
PS: I want the MOST optimized way of achieving this functionality, bcause I am expecting a growth on the raw table over time, so PERFORMANCE is of great consideration.
I would really appreciate it, if somebody can help me.
RegardsI can think of two techniques based on the data selecticity
1) using row number function
2) using cross apply operator
USE Northwind;
-- Solution 1
SELECT S.SupplierID, S.CompanyName, CA.ProductID, CA.UnitPrice
FROM dbo.Suppliers AS S
CROSS APPLY
(SELECT TOP (10) *
FROM dbo.Products AS P
WHERE P.SupplierID = S.SupplierID
ORDER BY UnitPrice DESC, ProductID DESC) AS CA
ORDER BY S.SupplierID, CA.UnitPrice DESC, CA.ProductID DESC;
-- Solution 2
WITH C AS
SELECT S.SupplierID, S.CompanyName, P.ProductID, P.UnitPrice,
ROW_NUMBER() OVER(
PARTITION BY P.SupplierID
ORDER BY P.UnitPrice DESC, P.ProductID DESC) AS RowNum
FROM dbo.Suppliers AS S
JOIN dbo.Products AS P
ON P.SupplierID = S.SupplierID
SELECT SupplierID, CompanyName, ProductID, UnitPrice
FROM C
WHERE RowNum <= 10
ORDER BY SupplierID, ProductID DESC, UnitPrice DESC;
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
To Select Max Value in the table
Hello All,
I want to select a record from a Z table which has the following fields.
POSID
STLNR
DATUM
UZEIT
UNAME
TCODE
I have to select LAstest record depending upon the datum & uzeit field
I am using like this.
SELECT SINGLE UNAME
MAX (DATUM)
MAX (UZEIT) FROM Z48C_ZDIS_MD51 INTO
(G_R_MD51-UNAME,G_R_MD51-DATUM,G_R_MD51-UZETIT)
WHERE POSID = WA_PRPS-POSID
GROUP BY DATUM UZEIT.
But Its giving syntax error.Select All records into internal table. Sort descending by date and time. Read the first record.
data: itab type table of ztable with header line.
select * into itab from ztable.
sort itab descending by datum uzeit.
read table itab index 1.
Regards,
Rich Heilman -
Selecting single value using for all entries.
Hi Experts,
I want to know that is it possible to fetch only the first record for a particular condition while using for all entries.
For ex:
Suppose i got 10 different vbeln from vbak table into my internal table it_vbak. For a particular vbeln there can be multiple records in vbap table.
Now i need to fetch only the first record which is getting from vbap table for different vbeln while using 'for all entries in it_vbak where vbeln = it_vbak-vbeln'. Is it possible?
Thanks in Advance
Be$t!N
Moderator message - Moved to the correct forum
Edited by: Rob Burbank on Nov 17, 2009 9:38 AMHi Rob Burbank,
Thanks..
You are correct.. If that is the scenario in their company... Again it depends on the configuration and business process.. But it's possible that they may need to delete first on any line item after creation of sale order..
In that case below solution will work..
IF IT_VBAK[] IS NOT INITIAL.
SELECT * FROM VBAP
INTO TABLE IT_VBAP
for all entries in it_vbak
where vbeln = it_vbak-vbeln.
ENDIF.
SORT IT_VBAP BY VBELN POSNR.
LOOP AT IT_VBAK INTO WA_VBAK.
READ TABLE IT_VBAP INTO WA_VBAP WITH KEY VBELN = WA_VBAK-VBELN.
IF SY-SUBRC = 0.
APPEND WA_VBAP TO IT_VBAP2. " Another Internal table which stores only first record
ENDIF.
CLEAR : WA_VBAP.
ENDLOOP.
OR
IT_VBAP3[] = IT_VBAP[].
SORT IT_VBAP3 BY VBELN POSNR.
DELETE ADJACENT DUPLICATES FROM IT_VBAP3 COMPARING VBELN.
Now Table IT_VBAP2 and IT_VBAP3 will be having only first line items for all sales orders..
Do some little changes in the code as per your requirement.
Hope it will solve your problem..
Thanks & Regards
ilesh 24x7
ilesh Nandaniya -
Avoiding null and duplicate values using model clause
Hi,
I am trying to use model clause to get comma seperated list of data : following is the scenario:
testuser>select * from test1;
ID VALUE
1 Value1
2 Value2
3 Value3
4 Value4
5 Value4
6
7 value5
8
8 rows selected.
the query I have is:
testuser>with src as (
2 select distinct id,value
3 from test1
4 ),
5 t as (
6 select distinct substr(value,2) value
7 from src
8 model
9 ignore nav
10 dimension by (id)
11 measures (cast(value as varchar2(100)) value)
12 rules
13 (
14 value[any] order by id =
15 value[cv()-1] || ',' || value[cv()]
16 )
17 )
18 select max(value) oneline
19 from t;
ONELINE
Value1,Value2,Value3,Value4,Value4,,value5,
what I find is that this query has duplicate value and null (',,') coming in as data has null and duplicate value. Is there a way i can avoid the null and the duplicate values in the query output?
thanks,
Edited by: orausern on Feb 19, 2010 5:05 AMHi,
Try this code.
with
t as ( select substr(value,2)value,ind
from test1
model
ignore nav
dimension by (id)
measures (cast(value as varchar2(100)) value, 0 ind)
rules
( ind[any]= instr(value[cv()-1],value[cv()]),
value[any] order by id = value[cv()-1] || CASE WHEN value[cv()] IS NOT NULL
and ind[cv()]=0 THEN ',' || value[cv()] END
select max(value) oneline
from t;
SQL> select * from test1;
ID VALUE
1 Value1
2 Value2
3 Value3
4 Value4
5 Value4
6
7 value5
8
8 ligne(s) sélectionnée(s).
SQL> with
2 t as ( select substr(value,2)value,ind
3 from test1
4 model
5 ignore nav
6 dimension by (id)
7 measures (cast(value as varchar2(100)) value, 0 ind)
8 rules
9 ( ind[any]= instr(value[cv()-1],value[cv()]),
10 value[any] order by id = value[cv()-1] || CASE WHEN value[cv()] IS NOT NULL
11 and ind[cv()]=0 THEN ',' || value[cv()] END
12 )
13 )
14 select max(value) oneline
15 from t;
ONELINE
Value1,Value2,Value3,Value4,value5
SQL> -
Selecting a MAX value from a table & displaying it on a form using OCI PHP
WinXP Pro SP3- 32bit
Apache/2.2.22 (Win32) mod_fcgid/2.3.6 PHP/5.4.0
PHP Version 5.4.0
Oracle epxress 11g
problem: output on form displaying Resource id #4 instead of the number from the table.
(maybe it's number to string conversion issue?)
$conn = oci_connect('system', 'mypassword', 'localhost/xe');
$query = "SELECT MAX(CustNo)+1 AS MAXNUM FROM customer";
$stmt = OCIParse($conn, $query);
OCIExecute($stmt);
OCIFetch($stmt);
echo OCIResult($stmt, "MAXNUM")." will be next number.";
//OCIFreeStatement($stmt); //frees the resources- use a the end!
$daNextNo = $stmt;
echo $daNextNo;
//$daNextNo = OCIBindByName($stmt, ":text", &$form_text, -1); //not sure how this works if it'll fix the issue
?>
<form name="Addcust" action="process_cust.php" method="post">
<input type="text" name="CustNo" value="<?php echo $daNextNo;?>" /></br>
output of complete code:
45 will be next number.Resource id #4
And inside the textbox it says: 45 will be next number.Resource id #4
how can I put the 45 into $php variable so i can use it in a textbox or on a label?
Thank you in advance .THanks. i couldn;t find the PHP section.
I figured it out
i used nextval() to convert the object to a string.
<?php
$conn = oci_connect('system', '1234', 'localhost/xe');
if (!$conn) {
$m = oci_error();
trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
$query = "SELECT MAX(CustNo)+1 AS MAXNUM FROM customer";
//$daNextNo = $query;
$stmt = OCIParse($conn, $query);
OCIExecute($stmt);
OCIFetch($stmt);
echo OCIResult($stmt, "MAXNUM")." will be next number.";
//OCIFreeStatement($stmt); //frees the resources- use a the end!
$daNextNo = strval(OCIResult($stmt, "MAXNUM")." .");
$daNextNo = strval(OCIResult($stmt, "MAXNUM"));
echo "danextNo: ";
echo $daNextNo;
?>
<form name="Addcust" action="process_cust.php" method="post">
<div>
<dl>
<dt>* <?php //echo $this->lang->line('cust_fn'); ?>: </dt>
<dd><input type="text" name="CustNo" value="<?php echo $daNextNo;?>" /></dd> -
How to pick max value from a column of a table using cursor and iteration
Hello Everybody
I have a table loan_detail
and a column in it loan_amount
now i want to pick values from this table using cursor and then by using iteration i want to pick max value from it using that cursor
here is my table
LOAN_AMOUNT
100
200
300
400
500
5600
700i was able to do it using simple loop concepts but when i was trying to do this by using cursor i was not able to do it .
Regards
PeeyushSQL> SELECT MAX(sal) Highest_Sal,MIN(sal) Lowest_Sal FROM emp;
HIGHEST_SAL LOWEST_SAL
5000 800
SQL> set serverout on
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := counter;
10 counter := counter + 1;
11 END LOOP;
12 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
13 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
14 END;
15 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Even smaller
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := 1;
10 END LOOP;
11 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
12 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
13 END;
14 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Edited by: Saubhik on Jan 5, 2011 4:41 PM -
Min and MAx Value in a SELECT Statement
Hi,
I have a scenario where I am Selecting the values BETWEEN MIN and MAX values:
SELECT * FROM ABC WHERE CODE BETWEEN MIN(CODE) AND MAX(CODE)
ITS GETTING Error as:ORA-00934: group function is not allowed here
Any help will be needful for me.select substr(no,1,3)||to_char(substr(no,4,1)+1) "first missing number"
from
with t as
(select 'ABC1' no from dual
union select 'ABC2' from dual
union select 'ABC3' from dual
union select 'ABC5' from dual
union select 'ABC6' from dual
union select 'ABC8' from dual
select no, lead(no,1,0) over (order by no) next_no from t
where substr(next_no,4,1) - substr(no,4,1) > 1
and rownum = 1; -
Filtering internal table using select-option values
Hi,
I got an internal table with select-option values. for eg. it_perno with the values I BT 000160 000170.
Now i got another second internal table which is have the person number. Now i need to filter this second internal table based on the values from the select option table. my question is
i can collect all the values from 160 to 170 in a separate table by looping over the select option table. and then based on the values filter the second internal table person number.
But what would be the case if the select option contains both intervals and multiple options. For eg.
I BT 000160 000170.
I EQ 000185.
So can you suggest any good solution, that based on this select option table i need to filter the second internal table Person number.
Thanks in advance.
Regards,
anbu.Hi,
Can you not use the select-options in the Select query while populating the internal table.
SELECT pernr FROM pa0000 INTO it_tab WHERE pernr IN s_pernr.
If you cannot do this and you do not want to delete the unwanted entries, then you can use the WHERE clause in the LOOP statement to process the specific entries required:
LOOP AT it_tab INTO wa_tab WHERE pernr IN s_pernr.
Regards,
Aparna Alashe. -
Max value without using max() function
Hi
Is there any way to get the max value from a table without using MAX() function
Thankswell if you think about it i'm sure you'll find a solution
what does max(field) means, it simply is the value of the field where no other value of the same field that is > than this value exists.
consider the following :
table TAB(
fld NUMBER(5));
translate the logic and you'll have
select a.fld from TAB a where NOT EXISTS(select b.fld from TAB b where b.fld>a.fld) and rownum=1;
of course there are better ways i'm sure, you'll just have to figure'em out. -
Finding max value in column using java..
Hello there!!
I have a problem that i tried finding in google but did not get what i wanted.. that's why i hope you guys could help me out.
How do i find the maximum value in a column using resultset..
for example..
Connection con = DriverManager.getConnection("jdbc:mysql:///eproc","root", "");//eproc is dbase name in MySQL
Statement stmt=con.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT MAX(id) FROM cat_work_sor_category");now, how do i use resultset to make sure i get the max value and assign to integer variable...
Please do help me out.
Thankyou.Connection con =
DriverManager.getConnection("jdbc:mysql:///eproc","roo
t", "");//eproc is dbase name in MySQL
Statement stmt=con.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT MAX(id) FROM
cat_work_sor_category");
Integer MaxValue = new Integer(-1);
if(rs.next()){
MaxValue = rs.getInt(1);
int MAX_VALUE = MaxValue.intValue();
Maybe you are looking for
-
hi all, I have a text file called abc.txt which consits of the following data 001~ joshi (i.e account number and name - tilda seperated list). I have a table called abc which consits of two cols. acc_no and acc_name. My table consists of some data. N
-
HP Pavilion Elite e9280t Desktop PC power on/start windows issue
HP Pavilion Elite e9280t Desktop PC, Windows 7 Pro. When powered on (using the power switch), the blue power light and amber hard drive lights on the front illuminate, but monitor shows no signal and the keyboard lights do not come on. F10 on the k
-
Inventory Management (Non-cumulative values)
Dear experts, I want to extract inventory data from 2LIS_03_BF. How do I have to configure the Key figures; for example: 0VALSTCKQTY. Is there a need to use Non-cumulative value with in- and outflow as in Content. Which other possibilities exist? Aft
-
Change Tracking System in SQL Server 2012
Hi, I have enabled change tracking to one of the database with retention period 1 day and auto clean is true. As per this setting changed data of one day old should get cleaned up automatically. But i am able to see 4 days old data also using CHANGET
-
I was upgrading my iPhone to iso7 and it got stuck on connect to iTunes. How can I fix this ?