Select the last Date of multicolumn and its value adjustent cust_id
I have a Table with 5 customer_id and 5 Dates with each belonging to its cust_id.
I need to take the last, in my example would be CUSTID_5. I know
this with the GREATEST who select the CHANGE_DATE_5 (in my example), but
How to choose the CUSTID_x? that goes along with the winning Date?
create table test_tbl)
number of custid_1
date of change_date_1,
number of custid_2
date of change_date_2,
number of custid_3
date of change_date_3,
number of custid_4
date of change_date_4,
number of custid_5
date of change_date_5
);
INSERT INTO test_tbl
(CUSTID_1, CHANGE_DATE_1, CUSTID_2, CHANGE_DATE_2, CUSTID_3, CHANGE_DATE_3, CUSTID_4, CHANGE_DATE_4, CUSTID_5, CHANGE_DATE_5)
VALUES
(111, sysdate, 222, sysdate + 1, 333, sysdate + 2, 444, sysdate + 3, 555, sysdate + 4);
commit;
Hello
Here's one way:
WITH unpivoted_data AS
(
SELECT custid, change_date
, EVALUATE () OVER (ORDER BY change_date DESC) AS r_num
OF test_tbl
UNPIVOT ((custid, change_date)
Label ((custid_1, change_date_1) AS 1)
, (custid_2, change_date_2) AS 2
, (custid_3, change_date_3) AS 3
, (custid_4, change_date_4) AS 4
, (custid_5, change_date_5) AS 5
)
)
)
SELECT custid, change_date
Of unpivoted_data
WHERE r_num = 1
;
Having several columns for several occurrences of basically the same thing is a bad table. This problem would be much simpler, but also more effective if each pair custid/change_date has been strored on a separate line. In addition, you would not be limited to 5 pairs.
Tags: Database
Similar Questions
-
I have 2 tables, INVOICE and INVOICE_REFNUM similar to below. I'm writing a query that withdrawing the first date (less) for a single record remaining the two tables. Each table records the receipt of invoices, but according to the source of the received invoice some may reside in INVOICE_REFNUM and others the INVOICE table. I also use DECODE on the topic of INVOICE_SOURCE, when the date less is taken from the INVOICE table that contains that field, so if the date is in the field INVOICE_REFNUM DECODE is not necessary. Example:
INVOICE (table)
INVOICE_GID INVOICE_SOURCE INSERT_DATE
ABC. AVRT123 I HAVE 11/03/2012-15:49:32
ABC. CNWY234 G 12/03/2012 14:07:30
ABC. UPGF678 M 15/03/2012 20:21:54
INVOICE_REFNUM (table)
INVOICE_GID INVOICE_REFNUM_QUAL INVOICE_REFNUM_VALUE INSERT_DATE
ABC. AVRT123 SOURCE ACS 11/03/2012 14:49:32
ABC. CNWY234 SOURCE CSA 12/03/2012 13:07:30
ABC. UPGF678 SOURCE ACS 15/03/2012 21:21:54
My decode statement is: DECODE ("INVOICE_SOURCE, 'I', 'IDE', 'G', ' Auto-Pay", ', 'Manual')
If I were to write a query on these two tables, the output will resemble the following:
INVOICE_GID INVOICE_SOURCE INSERT_DATE
ABC. AVRT123 ACS 11/03/2012 14:49:32
ABC. CNWY234 ACS 12/03/2012 13:07:30
ABC. UPGF678 Manual 15/03/2012 20:21:54
I worked on it for 2 days, and I was able to pull a date by using the following SQL code, but cannot determine how to extract the corresponding source:
SELECT THE LEAST (GLOGOWNER. INVOICE_REFNUM. INSERT_DATE, GLOGOWNER. THE INVOICE. INSERT_DATE) AS 'DATE OF THE INVOICE.
GLOGOWNER. THE INVOICE. INVOICE_GID,
DECODE (GLOGOWNER. THE INVOICE. INVOICE_SOURCE, 'I', 'IDE', 'G', 'Auto-Pay', ', 'Manual').
GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_VALUE
OF GLOGOWNER. INVOICE
LEFT JOIN GLOGOWNER. INVOICE_REFNUM
ON GLOGOWNER. THE INVOICE. INVOICE_GID = GLOGOWNER. INVOICE_REFNUM. INVOICE_GID
WHERE GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_VALUE = "ACS."
AND GLOGOWNER. INVOICE_REFNUM. INVOICE_REFNUM_QUAL_GID = 'SOURCE '.
AND GLOGOWNER. THE INVOICE. INVOICE_TYPE = 'I '.
ORDER OF GLOGOWNER. THE INVOICE. INVOICE_GID
Any help is greatly appreciated!
Best regards
-Adam
Hello
Is that what you want?
SELECT i.invoice_gid
CASE
WHEN ir.insert_date<= i.insert_date ="" then ="">=>
WHEN i.invoice_souce = 'I' and THEN 'EDI '.
WHEN i.invoice_souce = 'G' THEN 'debit '.
WHEN i.invoice_souce = am' THEN 'manual '.
END AS invoice_source
, The LEAST (i.insert_date, ir.insert_date) AS insert_date
I have the Bill OF
JOIN invoice_refnum ON i.invoice_gid = ir.invoice_gid ir
WHERE ir.invoice_refnum_value = 'ACS '.
AND ir.invoice_refnum_qual_gid = 'SOURCE '.
AND i.invoice_type = 'I '.
ORDER BY i.invoice_gid
;
There is no reason to do an outer join if you only want the lines with specific values anyway.
What happens if the 2 tables have exactly the same invoice_date? The query above takes the invoice_source of invoice_refnum in this case, but that can easily be changed.
Or the other of the insert_dates can be NULL?
I hope that answers your question.
Otherwise, thank you post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also after the results you want from these data (otherwise what you have already posted).
Explain, using specific examples, how you get these results from these data.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: https://forums.oracle.com/message/9362002#9362002
-
Hello
I have a table that looks like this:
no date of activity
1 sport 2008-11-23
2 bike 12/15/2008
3-Dy free 01/05/2009
How to choose the last day in the table above? I tried this:
Select the activity of tbl_above
where < < date is the last > >
I want back the 'activity' only, so I got to put the 'check' the ' where clause'
Any idea?Please see this...
SQL > select * from emp;
EMPNO, ENAME, JOB HIREDATE DEPTNO COMM SAL MGR
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH COMMITTED 7902 17 DECEMBER 20 800 80
7499 ALLEN 7698 1600 20 FEBRUARY SALESMAN 81 300 30
7521 WARD 7698 1250 22 FEBRUARY SALESMAN 81 500 30
7566 JONES MANAGER 7839 2975 2 APRIL 81 20
7654 MARTIN 7698 28 - SEP - 81 1250 1400 30 SALESMAN
7698 BLAKE MANAGER 7839 2850 MAY 1, 81 30
7782 CLARK MANAGER 7839 2450 JUNE 9 81 10
7788 SCOTT ANALYST 7566 3000 APRIL 19, 87 20
PRESIDENT OF 7839 17 NOVEMBER KING 81 5000 10
7844 TURNER 7698 08 - SEP - 81 1500 0 30 SALESMAN
7876 ADAMS 7788 1100 23 MAY CLERK 87 20EMPNO, ENAME, JOB HIREDATE DEPTNO COMM SAL MGR
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7900 7698 DECEMBER 3, CLERK JAMES 81 950 30
7902 7566 3000 3 DECEMBER ANALYST FORD 81 20
7934 MILLER COMMITTED JANUARY 7782 1300 23 82 10
1-1-1015 selected lines.
SQL > select empno from emp where hiredate = (select max (hiredate) from emp);
EMPNO
----------
7876SQL >
Concerning
-
Select the last record of each month
Hello
to select the last docnumber and qty of every month
Structure
Name Null? Type
------------------------------- -------- ----
PRODUCTCODE NUMBER
NUMBER NUMBER
DATE OF PLANFORMONTH
NUMBER OF CLEXPECTEDQTY
NUMBER OF CLCONFIRMQTY
NUMBER OF CLFGBUFFERQTY
NUMBER OF CLTOTPRINTBUFFER
in this select query if I take the sum he required all records where, as I want the last record of each month
SELECT productcode docnumber MAX (Number), planformonth, SUM (clexpectedqty) clexpectedqty (planformonth) MAX.
SUM (clconfirmqty) clconfirmqty, SUM (clfgbufferqty) clfgbufferqty, cltotprintbuffer OF FGSALESPLAN of the SUM (cltotprintbuffer)
GROUP BY productcode, (planformonth)
Sandy
Hello
Thanks its working now the code is written as below:
SELECT * FROM
(SELECT
e.*,
ROW_NUMBER() OVER (partition by PRODUCTCODE by DOCNUMBER DESC order) r
Of
E FGSALESPLAN)
WHERE r = 1 AND
entryvalid = 'Y '.
order by Number
Sandy
-
How to select the last 20 transactions in a table
Hi guys,.
I have a table named INOUT with following columns
EMPLOYEECODE VARCHAR2
DATE OF THE RESPONDENT
each day employees now their me 'in '. then entered came and came in this table. If we want to select the last 20 EMPLOYEECODE in this table that make their selves 'in '. How to choose that? just 20 last employees who traded. Help, pleaseHello
This is called a Query Top - N , you want to choose elements of N (N = 20, in this case) from the top of a sorted list.
Here's a way to do it:WITH got_r_num AS ( SELECT emloyeecode , MAX (intime) AS latest_intime -- If wanted , RANK () OVER (ORDER BY MAX (intime) DESC) AS r_num FROM inout GROUP BY employeecode ) SELECT employeecode , latest_intime -- If wanted , r_num -- If wanted FROM got_r_num WHERE r_num <= 20 ;
That actaully could take more than 20 people: If there be a tie, it includes any person who is entitled to be in the top 20. If you want exactly 20 people, then add the columns of tiebreaker to analytical or use ROW_NUMBER instead of RANK ORDER BY clause.
-
How to select the last 7 days of my table, I use the query, but it is the calculation according to sysdate
It is the data of my talble DP_DATE --------- 03-APR-10 21-APR-10 22-APR-10 23-APR-10 26-APR-10 27-APR-10 28-APR-10 29-APR-10 30-APR-10 03-MAY-10 04-MAY-10 11 rows selected.
I want to choose last 7 daysselect distinct dp_date from dailyprice where trunc(dp_date) between trunc(sysdate-7) and trunc(sysdate) this query is returning DP_DATE --------- 29-APR-10 30-APR-10 03-MAY-10 04-MAY-10
APRIL 26, 10
APRIL 27, 10
28 APRIL 10
APRIL 29, 10
30 APRIL 10
MAY 3, 10
4 MAY 10
kindly Guide
concerningHello
Use DENSE_RANK() rather than ROW_NUMBER().
with mytable as ( select 'abcFund' FND , to_date('09-MAY-10','dd-mon-rr') dp_date, 10 price from dual union all select 'dddFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all select 'edfFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all select 'eeeFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all select 'dddFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all select 'edfFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all select 'eeeFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all select 'eeeFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all select 'dddFund', to_date('07-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('07-MAY-10','dd-mon-rr'), 12 from dual union all select 'edfFund', to_date('07-MAY-10','dd-mon-rr'), 12 from dual union all select 'edfFund', to_date('05-MAY-10','dd-mon-rr'), 11 from dual union all select 'dddFund', to_date('05-MAY-10','dd-mon-rr'), 12 from dual union all select 'eeeFund', to_date('05-MAY-10','dd-mon-rr'), 12 from dual union all select 'dddFund', to_date('04-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('04-MAY-10','dd-mon-rr'), 12 from dual union all select 'dddFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all select 'edfFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all select 'eeeFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all select 'dddFund', to_date('01-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('01-MAY-10','dd-mon-rr'), 12 from dual union all select 'abcFund', to_date('30-APR-10','dd-mon-rr'), 11 from dual ) select dp_date from ( select dp_date , dense_rank() over (order by dp_date desc) rn from mytable ) where rn <= 7;
Courtesy: Rob SQL
* 009 *.Published by: 009 on May 6, 2010 05:01
-
Don't know how it happened but my file deleted is past the last date in an alphabetical list.
Don't know how it happened but my file deleted is past the last date in an alphabetical list. How ti to return with the last date in?
original title: deleted file.If you mean the trash in Explorer mode, simply click on the column heading "Date deleted" until the most recent appears first on the list. If you do not have this column, do a right click No matter what column header and click to put a check on 'Delete Date' in the context menu that appears.
-
DateFields 5.0 + operating systems. Impossible to select the current date.
Have a slight situation. Have a date with the value field by default of - etc. The user is not able to select the current date, and must choose another date and what to do again to select the current date.
This works very well in the pre - 5.0 OSes, but does not work with 5.0 +.
The date field popup works fine, but if the user tries to select the current date, it is not get populated in the date field.
I see what you mean now. I connected it the Issue Tracker here: https://www.blackberry.com/jira/browse/JAVAAPI-1163
-
DateField, can not be able to select the current date
Hai,
I use DateField in my application. Initially, the date field is showing-/-/---(JJ/MM/AAAA) this format. So if I click on this field to date I can't able to select the current date. If I go on any later date or past date and define it and that I can not choose the current date.
Hai,
Thax for your quick response...
I found this question in "Issue Tracker" here: https://www.blackberry.com/jira/browse/JAVAAPI-1163 -
How to get the last date of 3 days for the current month?
Hello. Guy
How to get the last date of 3 days for the current month?
MY OUTPUT WOULD LOOK LIKE THIS
JANUARY 29, 2016
JANUARY 30, 2016
JANUARY 31, 2016
GUYS HELP ME / / /...
SQL > select last_day (sysdate) - level + 1 double connect by level<= 3="" order="" by="">=>
LAST_DAY)
---------
29 JANUARY 16
30 JANUARY 16
31 JANUARY 16
-
How to find the last date of execution of the objects in database
Hi all
We're going for an upgrade of the database 11g and for the same thing, we want to know the last date of the following-
Packages, Tables, views
Conc. programs / value Sets / look up / profile options
Is there a way we can find the same thing.
The answers would be really useful.
Kind regards
ShrutiSo can you provide clues on how to find the last opportunity registration
I don't think you can find such a script with having activated audit.
Thank you
Hussein -
Select the last 10 records in a table
Oracle9i. I am trying to select the last 10 numbers in a table with this command series
but it's not not selecting the most recent serial number 10. How to do?select serial from tab_42 where employeecode='00001' and rownum<=10 order by serial desc;
The rownum clause is applied before the order by clause, one must use a subquery...
select * from (select serial from tab_42 where employeecode='00001' order by serial desc) where rownum<=10;
-
How to select the last 3 records in a table?
I have a table named HRM_ATTENDANCE
as follows
the table contains the record of leave of employees as followsName Type Nullable Default Comments ------------ ------------ -------- ------- -------- LEAVEDATE DATE EMPLOYEECODE VARCHAR2(8) LEAVETYPE VARCHAR2(4) APPROVEDBY VARCHAR2(8) Y FEEDBY VARCHAR2(8) Y FEEDTIME DATE Y REMARKS VARCHAR2(50) Y
I want to select the last 3 leaves (records) to employeecode "00001" so I don't know the what are the dates of the leave. in easy words I want to choose the folder where leavedate > = 5 June 2013 ", but I don't know any dates."LEAVEDATE EMPLOYEECODE LEAVETYPE APPROVEDBY FEEDBY FEEDTIME REMARKS ----------- ------------ --------- ---------- -------- ----------- -------------------------------------------------- 6/1/2013 00001 CL 6/3/2013 00001 EL 6/5/2013 00001 CPL 6/7/2013 00001 SL 6/9/2013 00001 CL
Select * from HRM_ATTENDANCE where rownum<=3 order="" by="" leavedate="">=3>
-
To get the last date of the year
Can someone tell me how to get the date of the year from the first date of last year:
My query below gives the last date of the month
SELECT LAST_DAY (ADD_MONTHS (TO_DATE (JANUARY 1, 2012 "," DD/MM/YYYY '), 12-))
TO_NUMBER (to_char (sysdate, 'mm'))) OF the DOUBLE
Thnx in advanceSELECT LAST_DAY (ADD_MONTHS (TO_DATE ('01/01/2012', 'DD/MM/YYYY'), 11)) FROM DUAL
-
Get the amount of the last date
Greedings,
Sorry for my question of amateur but how is it possible to get the (column1) amount of the last date (column2)
To get the last date we use Column2 but how to get the amount of this line of Column1?
Thank you for your helpAnother way using analytical functions...
SQL> select distinct first_value(sal) over (order by hiredate desc) from emp; FIRST_VALUE(SAL)OVER(ORDERBYHIREDATEDESC) ----------------------------------------- 1100
Order by hiredate desc, gives us the dates in descending order, the 'maximum' date is first, then we can get the first_value of the column we want based on that (give us the first value of lines in ascending order of lines). Because this is an analytic function, it applies the same value to all the rows in the table, then use separate to get just the value of a.
Maybe you are looking for
-
Satellite C55-A-1JL - 10 Windows sound driver - cannot create the instance of the APO
Hello. I have Toshiba Satellite C55 - A (PSCGAE). Yesterday, I've updated my Windows 8.1 to Windows 10 and install the drivers. Version 1.2.41.0 of his Studio of DTS (32/64 bit). When I try to open this application I have error: cannot create the ins
-
Compaq Presario: NVIDIA GeForce 6150SE nForce 430 chipset is not compatible with Windows 10
I try to upgrade to Windows 10 and I get message that current chipset is not compatible with Windows 10. My PC is Compaq Presario motherboard M2N68-(NARRA5). Is there a graphics card for this motherboard is compatible Windows 10?
-
My Macbook Air stores 43,83 GB of "other". What is c?
My Macbook Air has a total of 59.81 GB of storage space. What is it? ACE, which GB 43,83 are "other". What is c? How can I reduce it?
-
Circular edge IMAQ find 3: result rounded
Hello Can someone please explain to me the significance of the result "Roundness" of the circular edge IMAQ vi find 3? I know that the definitions of roundness like MCC, MIC and MZC.Is one of these related to this result? I get results ranging from 0
-
I have Volvo ptt program to make truck diagnostics it calls for professional vista or xp professional, I have vista Home premium which can do to upgrade?