All months in date range plus running count
Oracle 11g
Hello all,
Having trouble getting the following query to return proper results. Have a table with a MEMBERNO, BUSINESS_LINE, ELIGIBILITY_START_DATE, ELIGIBILITY_END_DATE.
MEMBERNO is not unique
BUSINESS_LINE is not either
Start and end date are periods of time where: MEMBERNO&BUSINESS_LINE have changed
I need to list the member number, business_line, and each month that falls within the date range beginning with eligibility_start_date & eligibility_end_date, as well as a running count of the total in that span.
Eg.
member, business_line, month, year, count
1234, bus1, 01, 2001, 1
1234, bus1, 02, 2001, 2
1234, bus1, 03, 2001, 3
Here is my SQL, it is not sequencing the months dates correctly and I can not figure out why. Any help is very appreciated:
SELECT memberno,
business_line,
TO_CHAR (ADD_MONTHS (start_date, LEVEL - 1), 'MM') as MONTH,
TO_CHAR (ADD_MONTHS (start_date, LEVEL - 1), 'YYYY') as YEAR,
ROW_NUMBER () OVER (PARTITION BY key1 ORDER BY start_date ASC) as MEMBER_MONTH_COUNT
FROM (SELECT memberno,
business_line,
eligibility_start_date as start_date,
eligibility_end_date as end_date,
member_nbr || business_line as key1
FROM eligibility)
CONNECT BY LEVEL <=
MONTHS_BETWEEN (TRUNC (END_DATE, 'MM'),
TRUNC (START_date, 'MM'))
+ 1;
Edited by: 935047 on Jul 25, 2012 5:58 AM
Edited by: 935047 on Jul 25, 2012 6:18 AM
935047 wrote:
I need to list the member number, business_line, and each month that falls within the date range beginning with eligibility_start_date & eligibility_end_date, as well as a running count of the total in that span.
Eg.
member, business_line, month, year, count
1234, bus1, 01, 2001, 1
1234, bus1, 02, 2001, 2
1234, bus1, 03, 2001, 3I could not understand what the Running Count mean. Hence, I used Row_Number (Same as you did).
Below query might match yours.
with data (memb_no, bus_line, st_date, end_date) as
select 1234, 'bus1', to_date('01-01-2001', 'MM-DD-YYYY'), to_date('06-30-2001', 'MM-DD-YYYY') from dual
union all
select 1234, 'bus1', to_date('07-01-2001', 'MM-DD-YYYY'), to_date('07-30-2002', 'MM-DD-YYYY') from dual
min_max as
select memb_no, bus_line, min(st_date) st_date, max(end_date) end_date
from data
group by memb_no, bus_line
lvl as
select level l
from dual
connect by level <= (select max(round(months_between(end_date, st_date))) from min_max)
select memb_no,
bus_line,
to_char(add_months(st_date, l - 1), 'MM') months,
to_char(add_months(st_date, l - 1), 'YYYY') Year,
row_number() over (partition by memb_no, bus_line order by st_date) cnt
from min_max cross join lvl
order by year, months;
----OUTPUT------------------------
MEMB_NO BUS_LINE MONTHS YEAR CNT
1234 bus1 01 2001 1
1234 bus1 02 2001 19
1234 bus1 03 2001 3
1234 bus1 04 2001 4
1234 bus1 05 2001 5
1234 bus1 06 2001 6
1234 bus1 07 2001 7
1234 bus1 08 2001 8
1234 bus1 09 2001 9
1234 bus1 10 2001 10
1234 bus1 11 2001 11
1234 bus1 12 2001 12
1234 bus1 01 2002 13
1234 bus1 02 2002 14
1234 bus1 03 2002 15
1234 bus1 04 2002 16
1234 bus1 05 2002 17
1234 bus1 06 2002 18
1234 bus1 07 2002 2
19 rows selected
Similar Messages
-
Grouping by month within date range
Hi,
I have a date range which I like to group by months, but not from the 1st to last date of the calendar month but by given start date. For example the table below represents the date range with some values in a table. So the
first month range in the group should be from 06/06/2013 to 08/07/2013. The 06/07/2013 is absent because it's Saturday. In fact the data is populated with working days only.
Table below shows what I really would like to get:
There is a simple calculation for each month anniversary date, if the value from the start date, 06/06/2013, here 12 is less than the next month anniversary date, 08/07/2013, which is 9, then it should ignore it. And only show the value from the month
who's anniversary date value is less than the first date value, 12. So the example above shows value 14 for the second month's anniversary date, 06/08/2013. If the value has been found in any month the show, and ignore the rest.
I am just interested if there are any and report which number of month and the value.
I can implement this in procedural way, such as in VBA, but because the above looped many times it will take hours to run the process. I have tried in SQL Server, but first problem I came up with is that I can't group by month from the given start
date.
Is this possible to do in T-SQL ?I'm not sure I entirely understand your question. So I'll break the answer into two parts.
First, as I understand it, you are looking for a way to group data by months, but not all dates in June, 2013 as one group, all dates in July, 2013 as the next group, etc. Instead you want all dates from June 6, 2013 to July 5, 2013 as the first month,
all dates from July 6, 2013 to August 5 as the second month etc. The way to do that is use
DATEDIFF(month, '20130606', <your date column>) + CASE WHEN DAY(<your date column>) < 6 THEN -1 ELSE 0 END
So you can assign the month number you want to every row when you select from your table by
SELECT DATE, VALUE, DATEDIFF(month, '20130606', DATE) + CASE WHEN DAY(DATE) < 6 THEN -1 ELSE 0 END AS MonthNumber
FROM <your table name>
Now as I understand it (but am not sure), you want the first value in each "month" (as you are defining "month" which is greater than the value contained in your start date and if no value in a particular month is greater than that value,
you don't want any row for that month. For that you could do something like
Declare @StartDate date;
Declare @StartValue int;
Set @StartDate = '20130606';
Select @StartValue = Value From <your table name>
Where Date = @StartDate;
;With cteMonthNumber As
(SELECT DATE, VALUE, DATEDIFF(month, @StartDate, DATE) + CASE WHEN DAY(DATE) < DAY(@StartDate) THEN -1 ELSE 0 END AS MonthNumber
FROM <your table name>
WHERE DATE > @Date AND VALUE > @Value),
cteOrdered As
(Select DATE, VALUE, MonthNumber,
ROW_NUMBER() OVER (PARTITION BY MonthNumber ORDER BY DATE) As rn
From cteMonthNumber)
Select MonthNumber, Value As KickOutValue
From cteOrdered
Where rn = 1;
Tom
P.S. Notice that I wrote my date literal as YYYYMMDD. This is a good idea in SQL Server since '20130806" will always be interpreted as August 6. But depending on the settings on your server and/or client '06/08/2013' might be dd/mm/yyyy
and so be August 6, but it might be interpreted as mm/dd/yyyy and be June 8.
It is also a good idea to express date literals in this forum as yyyymmdd. There are a lot of us on the forum from the United States and we think 06/08/2013 is June 6 and a lot of us from other places who think 06/08/2013 is Aug 8. If you give
your dates as yyyymmdd we don't have to try to figure out what format you are using. -
Different totals based on dates (month vs date range)
I know I've seen this before, but I can't find the thread. I have a column in a fact table for price. When I add price and the month (eg April 2012) from the time dimension, it totals one number, but when I remove the month, and add a date range (4/1/2012 - 4/30/2012) it totals another (lower value). I can't find the solution I found last time.
Anyone else run into this? and solve it?RiZapata wrote:
I know I've seen this before, but I can't find the thread. I have a column in a fact table for price. When I add price and the month (eg April 2012) from the time dimension, it totals one number, but when I remove the month, and add a date range (4/1/2012 - 4/30/2012) it totals another (lower value). I can't find the solution I found last time.
Anyone else run into this? and solve it?The solution is to understand what OBIEE is doing. If the totals differ, then the definitions are different. You need to troubleshoot to see what the difference is. When you add the month column and the date (so you can see every date) and add the total then, which total does it tie to? Is there rounding? How off are the two totals? That often may give a clue as to what is missing in the lower total. -
Sum a by previous month's date range
Hello,
i am having trouble creating a formula for the following.
sum(,,) in minimum(Lastfullmonth)-1 and in maximum(Lastfullmonth)-1
can anyone help me out with this formula?
thanks
david
Edited by: davitali on Nov 22, 2011 2:54 AMHi David,
If you're using CR 2008, you can make use of the advanced cross-tab features available.
Follow the steps below to create a calculated member (a.k.a row or column):
1) In the Preview mode, right-click the column header that shows the last month. Considering September as the last month, Right-click the column header > Calculated Member > Insert Column
2) A column with a blank header and zero values comes up after September
3) Right-click the blank column header and select Calculated Member > Edit Header Formula. Type this in the editor:
"Difference"
4) Now, the blank column header displays the text 'Difference'
5) Next, right-click one of the zero values in this column and select Calculated Member > Edit Calculation Formula. Type this formula:
Gridvalueat(currentrowindex,currentcolumnindex-1,currentsummaryindex) - Gridvalueat(currentrowindex,currentcolumnindex-2,currentsummaryindex)
The Crosstab would now display the difference between the two months.
To change the font color of the value try this:
1) Right-click one of the values from the column and select Format Field > Font tab > click the formula button beside Color and use this formula:
if CurrentColumnIndex = 1 then
if currentfieldvalue < Gridvalueat(currentrowindex,currentcolumnindex-1,currentsummaryindex)
then crRed else crGreen
else
crNoColor
If you have not suppressed the Row Totals or if the Row Totals is the 1st Column, then the formula might color the column beside Total (October) instead of the last (September). Just change the value of CurrentColumnIndex in the above formula to 2.
Let me know how this goes!
-Abhilash -
Count records for a specific date range
Hi,
I am using BI Publisher with Siebel 8.1.1.1. I have a monthly report where I want to count the number of service requests entered per customer per month.
I am using the following expression that works for one month:
<?count(ssServiceRequest[ssSeverity[.='1-Critical'] and xdoxslt:date_diff('d',psfn:getCanonicalDate(ssCreated), xdoxslt:current_date($_XDOLOCALE,$_XDOTIMEZONE), $_XDOLOCALE, $_XDOTIMEZONE) <=28])?>
However, I want to be able to specify a specific date range for my count, i.e ssCreated >= '01/02/2011' and <= '28/02/2011', as opposed to count for the past 28 days. Can you please advise what syntax I can use to specify a date range?
Any help will be greatly appreciated!
ClaireHello,
Many many thanks for your reply.
I'm doing as you suggest and am using the date_diff to the end of the period in question. I've tried the following to get the number of Service Requests with Severity 1-Critical, created in Jan 2011, feb 2011:
<?count(ssServiceRequest[ssSeverity[.='1-Critical'] and xdoxslt:date_diff('d', (psfn:getCanonicalDate(ssCreated), $_XDOLOCALE, $_XDOTIMEZONE), '2011-01-31', $_XDOLOCALE, $_XDOTIMEZONE) <=31])?>
<?count(ssServiceRequest[ssSeverity[.='1-Critical'] and xdoxslt:date_diff('d', (psfn:getCanonicalDate(ssCreated), $_XDOLOCALE, $_XDOTIMEZONE), '2011-02-28', $_XDOLOCALE, $_XDOTIMEZONE) <=28])?>
In my xml sample data that I'm using I have 2 service requests that mean the criteria for Jan, yet when I run my report I'm getting '0'. The problem seems to be with my end of period date '2011-01-31'. I've also tried with both start and end period date (using a negative value as per your suggestion), but I still get '0':
<?count(ssServiceRequest[ssSeverity[.='1-Critical'] and xdoxslt:date_diff('d', (psfn:getCanonicalDate(ssCreated), $_XDOLOCALE, $_XDOTIMEZONE), '2011-01-01', $_XDOLOCALE, $_XDOTIMEZONE) <=-31 and xdoxslt:date_diff('d', (psfn:getCanonicalDate(ssCreated), $_XDOLOCALE, $_XDOTIMEZONE), '2011-01-31', $_XDOLOCALE, $_XDOTIMEZONE) <=31])?>
Many thanks,
Claire -
Count days in a month for a date range
i am trying to find no. of days between 2 Date Ranges for a list of Ids. i used the logic in the below link:
count days of the month
My query is giving duplicates since, I have list of Ids.
Doctor_ID Patient_ID ARRIVE_DT_TM DISCH_DT_TM
755722 42972229 10/18/2012 7:50 3/14/2013 20:45
763305 42972232 1/7/2013 20:27 3/15/2013 19:15
25391509 42972298 2/4/2013 22:45 3/8/2013 22:03
746779 42972331 1/4/2013 23:00 3/26/2013 21:50
763305 42972338 3/4/2013 22:19 3/6/2013 19:35
763305 42972411 11/4/2013 22:32 3/29/2013 17:30
I am looking for query to give me for Patient_ID = 42972229
MONTH COUNT_DAYS
201210 14
201211 30
201212 31
201301 31
201302 28
201303 14
I am running the following code and it loops through the months and gives duplicates when I remove where Patient_id IN (42972229)
select
Doctor_ID
, Patient_ID
, AR_DTTM
, DSC_DTTM
, TO_CHAR(ADD_MONTHS(TRUNC(date1, 'MONTH'), LEVEL - 1), 'YYYY MM') MONTHS_BET
, (LEAST(date2, ADD_MONTHS(TRUNC(date1, 'MONTH') - 1, LEVEL)) - GREATEST(date1, ADD_MONTHS(TRUNC(date1, 'MONTH'), LEVEL - 1)))+ 1 AS DAYSCOUNT
from (select
Doctor_ID
, Patient_ID
, ARRIVE_DT_TM AR_DTTM
, DISCH_DT_TM DSC_DTTM
,TRUNC(ARRIVE_DT_TM,'DDD') AS date1
,TRUNC(DISCH_DT_TM,'DDD') AS date2
from temp where Patient_id IN (42972229)
CONNECT BY LEVEL <= MONTHS_BETWEEN(TRUNC(date2, 'MONTH'), TRUNC(date1, 'MONTH')) + 1
Please help!Hi,
ASTRA_007 wrote:
Results I would like to see are:
Doctor_ID Patient_ID ARRIVE_DT_TM DISCH_DT_TM Month CountofDays
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2012 10 14
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2012 11 30
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2012 12 31
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2013 01 31
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2013 02 28
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2013 03 14
763305 42972232 1/7/2013 20:27 3/15/2013 19:15 2013 01 25
763305 42972232 1/7/2013 20:27 3/15/2013 19:15 2013 02 28
763305 42972232 1/7/2013 20:27 3/15/2013 19:15 2013 03 15
and so on...So each row represents a patient-month, and you want to display several columns from the temp table on each output row. In that case, include all those columns in both the SELECT and GROUP BY clauses, like this:
WITH universe AS
SELECT *
FROM temp
-- WHERE patient_id IN (42972229)
, date_range AS
SELECT TRUNC (MIN (arrive_dt_tm)) AS first_date
, TRUNC (MAX (disch_dt_tm)) AS last_date
FROM universe
, all_dates AS
SELECT first_date + LEVEL - 1 AS a_date
FROM date_range
CONNECT BY LEVEL <= (last_date + 1) - first_date
SELECT u.doctor_id
, u.patient_id
, u.arrive_dt_tm
, u.disch_dt_tm
, TO_CHAR ( TRUNC (a.a_date, 'MONTH')
, 'YYYY MM'
) AS month
, COUNT (*) AS count_days
FROM all_dates a
JOIN universe u ON a.a_date BETWEEN TRUNC (u.arrive_dt_tm)
AND u.disch_dt_tm
GROUP BY u.doctor_id
, u.patient_id
, u.arrive_dt_tm
, u.disch_dt_tm
, TRUNC (a.a_date, 'MONTH')
ORDER BY u.patient_id
, TRUNC (a.a_date, 'MONTH')
;Output from your sample data (with no filtering):
`DOCTOR_ID PATIENT_ID ARRIVE_DT_TM DISCH_DT_TM MONTH COUNT_DAYS
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2012 10 14
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2012 11 30
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2012 12 31
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2013 01 31
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2013 02 28
755722 42972229 10/18/2012 7:50 3/14/2013 20:45 2013 03 14
763305 42972232 1/7/2013 20:27 3/15/2013 19:15 2013 01 25
763305 42972232 1/7/2013 20:27 3/15/2013 19:15 2013 02 28
763305 42972232 1/7/2013 20:27 3/15/2013 19:15 2013 03 15
25391509 42972298 2/4/2013 22:45 3/8/2013 22:3 2013 02 25
25391509 42972298 2/4/2013 22:45 3/8/2013 22:3 2013 03 8
746779 42972331 1/4/2013 23:0 3/26/2013 21:50 2013 01 28
746779 42972331 1/4/2013 23:0 3/26/2013 21:50 2013 02 28
746779 42972331 1/4/2013 23:0 3/26/2013 21:50 2013 03 26
763305 42972338 3/4/2013 22:19 3/6/2013 19:35 2013 03 3
In the end the objective is to count the no. of days in each month between the arrival and discharge dates by Physician and for his/her patients.Then is the output above really what you want? Say you're interested in physician 763305. That physician had 18-patient days in March, 2013, but the output doesn't make it clear.
I ran your query, it works great but I have a long list of patients for whom I have to run these counts.the query above includes all patient_ids.
That's a separate problem, to be solved in the first sub-query, universe. The rest of the query will be unchanged.
How will you know which patients to include? If you can derive the list from temp itself, just use a WHERE clause in universe. If you need to look at other tables, join them in universe, or use them in sub-queries in universe, or both.
For exmple, if you decide that the list of patient_ids has no pattern, and that you'll need to store their ids in a separate table (perhaps a global temporary table), then universe might be:
WITH universe AS
SELECT t.* -- or list columns needed
FROM temp t
JOIN patient_ids_to_include p
ON p.patient_id = t.patient_id
) ...The rest of the query can be the same as above.
If a same patient is admitted again then Patient_ID will be different no matter when readmitted.Are you saying that patient_id identifies a visit, not a patient, and that the same person is assigned a different patient_id every time that person is admitted?
For
INSERT INTO temp (doctor_id, patient_id, arrive_dt_tm, disch_dt_tm)
VALUES ( 755722
, 42972229
, TO_DATE ('03/14/2013 23:00', 'MM/DD/YYYY HH24:MI')
, TO_DATE ('04/01/2013 12:00', 'MM/DD/YYYY HH24:MI')
First the Patient ID will be different from the earlier admission. Second the results will show like:
Doctor_ID Patient_ID ARRIVE_DT_TM DISCH_DT_TM Month CountofDays
755722 42972229 3/14/2013 23:00 4/1/2013 12:00 2012 03 14
755722 42972229 3/14/2013 23:00 4/1/2013 12:00 2012 04 1Are you saying that temp.patient_id is unique, and so the situation is impossible?
Edited by: Frank Kulash on May 7, 2013 10:23 AM -
Extracting a count of distinct values between two date ranges over months
Hi All,
I am having a bit of difficulty in figuring out the query to build a list of active campaigns over a date range.
i.e. I have a table with campaign IDs and their start and end date details like this
Campaign_id Start_date End_date
10001 1-Jun-09 31-May-11
10002 1-Jun-09 23-Jun-11
30041 21-Aug-09 31-Dec-09
20005 3-Jun-10 31-May-11
90021 21-Nov-09 30-Nov-10
54000 1-Jun-11 1-Dec-12
35600 1-Mar-10 31-Mar-12 What the above data means is, for eg. the campaign 10001 is active from 1-Jun-09 to 31-May-11 i.e. for 24 months (inclusive of the month Jun-09 and May-11)
What I need to figure out is the counts of active campaigns between a date range and display that active count at a month level (for e.g. lets say we want to see all the campaigns that were active
between the date range '01-JUN-2007' and '30-APR-2012' ). So the partial output would be as seen below. The list would continue till december-2012
Month Year Count of active campaigns
Jan 2009 0
Feb 2009 0
Mar 2009 0
Apr 2009 0
May 2009 0
Jun 2009 2
Jul 2009 2
Aug 2009 3
Sep 2009 3
Oct 2009 3
Nov 2009 4
Dec 2009 4
Jan 2010 3
Feb 2010 3
Mar 2010 4
Apr 2010 4
Dec 2012 1 Could anybody please help me with the right query for this.
Thanks a lot for help
Regards
Goldiset pagesize 40
with tab as
select 1 id, sysdate -100 start_date, sysdate end_date from dual
union
select 1 id, sysdate -200 start_date, sysdate -150 end_date from dual
union
select 1 id, sysdate -600 start_date, sysdate - 400 end_date from dual
union
select 1 id, sysdate -300 start_date, sysdate - 150 end_date from dual
union
select 2 id, sysdate -100 start_date, sysdate-50 end_date from dual
year_tab as
select
add_months(min_date, level -1) m
from
select min(trunc(start_date,'YYYY')) min_date, add_months(max(trunc(end_date,'YYYY')), 12) max_date
from tab
connect by level <= months_between(max_date, min_date)
select to_char(m,'YYYY') year_,
to_char(m,'Month') month_,
nvl(act, 0) act
from year_tab,
select m date_,count(*) act
from tab, year_tab
where m between trunc(start_date,'MM') and trunc(end_date,'MM')
group by m
) month_tab
where m = date_(+)
order by m;
YEAR_ MONTH_ ACT
2010 January 0
2010 February 0
2010 March 0
2010 April 0
2010 May 0
2010 June 0
2010 July 0
2010 August 0
2010 September 1
2010 October 1
2010 November 1
2010 December 1
2011 January 1
2011 February 1
2011 March 1
2011 April 0
2011 May 0
2011 June 0
2011 July 1
2011 August 1
2011 September 1
2011 October 2
2011 November 2
2011 December 2
2012 January 2
2012 February 2
2012 March 2
2012 April 1
2012 May 1
2012 June 0
2012 July 0
2012 August 0
2012 September 0
2012 October 0
2012 November 0
2012 December 0
36 rows selected. -
Getting Record Count From Date Range - IDE: PLSQL Developer
I would like to count the number of member records that fall within a specified date range based on the members effective and expiration dates and their 'elg_code'. I have posted the SQL for some sample data. What I would like to see returned is three columns of counts where the members eff_date exp_date fall within the date range specified by the SQL and have an Elg_code of ' ' (one blank space).
So, what I would like is all members with elg_code ' ' where there eff_dt and exp_dt range falls within APR 2012, MAY 2012 & JUN 2012. So, based on the sample data I posted, Mark, where his elg_code record is ' ', his eff_dt is 1/1/2011 and his exp_dt is within APR 2012 (4/30/2012). Mark's range falls within APR 2012, but not MAY or JUNE of 2012. Marty would tally for both APR and MAY since his eff_dt is before MAY 2012 and his exp is within MAY 2012. etc..
Based on the data below, the results should look like:
APR MAY JUN
4 3 2
APR should have FRANK, MARK, MARTY, MARY,
MAY should have FRANK, MARTY, MARY
JUN should have FRANK and MARY
NOAM and JEAN should not show up as their records with elg_code ' ' do not have eff_dt and exp_dt records that fall within APR-JUN 2012.
So what I tried without success as it appears I have some kind of Cartesian issue (?), is:
select count(m1.mbr_name) APR,
count(m2.mbr_name) MAY,
count(m3.mbr_name) JUN
from 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 < '01-jun-2012'
and m2.exp_dt > '01-may-2012'
and m2.elg_code = ' '
and m3.eff_dt < '01-jul-2012'
and m3.exp_dt > '01-jun-2012'
and m3.elg_code = ' '
Below is the DML
Thanks for any assistance!
create table mbr2 (mbr_name varchar(10), grpid varchar(1), eff_dt date, exp_dt date, elg_code varchar(1))
commit
insert into mbr2 values ('MARK', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('04-30-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('MARK', 'A', to_date('05-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('MARTY', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('05-31-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('MARTY', 'A', to_date('06-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('FRANK', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('FRANK', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('MARY', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('MARY', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('JEAN', 'C', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('07-01-2011', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('JEAN', 'C', to_date('07-01-2011', 'MM-DD-YYYY'), to_date('01-01-2012', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('NOAM', 'D', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), ' ')
commitHi,
Here's one way:
WITH all_months AS
SELECT LEVEL AS month_num
, ADD_MONTHS (first_month, LEVEL - 1) AS month_begin
, ADD_MONTHS (first_month, LEVEL) AS next_month_begin
FROM (
SELECT TO_DATE ('04-01-2012', 'MM-DD-YYYY') AS first_month
, TO_DATE ('06-01-2012', 'MM-DD-YYYY') AS last_month
FROM dual
CONNECT BY level <= 1 + MONTHS_BETWEEN (last_month, first_month)
SELECT COUNT (CASE WHEN month_num = 1 THEN 1 END) AS month_1
, COUNT (CASE WHEN month_num = 2 THEN 1 END) AS month_2
, COUNT (CASE WHEN month_num = 3 THEN 1 END) AS month_3
FROM all_months a
JOIN mbr2 m ON a.month_begin <= m.exp_dt
AND a.next_month_begin > m.eff_dt
WHERE m.elg_code = ' '
;This assumes you know how many months will be in the output. If you want to derive that from the data, or to give the columns meaningful aliases (such as APR-2011 instead of MONTH_1), then you'll need dynamic SQL (to get separate columns for each month) or string aggregation (if you don't mind one big VARCHAR2 column, formatted to look like several columns). See {message:id=3527823}
If you don't mind modifying the query a little every time you run it, you can hard-code the number of columns and the month names. In the main query, put exactly as many lines as you need, with the alias you want at the end. That is, instead of
, COUNT (CASE WHEN month_num = 2 THEN 1 END) AS month_2say
, COUNT (CASE WHEN month_num = 2 THEN 1 END) AS may_2012I'm not sure exactly what the problem was with the query you posted. It looks like this site cut off parts of the query.
One thing that is definitely a mistake is that you're comparing DATE columns to VARCHAR2 literals. Never compare a DATE to a VARCHAR2; compare DATEs to DATEs (or compare VARCHAR2s to VARCHAR2s). So instead of
and m1.exp_dt > '01-apr-2012'you should say
and m1.exp_dt > TO_DATE ('01-apr-2012', 'dd-mon-yyyy')Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful.
Edited by: Frank Kulash on Jun 26, 2012 3:32 PM -
Assign Month within a date range (by most days in a given month)
I have a begin and end date, sample data as such
select to_date('01-13-12','mm-dd-yy') from_dt,
to_date('02-23-12','mm-dd-yy') to_dt
from dual
union all
select to_date('03-15-2012','mm-dd-yy') from_dt,
to_date('04-16-2012','mm-dd-yy') to_dt
from dual
union all
select to_date('05-13-2012','mm-dd-yy') from_dt,
to_date('07-23-2012','mm-dd-yy') to_dt
from dual
How do I assign a month by the most days in a month within that date range? Sometimes the date range might have the exact same amount of days in a month (like 3/15/2012 has 16 days and 4/16/2012 has 16 days). In this case, I want the earlier month (march).
So from the sample data:
01/13/2012, 02/23/2012, February
03/15/2012, 04/16/2012, March
05/13/2012, 07/23/2012, June
Thanks
Edited by: user4422426 on Mar 1, 2012 5:15 PMHi,
Here's one way:
WITH cntr AS
SELECT LEVEL - 1 AS n
FROM (
SELECT 1 + MAX (to_dt - from_dt) AS max_day_cnt
FROM table_x
CONNECT BY LEVEL <= max_day_cnt
, got_r_num AS
SELECT x.from_dt, x.to_dt
, TRUNC (x.from_dt + c.n, 'MONTH') AS month
, count (*) AS cnt
, ROW_NUMBER () OVER ( PARTITION BY from_dt, to_dt
ORDER BY COUNT (*) DESC
, TRUNC (x.from_dt + c.n, 'MONTH')
) AS r_num
FROM cntr c
JOIN table_x x ON c.n <= x.to_dt - x.from_dt
GROUP BY x.from_dt, x.to_dt
, TRUNC (x.from_dt + c.n, 'MONTH')
SELECT from_dt, to_dt
, TO_CHAR (month, 'Mon YYYY') AS mon
, cnt
FROM got_r_num
WHERE r_num = 1
;Thanks for posting code to create the same data. Please test your code before you post it: you got the order of arguments to TO_DATE reversed. -
Hi, I have data containing a task name and it's start and end date.
Now, I want new data based on above that will tell how many tasks were running at any given date.
Assuming that time stamp for each date is 00:00:00, and tasks start at Start_Dt but end just before End_dt (lower bound included, upper bound not)
Input:
Task Start_Dt End_D
A 2013-12-01 2013-12-05
B 2013-12-03 2013-12-04
C 2013-12-03 2013-12-04
D 2013-12-03 2013-12-06
E 2013-12-04 2013-12-05
F 2013-12-06 2013-12-07
Output:
Start
End
Count
(Jobs)
2013-12-01
2013-12-03
1
(A)
2013-12-03
2013-12-04
4
(A,B,C,D)
2013-12-04
2013-12-05
3
(A,D,E)
2013-12-05
2013-12-06
1
(D)
2013-12-06
2013-12-07
1
(F)
In the above, Task B should not be in 3rd row since it ended on 12/04 and hence not to be included in the 12/04 - 12/05 range (and B was not running in this period)
alter session set nls_date_format = 'YYYY-MM-DD';
select tmp.* from
select 'A' TASK, to_date('2013-12-01','YYYY-MM-DD') START_DT, to_date('2013-12-05','YYYY-MM-DD') END_DT from dual
union
select 'B' TASK, to_date('2013-12-03','YYYY-MM-DD') START_DT, to_date('2013-12-04','YYYY-MM-DD') END_DT from dual
union
select 'C' TASK, to_date('2013-12-03','YYYY-MM-DD') START_DT, to_date('2013-12-04','YYYY-MM-DD') END_DT from dual
union
select 'D' TASK, to_date('2013-12-03','YYYY-MM-DD') START_DT, to_date('2013-12-06','YYYY-MM-DD') END_DT from dual
union
select 'E' TASK, to_date('2013-12-04','YYYY-MM-DD') START_DT, to_date('2013-12-05','YYYY-MM-DD') END_DT from dual
union
select 'F' TASK, to_date('2013-12-06','YYYY-MM-DD') START_DT, to_date('2013-12-07','YYYY-MM-DD') END_DT from dual
)tmp
order by 2,3
How to write the sql on the input ('tmp' table)?
Thanks,
-srinivas y.
Message was edited by: ysri
Message was edited by: ysri. Updated tasks and dates.with
data_table as
(select 'A' task,date '2013-12-01' started,date '2013-12-05' ended from dual union all
select 'B',date '2013-12-03',date '2013-12-04' from dual union all
select 'C',date '2013-12-04',date '2013-12-05' from dual union all
select 'D',date '2013-12-06',date '2013-12-07' from dual
date_periods as
(select date '2013-12-01' started,date '2013-12-03' ended from dual union all
select date '2013-12-03',date '2013-12-04' from dual union all
select date '2013-12-04',date '2013-12-05' from dual union all
select date '2013-12-06',date '2013-12-07' from dual
select to_char(p.started,'yyyy-mm-dd') "Start",
to_char(p.ended,'yyyy-mm-dd') "End",
count(*) "Count",
'('||listagg(t.task,',') within group (order by t.task)||')' "(Jobs)"
from date_periods p
left outer join
data_table t
on greatest(t.started,p.started) < least(t.ended,p.ended)
group by p.started,p.ended
order by p.started
Start
End
Count
(Jobs)
2013-12-01
2013-12-03
1
(A)
2013-12-03
2013-12-04
2
(A,B)
2013-12-04
2013-12-05
2
(A,C)
2013-12-06
2013-12-07
1
(D)
Regards
Etbin -
List all dates in date range even if there is no record for that day
Hi,
I have a really simple report but am having a problem with dates.
I want to list sales per day over a date range (start date and end date parameters).
The data i am working with may not have sales on every day which is causing grief with my average calculations.
I need to capture days in current month which i have with DateDiff ("d", {?Start Date}, {?End Date}+1)
Then days elapsed in the month. This is where my problem is. I have grouped my data by day to get day totals, and have then attempted to use a summary on DISTINCT COUNT to get the result i need. The problem is it only counts the lines that are there, so if a day has no sales it is skipped and the count is incorrect and then the average calculations i have are not correct.
Help will be much appreciated.YOu can not create data that does not exist.
You will need to add in a table which has all dates and make that you first table with all other data joined with a left out from this date table.
Ian -
Multi-month, year, and date-range views
My apologies for wasting everyone's time if I've missed something really obvious here, but am I correct in concluding that there is no way to generate anything beyond a static one-month view in iCal? No multi-month view? No year view? No "view date range"? At this stage of the game, how is it possible that something so elemental could be omitted from this program?
At this point I'm reduced to exporting each month to a pdf file and then arranging them as tiles on the screen. But there has to be a better way.
Short of a new version from Apple, are there any plug-ins that would do this? Does anyone have any other work-arounds? Thanks in advance.A quick search of the forums shows that this is a HIGHLY sought after feature (including by me). We all need to use the feed back link (below) to let Apple know this. In my experience with the tech support folks, they seem to have NO idea about what these forums say. In the past I have actually had them log in to these forums and search the issue I was calling them about and they have been blown away when they see the hundreds / thousands of posts with the very same thing they're discussing with me. So, as the wise man says .. "you don't ask, you don't get!" Use the form and let them know...
http://www.apple.com/feedback/ical.html -
All my game data i was playing for months was lost. Seems when i trie to put my nickname its already in used. So i need to put a new nickname then it create a whole new acc for me. All my previous data was lost. How can i recover my all my data or use my old nickname? Pls help
kerryp123
Yes, it is the same with me. I know a friend how upgraded to iso7 and lost all of his game center data. He tried singning in and re singning in but no change. Plus on to of all of this, apple is like have me and him to re-validate our itunes acount. Somthing weried is going on. So verified my itunes acount and it shows under settings that I am signed in, but when I go to the app store on my iPhone and try downloading or even updating a app, it says "can not conect to itunes store" after singing in! AND I'M NOT SURE I AM GOING TO UPGRADE TO ISO7 YET UNTILL THEY FIX THIS BUG!!!! HELP! -
SQL DISTINCT COUNT IN DATE RANGE
Dear All
Thanks for your attention.
A table is storing the task summary of each order
e.g.
Date | Order Number | Task Number
2015-01-01 00:00:01 | ABC123456 | JOB001
2015-01-01 00:01:02 | ABC123456 | JOB002
2015-01-01 00:10:02 | ABC123444 | JOB001
2015-01-01 10:12:59 | ABC123456 | JOB002 (Since the job002 is not done correctly, need to work again)
2015-01-01 18:20:05 | ABC888888 | JOB001
2015-01-01 20:22:42 | ABC789456 | JOB001
2015-01-01 21:02:11 | BBB121212 | JOB002
I would like to write a single query that get the distinct count of order number in sepcific date range
result as expected in three columns:
2015-01-01
task_number | AM | PM
JOB001 | 2 | 2
JOB002 | 1 | 1
explain the figures here:
JOB001 AM = 2 (ABC123456, ABC123444)
JOB002 AM = 1 (two records of ABC123456 count as 1 since we would like to know the number of orders)
JOB001 PM = 2 (ABC888888, ABC789456)
JOB002 PM = 1 (BBB121212)
I wrote a query get similar result but the count of order is not distinct
SELECT task_number,
AM=SUM(CASE WHEN date >= ('2015-01-01 00:00:00') AND date < ('2015-01-01 12:00:00') THEN 1 ELSE 0 END),
PM=SUM(CASE WHEN date >= ('2015-01-01 12:00:00') AND date < ('2015-01-02 00:00:00') THEN 1 ELSE 0 END)
FROM MyTable
WHERE task_number = 'JOB001'
Could anyone advise how to enhance this query to let the result become disintct of order number?
Many Thanks,
swivanTry
select task_number,
count(distinct case when datepart(hour, [date]) >=0 and datepart(hour, [date]) < 12 then [OrderNumbers] END) as AM,
count(distinct case when datepart(hour, [date]) >=12 and datepart(hour, [date]) < 24 then [OrderNumbers] END) as PM FROM myTable
GROUP BY Task_Number
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Thank you very much, short and good answer -
How to convert Date,month,date range row to column
hi experts
i have a requirement like
i want to change date, month and if user key in date range (01-04-2010 to 31-04-2010) then o/p like below
1st-o/p-format
d1 d2 d31
01-04-2010 02-04-2010 ------------------------------- 31-04-2010
2nd-o/p-format -based on transaction month
d1 d2 d31
jan feb ------------------------------------------ mar
3rd-o/p-format date range based on transaction date
d1 d2 d31
01-04-2008 02-04-2010 -------------------------------31-04-2009
Note :Basically i want to display these formate in Oracle Forms then based these dates i want to fetch record like below:
d1 d2 d31
jan feb dec
count sales amt avgsales count sales amt avgsales ------------------ count sales amt avgsales
so can anyone tell me what approach i shouls apply..
Thanks
abhishekThis forum is titled "Certification" - pl post in the PL/SQL or Database General forums for a better/faster response.
HTH
Srini
Maybe you are looking for
-
Is the Mini DisplayPort the same as the Thunderbolt port?
I need to know, because I'd like to mirror my computer to my TV.
-
Digitizer Driver for T410s?
I'm curious as to why the T410s has a driver for a digitizer. (Windows 7 64-bit, released 4/26/2010) http://www-307.ibm.com/pc/support/site.wss/document.do?lndocid=MIGR-73771 I read some of the forums and people stated that the T410s does not suppo
-
Shipment output condition record
Hi all, The Shipment output condition record is maintained which can be seen in T code VV73(V7 application) This is stored in the table B999.In this table iam getting the condition record number but there is no condition record creation date I want t
-
Variables/parrameters in stored procs
Hi All Are there any way of specifying something other than the stored procedure name to be appended in front of local variables and parrameters in a SP when migrating from Sybase 11.9.2. Our variables mostly have the same name as table columns since
-
RA-06553: PLS-801: internal error [55018]
Hi Friends.. Please Help in this. CREATE OR REPLACE FUNCTION dept_for_name( deptnamein IN departments.department_name%type) RETURN departments%rowtype AS l_return departments%rowtype; FUNCTION is_secret_dept( deptnamein IN de