Get number of records in the date range - IDE: PLSQL Developer
I want to count the number of records of members who register within a date range specified, based on effective and expiration dates and their "elg_code". I posted the SQL code for some examples of data. What I would like to see returned is in three columns of the counties where the eff_date date_exp members fall within the date range specified by the SQL and have an Elg_code of ' ' (space).So what I would like is all members with elg_code ' ' where he eff_dt and exp_dt range is April 2012, 2012 MAY & JUN 2012. Thus, according to data from the sample I posted, Mark, where his elg_code record is ' ', his eff_dt is 01/01/2011 and April 2012 (30/04/2012) is his exp_dt. Range of the brand statement may 2012, but not MAY or JUNE of 2012. Marty would tally for the APR and MAY because his eff_dt is before MAY 2012 and his exp to MAY 2012. etc...
According to the data below, the results should resemble:
APR MAY JUN
4-3-2
APR should have FRANK, MARK, MARTY, MARY.
MAY should have FRANK, MARTY, MARY
JUN should have FRANK and MARIE
NOAM and JOHN should not appear as his records with elg_code ' ' have no documents eff_dt and exp_dt which are April-June 2012.
So what I tried without success as it appears that I have a kind of Cartesian question (?), is:
Select count (m1.mbr_name) APR,
Count (m2.mbr_name) MAY,
Count (m3.mbr_name) JUN
mbr2 M1,
mbr2 m2,
mbr2 m3
"where m1.eff_dt < ' 01 - may - 2012"
"and m1.exp_dt > ' 01-Apr-2012.
and m1.elg_code = ' '
"and m2.eff_dt < 1 June 2012"
"and m2.exp_dt > ' 01 - may - 2012"
and m2.elg_code = ' '
"and m3.eff_dt < 1 July 2012"
"and m3.exp_dt > 1 June 2012"
and m3.elg_code = ' '
Here's the DML
Thanks for any help!
create table mbr2 (mbr_name varchar (10), varchar (1) grpid eff_dt date, date of exp_dt elg_code varchar (1))
Commit
insert into mbr2 values ('BRAND', 'A', to_date (January 1, 2011 ',' DD-MM-YYYY '), to_date (April 30, 2012 ',' DD-MM-YYYY '), ' ')
insert into mbr2 values ('BRAND', 'A', to_date (May 1, 2012 ',' DD-MM-YYYY '), to_date (31 December 2013 ',' DD-MM-YYYY '), 'C')
insert into mbr2 values ('MARTY', 'A', to_date (January 1, 2011 ',' DD-MM-YYYY '), to_date (May 31, 2012 ',' DD-MM-YYYY '), ' ')
insert into mbr2 values ('MARTY', 'A', to_date (June 1, 2012 ',' DD-MM-YYYY '), to_date (31 December 2013 ',' DD-MM-YYYY '), 'C')
insert into mbr2 values ('FRANK', 'B', to_date (January 1, 2011 ',' DD-MM-YYYY '), to_date (June 30, 2012 ',' DD-MM-YYYY '), ' ')
insert into mbr2 values ('FRANK', 'B', to_date (July 1, 2012 ',' DD-MM-YYYY '), to_date (31 December 2013 ',' DD-MM-YYYY ""), 'C')
insert into mbr2 values ('MARY', 'B', to_date (January 1, 2011 ',' DD-MM-YYYY '), to_date (June 30, 2012 ',' DD-MM-YYYY '), ' ')
insert into mbr2 values ('MARY', 'B', to_date (July 1, 2012 ',' DD-MM-YYYY '), to_date (31 December 2013 ',' DD-MM-YYYY ""), 'C')
insert into mbr2 values ('JOHN', 'C', to_date (January 1, 2011 ',' DD-MM-YYYY '), to_date (July 1, 2011 ',' DD-MM-YYYY '), ' ')
insert into mbr2 values ('JOHN', 'C', to_date (July 1, 2011 ',' DD-MM-YYYY '), to_date (1 January 2012 ',' DD-MM-YYYY ""), 'C')
insert into mbr2 values ("NOAM" 'd', to_date (July 1, 2012 ',' DD-MM-YYYY '), to_date (31 December 2013 ',' DD-MM-YYYY '), ' ' ")
Commit
This gives you a report for the current month and the two front. Column header must be adjusted ;-)
select
count(
case
when
eff_dt < add_months(trunc(sysdate,'MM'), -1)
and
exp_dt >= add_months(trunc(sysdate,'MM'), -2)
then 1
end) April
, count(
case
when
eff_dt < add_months(trunc(sysdate,'MM'), 0)
and
exp_dt >= add_months(trunc(sysdate,'MM'), -1)
then 1
end) May
, count(
case
when
eff_dt < add_months(trunc(sysdate,'MM'), 1)
and
exp_dt >= add_months(trunc(sysdate,'MM'), 0)
then 1
end) June
from mbr2
where
elg_code = ' '
and
eff_dt < add_months(trunc(sysdate,'MM'), 1)
and
exp_dt >= add_months(trunc(sysdate,'MM'), -2)
APRIL MAY JUNE
4 3 2
Tags: Database
Similar Questions
-
Find records in the date range
Hello
I have the following data
What is the best way to find all records in April 1, 2012 and April 30, 2012?with t as ( select 1234 prod_id, to_date('01-Jan-2008', 'dd-MON-yyyy') start_date, to_date('01-May-2012', 'dd-MON-yyyy') end_date from dual union select 4567 prod_id, to_date('01-Aug-2007', 'dd-MON-yyyy') start_date, to_date('01-Apr-2012', 'dd-MON-yyyy') end_date from dual union select 8910 prod_id, to_date('01-Jul-2006', 'dd-MON-yyyy') start_date, to_date('01-Mar-2012', 'dd-MON-yyyy') end_date from dual ) SELECT * FROM t
ConcerningAssuming that you are looking for cumulation ranges
SELECT * FROM t WHERE start_date BETWEEN date '2012-04-01' and date '2012-04-30' OR end_date BETWEEN date '2012-04-01' and date '2012-04-30' OR ( start_date < date '2012-04-01' AND end_date > date '2012-04-30' )
who produces the two lines expected
SQL> with t as ( 2 select 1234 prod_id, to_date('01-Jan-2008', 'dd-MON-yyyy') start_date, to_date('01-May-2012', 'dd-MON-yyyy') end_date 3 from dual union 4 select 4567 prod_id, to_date('01-Aug-2007', 'dd-MON-yyyy') start_date, to_date('01-Apr-2012', 'dd-MON-yyyy') end_date 5 from dual union 6 select 8910 prod_id, to_date('01-Jul-2006', 'dd-MON-yyyy') start_date, to_date('01-Mar-2012', 'dd-MON-yyyy') end_date 7 from dual 8 ) 9 SELECT * 10 FROM t 11 WHERE start_date BETWEEN date '2012-04-01' and date '2012-04-30' 12 OR end_date BETWEEN date '2012-04-01' and date '2012-04-30' 13 OR ( start_date < date '2012-04-01' 14 AND end_date > date '2012-04-30' ); PROD_ID START_DAT END_DATE ---------- --------- --------- 1234 01-JAN-08 01-MAY-12 4567 01-AUG-07 01-APR-12
Justin
-
SUM (case use this structure to get average values on the date range
I use:
Oracle SQL Developer (3.0.04) build hand - 04.34 Oracle Database 11 g Enterprise Edition 11.2.0.1.0 - 64 bit Production
How do we use the function sum with a structure of matter inside.
so I gave that looks like have an ID, date and value. I'm looking to get the day 7 medium for the date range of 04/01/2013 to 20/04/2013
* Please let me know if the table does not load.with t as ( select 1 ID_Key,to_date('4/1/2013','mm-dd-yyyy') date_val, 10 Value_num from dual union all select 1 ID_key,to_date('4/2/2013','mm-dd-yyyy'), 15 from dual union all select 1 ID_key,to_date('4/3/2013','mm-dd-yyyy'), 20 from dual union all select 1 ID_key,to_date('4/5/2013','mm-dd-yyyy'), 0 from dual union all select 1 ID_key,to_date('4/8/2013','mm-dd-yyyy'), 12 from dual union all select 1 ID_key,to_date('4/9/2013','mm-dd-yyyy'), 8 from dual union all select 1 ID_key,to_date('4/10/2013','mm-dd-yyyy'), 6 from dual union all select 1 ID_key,to_date('4/12/2013','mm-dd-yyyy'), 10 from dual union all select 1 ID_key,to_date('4/13/2013','mm-dd-yyyy'), 0 from dual union all select 1 ID_key,to_date('4/14/2013','mm-dd-yyyy'), 0 from dual union all select 1 ID_key,to_date('4/15/2013','mm-dd-yyyy'), 10 from dual union all select 1 ID_key,to_date('4/16/2013','mm-dd-yyyy'), 5 from dual union all select 1 ID_key,to_date('4/17/2013','mm-dd-yyyy'), 2 from dual union all select 1 ID_key,to_date('4/20/2013','mm-dd-yyyy'), 3 from dual union all select 2 ID_key,to_date('4/3/2013','mm-dd-yyyy'), 12 from dual union all select 2 ID_key,to_date('4/5/2013','mm-dd-yyyy'), 15 from dual union all select 2 ID_key,to_date('4/6/2013','mm-dd-yyyy'), 5 from dual union all select 2 ID_key,to_date('4/7/2013','mm-dd-yyyy'), 7 from dual union all select 2 ID_key,to_date('4/9/2013','mm-dd-yyyy'), 10 from dual union all select 2 ID_key,to_date('4/11/2013','mm-dd-yyyy'), 5 from dual union all select 2 ID_key,to_date('4/12/2013','mm-dd-yyyy'), 0 from dual union all select 2 ID_key,to_date('4/13/2013','mm-dd-yyyy'), 0 from dual union all select 2 ID_key,to_date('4/15/2013','mm-dd-yyyy'), 6 from dual union all select 2 ID_key,to_date('4/16/2013','mm-dd-yyyy'), 8 from dual union all select 2 ID_key,to_date('4/17/2013','mm-dd-yyyy'), 0 from dual union all select 2 ID_key,to_date('4/18/2013','mm-dd-yyyy'), 10 from dual union all select 2 ID_key,to_date('4/19/2013','mm-dd-yyyy'), 5 from dual )
I would get the average of 7 days, as long as there is date for the rank of enough previous dates, is it not, then it returns a null value.
the results should look like this
As you can see, there are gaps in the dates, the value is then processed by zeros for the 7Day_Avg and then ignored for the 7Day_Avg2 (not counted in the number of days on average do to no valu_num line)ID_Key date_val Value_num 7Day_Avg 7Day_Avg2 1 4/1/2013 10 null null 1 4/2/2013 15 null null 1 4/3/2013 20 null null 1 4/5/2013 0 null null 1 4/8/2013 12 6.71 11.75 1 4/9/2013 8 5.71 10.00 1 4/10/2013 6 3.71 6.50 1 4/12/2013 10 5.14 9.00 1 4/13/2013 0 5.14 7.20 1 4/14/2013 0 5.14 6.00 1 4/15/2013 10 4.86 5.67 1 4/16/2013 5 4.42 5.17 1 4/17/2013 2 3.85 4.50 1 4/20/2013 3 2.86 4.00 2 4/3/2013 12 null null 2 4/5/2013 15 null null 2 4/6/2013 5 null null 2 4/7/2013 7 5.57 9.75 2 4/9/2013 10 7.00 9.80 2 4/11/2013 5 6.00 8.40 2 4/12/2013 0 3.86 5.40 2 4/13/2013 0 3.14 4.40 2 4/15/2013 6 3.00 4.20 2 4/16/2013 8 2.71 3.80 2 4/17/2013 0 2.71 3.17 2 4/18/2013 10 3.43 4.00 2 4/19/2013 5 4.14 4.83
I tried something like this at first, but becomes "missing keyword" error
Should I have the structure outside the sum function?select t.*/, sum( case when date_val between :day2 - 6 and :day2 then value_num between date_val - 6 and date_val else null end ) as 7Day_avg form t
Any thoughts?
Published by: 1004407 on June 7, 2013 11:06Hello
If you want to calculate the average of the last 7 days, including the current day, then then RANGE should be 6 PRIOR, not 7.
Try this:
WITH got_min_date_val AS
(
SELECT id_key, date_val, value_num
MIN (date_val) compared to (AS min_date_val)
T
WHERE the date_val BETWEEN TO_DATE (April 1, 2013 ', "dd-mm-yyyy")
AND TO_DATE (April 20, 2013 ', "dd-mm-yyyy")
)
SELECT id_key, date_val, value_num
CASE
WHEN date_val > = min_date_val + 6
THEN SUM (value_num) OVER (PARTITION BY id_key
ORDER BY date_val
PRIOR TO TIER 6
)
/ 7
END AS avg_7_day
CASE
WHEN date_val > = min_date_val + 6
THEN AVG (value_num) OVER (PARTITION BY id_key
ORDER BY date_val
PRIOR TO TIER 6
)
END AS avg_7_day_2
OF got_min_date_val
ORDER BY id_key
date_val
;
Output:
ID_KEY DATE_VAL VALUE_NUM AVG_7_DAY AVG_7_DAY_2
---------- --------- ---------- ---------- ------------
1 1 APRIL 13 10
1 2 APRIL 13 15
1 APRIL 3, 13 20
1 5 APRIL 13 0
1 APRIL 8, 13 12 6.71 11.75
1 APRIL 9, 13 8 5.71 10.00
1 APRIL 10 13 6 3.71 6.50
1 12 APRIL 13 10 5.14 9,00
1 13 APRIL 13 0 5,14 7.20
1 14 APRIL 13 0 5,14 6.00
1 15 APRIL 13 10 4.86 5.67
1 16 APRIL 13 5 4.43 5.17
1 APRIL 17, 13 2 3.86 4.50
1 APRIL 20, 13 3 2.86 4.00
2 APRIL 3, 13 12
2 5 APRIL 13 15
2 6 APRIL 13 5
2 7 APRIL 13 7 5.57 9.75
2 9 APRIL 13 10 7,00 9.80
2 11 APRIL 13 5 6.00 8.40
2 12 APRIL 13 0 3.86 5.40
2 13 APRIL 13 0 3.14 4.40
2 15 APRIL 13 6 3.00 4.20
2 16 APRIL 13 8 2.71 3.80
2 17 APRIL 13 0 2.71 3.17
2 18 APRIL 13 10 3.43 4.00
2 APRIL 19, 13 5 4.14 4,83
Post edited by: FrankKulash
Sorry; I wanted to respond to OP, not Greg
-
Get the number of months between the dates.
Hi I need to get the exact number of months between the dates. So if I'm Sysdate - to_date(31-JAN-2011) I should get something like 8.4 months. Months_between function doesn't quite give me what I need. Is it possible that I can do this?876056 wrote:
Hi I need to get the exact number of months between the dates. So if I'm Sysdate - to_date(31-JAN-2011) I should get something like 8.4 months. Months_between function doesn't quite give me what I need. Is it possible that I can do this?I don't see how you get 8.4 months. The counted months are February, March, April, may, June, July, August and, as of today, 9 and change the days of September. Unless I forgot the lessons of counting, I learned in kndergarten is 7 full months and a bit.
Maybe you want something like:
SQL> select months_between(Sysdate, to_date('01-JAN-2011', 'dd-mon-yyyy')) mb, 2 (Sysdate - to_date('01-JAN-2011', 'dd-mon-yyyy'))/30 div30 3 from dual; MB DIV30 ---------- ---------- 8.27800142 8.38726813
If you get the form date a table, you can use trunc (date_column, 'mm') at the date of the first day of the month.
John
-
Get the date range min/max for consecutive events.
Hi all...
I am fairly new to the programming of the DB and is working on some examples I picked up a few places. The database version is 10g R2.
I would like to configure the data here and I can explain my requirement.
create table table_1 (product_id varchar2(25), region_id number, event_id number event_date date, event_status number(1)) /
Now the data because it is->
insert into table_1 values ('Prod-1',10, null, to_date('01-feb-2014','dd-mon-yyyy'),null) / insert into table_1 values('Prod-1',10, 1001, to_date('10-mar-2014','dd-mon-yyyy'), 1) / insert into table_1 values('Prod-1',10, 1001, to_date('20-mar-2014','dd-mon-yyyy'), 3) / insert into table_1 values('Prod-1',10,1002, to_date('01-apr-2014','dd-mon-yyyy'), 1) / insert into table_1 values('Prod-1',10, 1002, to_date('10-apr-2014','dd-mon-yyyy'), 3) / commit /
If the table now contains the following data
select * from table_1;
PRODUCT_ID REGION_ID EVENT ID EVENT_DAT EVENT_STATUS
------------------------- ---------- -------- --------- ------------
Prod-1 10 1 February 14
Prod-1 10 1001 10 March 14 1
Prod-1 10 1001 20 March 14 3
Prod-1 10 1002 1 April 14 1
Prod-1 10 1002 April 10, 14 3
Now, the condition is as follows:
Above is the dates start and end for the tests on a product in a given region. The event_status column indicates the dates of beginning and end. Event_status = 1, for the date of beginning and the event_status = 3 of the end date.
A new event is now coming from March 21 and ending on March 31.
The power required is a product identifier / region; If there are events that are ending and then count the days, for example, 1001 to end on March 20, but now the new event begins on 21 March... and the new event ends on 31 March and event 1002 begins 01 - Apr... and so on.
The output required for this is as follows:
PRODUCT_ID REGION_ID EVENT_MIN_DATE EVENT_MAX_DATE
------------------------- ---------- -------------- --------------
Prod-1 10 10 14 March 10 April 14
The output should give a product identifier / region, when events are immediately after the other, out the min date of beginning and end of max for all these manifestations of "back to back".
Now I wrote the code for this, but it goes into a purely 'loop' for application in PLSQL... But my head tells me that if the results are correct. the PLSQL implementation is not the most efficient and effective way.
Can someone help me to form the query? I tried to use functions analytical min/max but it gives me the start and end dates back even if my events are not "back to back" or previous/succeed each other... so my query result is not quite correct.
Am reading on the TYPE clause but would be grateful if someone could help me with this query... or any other form better to implement this event so PLSQL can be used. The database version is 10g R2.
Thank you
K
PS - The number of such events back to back is limited to 4 and the events could be created in any order. But if someone could help me with the scenario above; am sure I could make a request addressed to any change in the order. :-)
No this isn't a recursive with clause, perhaps the UNION ALL you got confused. She will work with 10g.
Since there are 2 tables that you said, the block all_data brings together only the lines of the two tables. table_1 start_date and end_date are built from the event_status (where the max in group by).
-
On the date ranges in search of oracle forms
I use service number, name, type person and date as search criteria in the custom form ranges.
When I enter the employee number, press the search button, I get the exact info in the block of result.
Result block contains
name of the employee, personid, emp numbed, org, start_date.
Similarly when I enter the employee number, dept, I get the correct values.
My question is, when I enter the date range. IAM unable to filter the data.
When I enter START_DATE between nvl(:BLOCKNAME.) Start_date, 1 January 1901 ') and nvl(:BLOCKNAME.) End_date, 31-DEC-4712') where block clause in the result. I had data for the range of dates also.
If I give condition in where clause results take so long when I search with the name of the employee, number, dept, person type.
If I have a query with the date, the persormance is good.
Think you know everything, how it prevents START_DATE between nvl(:BLOCKNAME.) Start_date, 1 January 1901 ') and nvl(:BLOCKNAME.) End_date, 31-DEC-4712') when look us with the employee's name, number, dept, person type.So leave the WHERE clause of your empty block and put something like the following in your search button:
IF :BLOCK.START_DATE IS NOT NULL OR :BLOCK.END_DATE IS NOT NULL THEN SET_BLOCK_PROPERTY('YOURBLOCK', ONETIME_WHERE, 'START_DATE between nvl(:BLOCKNAME.START_DATE,TO_DATE(''01-JAN-1901'', ''DD-MON-YYYY'')) and nvl(:BLOCKNAME.END_DATE,TO_DATE(''31-DEC-4712'', ''DD-MON-YYYY''))'); END IF;
-
Number of records in the repeat region
I have a page with a repeat region, which displays the records in Table1. Table1 is related to the Table2 (one to several, MS Access). I want to do is show the number of records in Table2 that are associated with a given row from Table1 shown in the repeat region.
Think of how many responses listed next to each message in this forum, because that's basically what I do. As it is now, I get the correct number of records for the first line of Table1 shown in the repeat region, but this number is indicated in each line. In other words, if the first record displayed from Table1 has 3 matching records in table 2, 3 number in each row in the repeat region.
I appreciate all the help that I'm a complete novice.
Maybe it is not very effective, but I bet you it will do what you want:
SELECT *.
FROM table1
ORDER BY name DESC
SELECT *.
FROM table2
Where tid = #table1.id #.
#table1.name #.
#table1.client #.
#table1.products #.
$# numberformat (table1.income, '999,999.99') #.
#table1.frequency #.
#table2. RecordCount #.
-
AT100 - impossible to get my calendar to store the dates of the annual events
Hello.
Cannot get my calendar to store the dates of the annual events birthdays for example.
Option gives (annual 27 July 2012), but would it be fair (27 July each year). No way to remove year so presumably won't wait that 2012 will come along! All the other choices for weekly and monthly do not specify the year.Annual option should I think be the same. Tried to enter next year, but comes with (year July 27, 2013) so it looks that have to enter every year which is a pain. Someone at - it ideas.
It's my first post he hopes in the right place.
Thank you
Hello
Why you n t try another Android app like for example calendar of birthday or anniversary Assistant
-
How can I recover only odd or even number of records in the table?
Hello
How can I recover only odd or even number of records in the table?
Thank you
Hi IndiMinds,
Alternatively, you can use in queries. :
Odd:
SELECT *.
Of
(SELECT rownum row_count, id, name FROM bigemp
)
WHERE mod (row_count, 2) = 1;
Still:
SELECT *.
Of
(SELECT rownum row_count, id, name FROM bigemp
)
WHERE mod (row_count, 2) = 0;
Thank you
-
Number of records in the top of the report page
Hello
IAM trying to display the total number of records in the upper part (I have not posted in the footer) of my report classic/IR using the below javascript but somehow, is not working. I appreciate any suggestion on this subject,
< Input id = "numrowsGet" type = "hidden" value = "" #TOTAL_ROWS # "/ >"
< script type = "text/javascript" >
If (document.getElementById("numrowsGet").value is "0")
assign value to {0;
}
on the other
{
document.getElementById("P11_RECORD_COUNTS").value = document.getElementById ("numrowsGet") .value;
}
< /script >
.. .THXOnce the page is rendered, then you see the numrowsGet article and its value in the view source?
Why not try this adding in the header of the region as
Row Count: #TOTAL_ROWS#
http://docs.Oracle.com/CD/E23903_01/doc/doc.41/e21674/ui_region.htm#HTMDB25572
-
Hello
I need where there are quick dashboard that selects the date, second interval column consists of >, <, = and the third column is seen number (in days)
for example, the user enters: 12 18, 2015, <, 9. here I report who should get the data (records) 9 days earlier from the date specified (from 12/10/2015 to 18/12/2015).
Similarly for > =.
How to get there. I'm unable to write the script and I use the column date in my report. Ask that you please advice how to solve this problem.
Please find below the screen for reference.
Kind regards
Chandra Khalil.
Hello
Not exactly why you're doing the report this way. If the user must select, basically, a period of dates, would not easier to have a column of data with a between the operator and the user would select 2 dates (upper and lower intervals)?
In any case, it is possible to achieve what you want to do as a result of your approach. You could use when's CASE and have a more complex filter, but I think that it is better to have a few nested filters where you use the range operator to determine what date filter that you use (see image below)
Some notes about my filter. First of all, I am making the assumption that the operator is "=", then the number of days specified must be ignored and you just return the data for the selected date. You will notice also that in my filters, I got a cast so far, but if you are using a column that is already a timestamp so it won't be necessary.
This should achieve what you want.
See you soon,.
Pedro
-
How to get the different records for the date max.
Hi all
Here is the sample sql for sample table and data.
Create table student (dept_id number(10), first_name varchar2(10),last_name varchar2(10),join_date date,years_attended number(10));
insert into student values (1,'Ann','Coleman',to_date('3/7/1917','MM/DD/YYYY'),4);
insert into student values (1,'Ann','Coleman',to_date('3/7/1916','MM/DD/YYYY'),5);
insert into student values (2,'Rock','Star',to_date('1/1/1920','MM/DD/YYYY'),5);
insert into student values (2,'Rock','Star',to_date('1/1/1921','MM/DD/YYYY'),6);
insert into student values (3,'Jack','Smith',to_date('7/1/1900','MM/DD/YYYY'),3);insert into student values (3,'Jack','Smith',to_date('7/1/1901','MM/DD/YYYY'),4);
commit;
I need to get maximum date records when the name and dep_id corresponds to. I wrote the query below and it becomes the expected result, but I'm not sure it's quite effective.
SELECT s.dept_id, s.first_name,s.years_attended
FROM (SELECT dept, MAX (join_date) join_date
STUDENT GROUP BY dept_id) x
Student JOIN s ON x.dept_id = s.dept_id AND x.join_date = s.join_date;
This above query returns records like below, and this is the goal.
DEPT_ID NAME YEARS_ATTENDED
1 Ann 4
2 Rock 6
3 Jack 4
Can you please let me know the query SQL I wrote is effective or not? This sample table may have less data, but I'm dealing with millions of records.
Hello
Thanks for posting CREATE TABLE and INSERT statement. This really helps.
Here's a solution. I also added a name that seems logical. In which case you can delete:
Select dept_id, first_name, last_name
, max (years_attended) Dungeon years_attended (last dense_rank order by join_date)
the student
Group of dept_id, first_name, last_name;
DEPT_ID FIRST_NAME LAST_NAME YEARS_ATTENDED
---------- ---------- ---------- --------------
1 Ann Coleman 4
Rock 2 star 6
3 Jack Smith 4
Kind regards.
Alberto
-
Recording of Split on the date range
Hello!
I use oracle 10g Standard Edition. I have following the set of records in a table T1 maintain periods of leave in it.
The number of records by empno varies and can be from 1 to n. I want the output like below:empno startdt enddt 12 15/12/2009 28/12/2009 12 10/01/2010 25/03/2010 12 30/03/2010 18/04/2010 34 05/02/2010 20/02/2010 34 01/03/2010 15/03/2010 86 11/11/2009 29/07/2010 . . .
I tried searching the forum but could not find a relevant answer. Any help is appreciated.empno startdt enddt 12 15/12/2009 28/12/2009 12 10/01/2010 31/01/2010 12 01/02/2010 28/02/2010 12 01/03/2010 25/03/2010 12 30/03/2010 31/03/2010 12 01/04/2010 18/04/2010 34 05/02/2010 20/02/2010 34 01/03/2010 15/03/2010 86 11/11/2009 30/11/2009 86 01/12/2009 31/12/2009 86 01/01/2010 31/01/2010 86 01/02/2010 26/02/2010 . . .
UMESHHello
umesh19 wrote:
Hello!I use oracle 10g Standard Edition. I have following the set of records in a table T1 maintain periods of leave in it.
What version (e.g. 10.2.0.3.0)? The version is more important in you giving a good response than that of the edition.
The solution below works in Oracle 9 (and), but could be modified to work in Oracle 8.1.empno startdt enddt 12 15/12/2009 28/12/2009 12 10/01/2010 25/03/2010 12 30/03/2010 18/04/2010 34 05/02/2010 20/02/2010 34 01/03/2010 15/03/2010 86 11/11/2009 29/07/2010 . . .
Always post your sample data in a form that people can use to recreate the problem and test their ideas.
For example:CREATE TABLE table_x ( empno NUMBER (4) , startdt DATE , enddt DATE ); INSERT INTO table_x (empno, startdt, enddt) VALUES (12, TO_DATE ('15/12/2009', 'DD/MM/YYYY'), TO_DATE ('28/12/2009', 'DD/MM/YYYY')); INSERT INTO table_x (empno, startdt, enddt) VALUES (12, TO_DATE ('10/01/2010', 'DD/MM/YYYY'), TO_DATE ('25/03/2010', 'DD/MM/YYYY')); INSERT INTO table_x (empno, startdt, enddt) VALUES (12, TO_DATE ('30/03/2010', 'DD/MM/YYYY'), TO_DATE ('18/04/2010', 'DD/MM/YYYY')); INSERT INTO table_x (empno, startdt, enddt) VALUES (34, TO_DATE ('05/02/2009', 'DD/MM/YYYY'), TO_DATE ('20/02/2010', 'DD/MM/YYYY')); INSERT INTO table_x (empno, startdt, enddt) VALUES (34, TO_DATE ('01/03/2010', 'DD/MM/YYYY'), TO_DATE ('15/03/2010', 'DD/MM/YYYY')); INSERT INTO table_x (empno, startdt, enddt) VALUES (86, TO_DATE ('11/11/2009', 'DD/MM/YYYY'), TO_DATE ('29/07/2010', 'DD/MM/YYYY')); COMMIT;
The number of records by empno varies and can be from 1 to n. I want the output like below:
empno startdt enddt 12 15/12/2009 28/12/2009 12 10/01/2010 31/01/2010 12 01/02/2010 28/02/2010 12 01/03/2010 25/03/2010 12 30/03/2010 31/03/2010 12 01/04/2010 18/04/2010 34 05/02/2010 20/02/2010 34 01/03/2010 15/03/2010 86 11/11/2009 30/11/2009 86 01/12/2009 31/12/2009 86 01/01/2010 31/01/2010 86 01/02/2010 26/02/2010 . . .
Explain how you get the desired results based on data you've posted.
I tried searching the forum but could not find a relevant answer. Any help is appreciated.
What you're looking for? Which could give us some clues as to what you want.
What have you tried yourself?If you would like each input line to split into one or more production lines, each for a single calendar month, then follow these steps:
WITH all_months AS ( SELECT ADD_MONTHS (firstdt, LEVEL - 1) AS startdt , ADD_MONTHS (firstdt, LEVEL ) - 1 AS enddt FROM ( SELECT TRUNC ( MIN (startdt) , 'MONTH' ) AS firstdt , MAX (enddt) AS lastdt FROM table_x ) CONNECT BY LEVEL <= CEIL ( MONTHS_BETWEEN ( lastdt , firstdt ) ) ) SELECT empno , GREATEST (t.startdt, m.startdt) AS startdt , LEAST (t.enddt, m.enddt ) AS enddt FROM table_x t JOIN all_months m ON t.startdt <= m.enddt AND m.startdt <= t.enddt ORDER BY empno , startdt ;
-
Difference in the number of records for the same date - 11 GR 2
Guy - 11 GR on Windows2005 2, 64-bit.
BILLING_RECORD_KPN_ESP - is a monthly partitioned table.
BILLING_RECORD_IDX #DATE - is a local index on "charge_date" in the table above.
SQL > select / * + index (BILLING_RECORD_KPN_ESP BILLING_RECORD_IDX #DATE) * /.
2 (trunc (CHARGE_DATE)) CHARGE_DATE;
3 count (1) Record_count
4. IN "RATOR_CDR". "" BILLING_RECORD_KPN_ESP ".
where the 5 CHARGE_DATE = January 20, 2013.
Group 6 by trunc (CHARGE_DATE)
5 m
CHARGE_DATE RECORD_COUNT
------------------ ------------
2401 20 January 13-> > some records here.
-> > Here I can see only '2041' records for Jan/20. But in the query below, it shows "192610" for the same date.
Why is this difference in the number of records?
SQL > select / * + index (BILLING_RECORD_KPN_ESP BILLING_RECORD_IDX #DATE) * /.
(trunc (CHARGE_DATE)) CHARGE_DATE,
2 count (1) Record_count
3. FOR "RATOR_CDR." "" BILLING_RECORD_KPN_ESP ".
"4 where CHARGE_DATE > 20 January 2013."
Group of 5 by trunc (CHARGE_DATE)
6 order by trunc (CHARGE_DATE)
5 m
CHARGE_DATE RECORD_COUNT
------------------ ------------
192610 20 January 13-> > more records here
JANUARY 21, 13 463067
JANUARY 22, 13 520041
23 JANUARY 13 451212
JANUARY 24, 13 463273
JANUARY 25, 13 403276
JANUARY 26, 13 112077
27 JANUARY 13 10478
28 JANUARY 13 39158
Thank you!Because in the second example you also select rows that have a nonzero component.
The first example selects only rows that are 00:00:00
(by the way, you should ask questions like this in the forum SQL)
-
Impossible to get Min, Max and median of the values in the date range values
Hello
I had a requirement as to show the data of each charge group of wise men as '< 100' ' 100-199 "" 200-299 "" 300-399 "400-499, 500-599 600-699 700-799 800-899 900-999 > = 1000 '"»
With the query be able to get the count between the beach and the total below. But impossible to get the Min and Max values for this range. For example if the County < 100 is 3 then in these 3, the lowest value is need to display in the min. Idem for Max column also.
In the light of the median value on these values.
Thanks in advance.
Requirement is as below:
State < 100 100-199, 200-299 300-399 400-499, 500-599 600-699 700-799 800-899 900-999 > = 1000 Min Total median Max
AK 1 2 0 4 1 4 4 35 35 4 1 $25 $85 850 $1,200
AL 0 0 2 27 10 17 35 2 2 35 0 $103 100-$1 500 750
* "QUERY ' * '"
WITH t AS
(SELECT 'AL' State, 12 DUAL FROM VALUE
UNION ALL
SELECT 'AL' State, 67 FROM DUAL VALUE
UNION ALL
SELECT 'AL' State, 23 FROM DUAL VALUE
UNION ALL
SELECT 'AL' State, 12 DUAL FROM VALUE
UNION ALL
SELECT 'AL' State, 12 DUAL FROM VALUE
UNION ALL
SELECT 'AL' State, 78 FROM DUAL VALUE
UNION ALL
SELECT 'AL' State, 34 FROM DUAL VALUE
UNION ALL
SELECT 'AL' State, 4 DUAL FROM VALUE
UNION ALL
SELECT 'AL' State, 12 DUAL FROM VALUE
UNION ALL
SELECT 'AL' State, 15 VALUE FROM DUAL
UNION ALL
SELECT "AZ" State, FROM DUAL VALUE 6
UNION ALL
SELECT "AZ" State, 123 FROM DUAL VALUE
UNION ALL
SELECT "AZ" State, 123 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 23 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 120 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 456 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 11 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 24 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 34 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 87 DUAL FROM VALUE
UNION ALL
SELECT 'MY' State, 23 FROM DUAL VALUE
UNION ALL
SELECT 'MY' State, 234 DUAL FROM VALUE
UNION ALL
SELECT 'MY' State, 789 FROM DUAL VALUE
UNION ALL
SELECT "HD" State, VALUE FROM DUAL 54321).
-End of test data
AS T1
(SELECT State,
NVL (COUNT (DECODE (VALUE, 0, 0)), 0) '< 100 ',.
NVL (COUNT (DECODE (VALUE, 1, 1)), 0) '100-199.
NVL (COUNT (DECODE (VALUE, 2, 2)), 0) '200-299.
NVL (COUNT (DECODE (VALUE, 3, 3)), 0) '300-399.
NVL (COUNT (DECODE (VALUE, 4, 4)), 0) '400-499.
NVL (COUNT (DECODE (VALUE, 5, 5)), 0) '500-599,'
NVL (COUNT (DECODE (VALUE, 6, 6)), 0) '600-699.
NVL (COUNT (DECODE (VALUE, 7, 7)), 0) '700-799.
NVL (COUNT (DECODE (VALUE, 8, 8)), 0) '800-899.
NVL (COUNT (DECODE (VALUE, 9, 9)), 0) '900-999. "
NVL (COUNT (DECODE (VALUE, 10, 10)), 0) ' > = 1000.
(SELECT STATE,
CASE
WHAT VALUE < 100 THEN 0
WHAT A VALUE BETWEEN 100 AND 199 THEN 1
WHAT VALUE BETWEEN 200 AND 299, THEN 2
WHAT VALUE BETWEEN 300 AND 399 THEN 3
WHAT VALUE BETWEEN 400 AND 499 THEN 4
WHAT VALUE BETWEEN 500 AND 599 5 THEN
WHAT VALUE BETWEEN 600 AND 699 6 THEN
WHAT VALUE BETWEEN 700 AND 799 THEN 7
WHAT VALUE BETWEEN 800 AND 899 8 THEN
WHAT VALUE FROM 900 TO 999 9 THEN
WHAT VALUE > = 10 THEN 1000
END
VALUE
T)
GROUP BY State)
SELECTION STATE,
"< 100."
"100-199.
"200 299",
"300-399.
"400-499.
'500-599,'
"600-699.
"700-799.
"800-899.
"900-999."
"> = 1000."
'< 100 '.
+ "100-199.
+ "200-299.
+ '300-399.
+ '400-499.
+ "500-599.
+ '600-699.
+ "700-799.
+ "800-899.
+ '900-999 ".
+ ' > = 1000.
in total,.
less ("< 100",)
"100-199.
"200 299",
"300-399.
"400-499.
'500-599,'
"600-699.
"700-799.
"800-899.
"900-999."
("> = 1000 ') min_val,.
largest ("< 100",)
"100-199.
"200 299",
"300-399.
"400-499.
'500-599,'
"600-699.
"700-799.
"800-899.
"900-999."
("> = 1000 ') max_val
FROM t1
/Why not keep it simple?
WITH t AS (SELECT 'AL' state, 12 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 67 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 23 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 12 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 12 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 78 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 34 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 4 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 12 VALUE FROM DUAL UNION ALL SELECT 'AL' state, 15 VALUE FROM DUAL UNION ALL SELECT 'AZ' state, 6 VALUE FROM DUAL UNION ALL SELECT 'AZ' state, 123 VALUE FROM DUAL UNION ALL SELECT 'AZ' state, 123 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 23 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 120 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 456 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 11 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 24 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 34 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 87 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 23 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 234 VALUE FROM DUAL UNION ALL SELECT 'MA' state, 789 VALUE FROM DUAL UNION ALL SELECT 'MH' state, 54321 VALUE FROM DUAL) SELECT state , NVL( COUNT( case when VALUE < 100 then 0 end ), 0 ) "<100" , NVL( COUNT( case when VALUE between 100 and 199 then 0 end ), 0 ) "100-199" , NVL( COUNT( case when VALUE between 200 and 299 then 0 end ), 0 ) "200-299" , NVL( COUNT( case when VALUE between 300 and 399 then 0 end ), 0 ) "300-399" , NVL( COUNT( case when VALUE between 400 and 499 then 0 end ), 0 ) "400-499" , NVL( COUNT( case when VALUE between 500 and 599 then 0 end ), 0 ) "500-599" , NVL( COUNT( case when VALUE between 600 and 699 then 0 end ), 0 ) "600-699" , NVL( COUNT( case when VALUE between 700 and 799 then 0 end ), 0 ) "700-799" , NVL( COUNT( case when VALUE between 800 and 899 then 0 end ), 0 ) "800-899" , NVL( COUNT( case when VALUE between 900 and 999 then 0 end ), 0 ) "900-999" , NVL( COUNT( case when VALUE >= 1000 then 0 end ), 0 ) ">=100" , count( value ) "total" , min( VALUE ) "min" , max( VALUE ) "max" , avg( VALUE ) "avg" , median( value ) "median" from t group by state
Maybe you are looking for
-
4600 pro: Win XP Pro SP1 crashes in
Hi, I have been out of the computer game for a little more than 10 years, so I'm a little rusty, I wonder if anyone can help. I recently bought a Satellite Pro 4600 and was very happy with it, until a week ago it started crashing within 5 minutes whe
-
How to hide a control and change a background image at the same TIME
Hello I made a slideshow on LabView VI, who also monitors data in real time to a data logger. The program works like this: -. Start/Page 1:-bottom pane displays a nice screenshot. A set of indicators displays the data in real time. Switches to Page 2
-
How to share the Win32 Applications created by Microsoft Visual Studio?
I have a Win32 Application that I created with Microsoft Visual Studio and I want to share with computers that do not have visual studio. How would I do that? I just want to share the application and no other files.
-
Fix for common problems associated with Windows Update repeatedly trying to solve the same problem
When the machine running Windows Update on a Windows XP Professional (SP3 with IE8), the information for the download of the ActiveX bar does not appear. Tried to run the Fix for "Common problems with Windows Update" and it says "Windows Update compo
-
My drive hard keeps opening on its own and remains open.
As soon as I turn my computer on my hard drive opens and it does not close. If I close it manually it just opens again. I try to pop in a disc and it is not even trying to read, he appears just open back. any suggestions?