CUIC Hourly Interval Query Throughout Date Range (SQL Squirrel Stuff)
I’m trying to create a specific report in CUIC using the "Contact Service Queue Activity Report by Interval” canned report. The objective is to query intervals (by hour), throughout the whole reporting time frame. This can be done by interval and reported per day in the canned report, but I’m trying to get the values per interval without the day displayed. For example, I’d like a report that shows total calls per hour (8-9 am, 9-10, 10-11am etc) throughout the reporting date range. The report would look like this:
Interval Start Time Interval End Time CSQ Name Calls Presented Handled
8:00:00 AM 9:00:00 AM CSQ_ 41 40
9:00:00 AM 10:00:00 AM CSQ_ 63 60
10:00:00 AM 11:00:00 AM CSQ_ 50 50
Instead of this:
Interval Start Time Interval End Time CSQ Name Calls Presented Handled
3/3/14 8:00 3/3/14 9:00 CSQ_ 16 16
3/3/14 9:00 3/3/14 10:00 CSQ_ 23 21
3/3/14 10:00 3/3/14 11:00 CSQ_ 24 24
3/4/14 8:00 3/4/14 9:00 CSQ_ 13 13
3/4/14 9:00 3/4/14 10:00 CSQ_ 26 25
3/4/14 10:00 3/4/14 11:00 CSQ_ 14 14
3/5/14 8:00 3/5/14 9:00 CSQ_ 12 11
3/5/14 9:00 3/5/14 10:00 CSQ_ 14 14
3/5/14 10:00 3/5/14 11:00 CSQ_ 12 12
Hi
So you want the report to add up all the calls between 9-10am, e.g. if you run it for a week it would add up the calls for each day between 9 and 10 am and show a single total for that hour?
Aaron
Similar Messages
-
Querying on date range issue?
Hi,
I'm looking for a solution for this issue,
In the DB Table there is a column of type TIMESTAMP. so when i query for date range using BETWEEN keyword it returns data inserted on next day at 12.00.00 AM
example :
Table A
ColumnA_
data1
data2
data3
ColumnTime_
08/03/2010 11.55.00 AM
08/04/2010 12.00.00 AM
08/04/2010 12.00.01 AM
here when I query using between (08/03/2010 and 08/04/2010) keyword on Column_Time then ,
Ouput
data1
data2
but I'm just looking for data1 in the output, any ideas?Between is inclusive of both boundaries:
column between condition1 and condition2Translates into
condition1 >= column
and
condition2 <= columnSo tell the code what you want ... in your case
condition1 >= column
and
condition2 < columnI'm not sure if you expect the >= or not...adjust as needed. -
Hi, I am using Oracle 10g. I want a query which gives me below output.
Data Setup
========
create table t_date_range (
ID number (2),
start_date date,
end_date date);
insert into t_date_range values (1,to_date('20110101', 'YYYYMMDD'),to_date('20110331', 'YYYYMMDD'));
insert into t_date_range values (2,to_date('20110401', 'YYYYMMDD'),to_date('20110531', 'YYYYMMDD'));
insert into t_date_range values (3,to_date('20110701', 'YYYYMMDD'),to_date('20110731', 'YYYYMMDD'));
insert into t_date_range values (4,to_date('20110901', 'YYYYMMDD'),to_date('20111130', 'YYYYMMDD'));
insert into t_date_range values (5,to_date('20111201', 'YYYYMMDD'),to_date('20111231', 'YYYYMMDD'));
commit;
SQL> select ID, to_char(start_date,'DD-MON-YYYY') START_DATE, to_char(end_date,'DD-MON-YYYY') END_DATE from t_date_range;
ID START_DATE END_DATE
1 01-JAN-2011 31-MAR-2011
2 01-APR-2011 31-MAY-2011
3 01-JUL-2011 31-JUL-2011
4 01-SEP-2011 25-OCT-2011
5 26-OCT-2011 30-NOV-2011
6 01-DEC-2011 31-DEC-2011
6 rows selected.
I want result in this form:
START_DATE END_DATE
01-JAN-2011 31-MAY-2011
01-JUL-2011 31-JUL-2011
01-SEP-2011 31-DEC-2011
Means if there is a difference of exact one day between "start_date of 2nd row" and "end_date of first row" then make a single row as shows in above results set.
Thanks!Hi,
Solomon Yakobson wrote:
Keep in mind, Franks's solution assumes date ranges do not overlap. ...That's true. If rows can overlap, then we might need to use two sub-queries, like you did: one to see if a new group starts with this row, and another to count how many new groups have already started.
The solution you posted assumes a relationship between id and dates. If we add a row like this to the sample data:
insert into t_date_range values (6,to_date('20101201', 'YYYYMMDD'),to_date('20121231', 'YYYYMMDD'));that overlaps all the others, then how would that solution work?
LAG won't work, because no matter how we sort the rows, we can't be sure that overlapping rows will be consecutive.
Here's one way to handle overlapping rows:
WITH got_new_grp AS
SELECT id, start_date, end_date
, CASE
WHEN start_date > 1 + MAX (end_date) OVER ( ORDER BY start_date
, id
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
THEN 1
END AS new_grp
FROM t_date_range
, got_grp AS
SELECT start_date, end_date
, COUNT (new_grp) OVER ( ORDER BY start_date
, id
) AS grp
FROM got_new_grp
SELECT MIN (start_date) AS start_date
, MAX (end_date) AS end_date
FROM got_grp
GROUP BY grp
ORDER BY start_date
;This solution uses id (assumed to be unique) just to get a consistent ordering, in case 2 (or more) rows have exactly the same start_date. It does not assume any relationship between ids and either start_date or end_date.
I'm also assuming that start_date<=end_date on each row.
Edited by: Frank Kulash on May 11, 2011 12:54 PM
The above refers to the solution you oriognally posted, which was:
with t1 as (
select id,
start_date,
end_date,
case
when lag(end_date) over(order by id) + 1 >= start_date then 0
else 1
end start_of_group
from t_date_range
t2 as (
select id,
start_date,
end_date,
sum(start_of_group) over(order by id) grp
from t1
select min(start_date) start_date,
max(end_date) end_date
from t2
group by grp
order by grp
; -
Hi I am using below query
select count(*) from
select M.Login_name
, P.IND_PROD_LOGIN_ID
, count(P.IND_PROD_LOGIN_ID) over (partition by P.IND_PROD_LOGIN_ID) cnt
from CITI_USER_2.CCS_CUSTOMER_MAST M
, CITI_USER_2.CCS_CUSTOMER_PROD P
WHERE M.CUSTOMER_ID = P.CUSTOMER_ID and P.IND_PROD_LOGIN_ID not like '508127%'
and to_char( M.CREATE_DATE , 'DD/MM/YYYY') = '16/10/2009'
) where cnt = 1
and translate(Login_name,'x0123456789','x') is null
and i got the result as 10 records but if i try to put in the date range as below
select count(*) from
select M.Login_name
, P.IND_PROD_LOGIN_ID
, count(P.IND_PROD_LOGIN_ID) over (partition by P.IND_PROD_LOGIN_ID) cnt
from CITI_USER_2.CCS_CUSTOMER_MAST M
, CITI_USER_2.CCS_CUSTOMER_PROD P
WHERE M.CUSTOMER_ID = P.CUSTOMER_ID and P.IND_PROD_LOGIN_ID not like '508127%'
and to_char( M.CREATE_DATE , 'DD/MM/YYYY') between '16/10/2009' and '17/10/2009'
) where cnt = 1
and translate(Login_name,'x0123456789','x') is null
i got the result as 653 records
But it should be 10 only and when i check the records it is giving me wrong result.
Can someone highlight how to use the range between the date, i need to check the number of records between the date range from 01/05/2009 to 01/10/2009this is all happening because of
to_char( M.CREATE_DATE , 'DD/MM/YYYY') between '16/10/2009' and '17/10/2009'in your second query...
that is doing a string comparision not date comparision...
in string comparision
check this example...
select * from dual where '17/10/2009' > '16/10/2010'which will result a record but you might also observe that 17 th in 2009 is less than 16th of 2010. this is because here string comparision is ocurring.
you need to change it as....
M.CREATE_DATE between to_date('16/10/2009','DD/MM/YYYY') and to_date('17/10/2009','DD/MM/YYYY')so your query becomes...
select count(*) from
select M.Login_name
, P.IND_PROD_LOGIN_ID
, count(P.IND_PROD_LOGIN_ID) over (partition by P.IND_PROD_LOGIN_ID) cnt
from CITI_USER_2.CCS_CUSTOMER_MAST M
, CITI_USER_2.CCS_CUSTOMER_PROD P
WHERE M.CUSTOMER_ID = P.CUSTOMER_ID and P.IND_PROD_LOGIN_ID not like '508127%'
and M.CREATE_DATE between to_date('16/10/2009','DD/MM/YYYY') and to_date('17/10/2009','DD/MM/YYYY')
) where cnt = 1
and translate(Login_name,'x0123456789','x') is null check your answer and get back.
Ravi Kumar
Edited by: ravikumar.sv on Oct 16, 2009 3:12 PM -
Query for date range? DPL
Sorry if this has been answered before, but I couldn't find the answer. I'm new to the DPL and was trying to find out how to query for objects that fall within a "date range". If I have an object that has a field "effective date", I would like to query for a list of objects that fall between a date range. Can I do this with DPL?
thanks,
JohnHi,
Yes, the DPL can be used to iterate over the entities where a given date field D falls with a specified date range. To do this, you define D as a @SecondaryKey, create a SecondaryIndex for it, and get a cursor for the specified range using one of the SecondaryIndex.entities() methods. Be sure to close the cursor.
See the Key Ranges section in this class description:
http://www.oracle.com/technology/documentation/berkeley-db/je/java/com/sleepycat/persist/EntityCursor.html
Dates are treated as long values, so a range of Date keys is no different that a range of long keys. Date and long are both "simple types" according to the DPL definition.
--mark -
Query for date range? JE
Hi,
I have seem some posts on the JE forum regarding quering for date range, but it is mostly using DPL.
Is there any way to do that using the JE API
Thanks,
MohammadHi Mohammad,
A date range query can be performed as a key range query. There's nothing special about dates except that you'll want to use a key binding that gives a meaningful sort order. If you're representing your dates in milliseconds, then a LongBinding (in com.sleepycat.bind.tuple) will work well. In general, use tuple bindings for keys, because they provide a meaningful sort order.
To perform a range query, this FAQ has some hints:
http://www.oracle.com/technology/products/berkeley-db/faq/je_faq.html#28
On range searches in general, they can be done with Cursor.getSearchKeyRange or with the SortedSet.subSet and SortedMap.subMap methods, depending on whether you are using the base API or the Collections API. It is up to you which to use.
If you use Cursor.getSearchKeyRange you'll need to call getNext to iterate through the results. You'll have to watch for the end range yourself by checking the key returned by getNext. This API does not have a way to enforce range end values automatically.
If you use the Collections API you can call subMap or subSet and get an Iterator on the resulting collection. That iterator will enforce both the beginning and the end of the range automatically.
Does this answer your question?
--mark -
SQL query generating data in SQL management studio but not in CUIC interface
Hello,
I'm working on a UCCE 9.0 system
I created SQL query for a report.
Whenever I run the query inside the SQL management studio, it works and generates data, but when I run it in the CUIC interface it works but generates nothing. As you can see in the below snapshot, it contains 209 records that are not being displayed.
Any help would be greatly apprciatedIs it a custom report ? Can you right click on your Custom Report and click on Edit Views. You need to check whether you have Grid Headers Listed.
Regards,
Senthil -
I have a query that will retrieve order between selected dates. It works great but returns no record if the 2 dates are the same.
Example:
Orders between 9-1-2010 and 9-30-2010 retunes 35 records.
But if I select between -9-25-2010 and 9-25-2010, so I can see all order from this 1 day, it returns 1 records, and I know there are records for that day!
Here's my query:
<%
Dim rsOrders__MMColParam
rsOrders__MMColParam = "1"
If (Request.QueryString("datefrom") <> "") Then
rsOrders__MMColParam = Request.QueryString("datefrom")
End If
%>
<%
Dim rsOrders__MMColParam2
rsOrders__MMColParam2 = "1"
If (Request.QueryString("dateto") <> "") Then
rsOrders__MMColParam2 = Request.QueryString("dateto")
End If
%>
<%
Dim rsOrders
Dim rsOrders_cmd
Dim rsOrders_numRows
Set rsOrders_cmd = Server.CreateObject ("ADODB.Command")
rsOrders_cmd.ActiveConnection = MM_ezcaldatasource_STRING
rsOrders_cmd.CommandText = "SELECT * FROM dbo.orders WHERE (OrderDate between ? and ?) AND Finalized = 1"
rsOrders_cmd.Prepared = true
rsOrders_cmd.Parameters.Append rsOrders_cmd.CreateParameter("param1", 135, 1, -1, rsOrders__MMColParam) ' adDBTimeStamp
rsOrders_cmd.Parameters.Append rsOrders_cmd.CreateParameter("param2", 135, 1, -1, rsOrders__MMColParam2) ' adDBTimeStamp
Set rsOrders = rsOrders_cmd.Execute
rsOrders_numRows = 0
%>2 possible
1) Change the column's data type from a datetime to a date if supported by your DBMS
2) Use date math to always add 1 day to the end date. So instead of the end date of 9-25-2010 (00:00) it will be 9-26-2010 (00:00) -
User requested to find all current terminated employee who was eligible for benefit in 2008.
We need to fetch data from Oracle Application HR Date Tracked table, PER_ALL_ASSIGNEMNTS_F which employee’s assignment record is stored based on the EFFECTIVE_START and EFFECITVE_END dates.
It's possible an employee who was eligible for benefit in MAR-MAY but became ineligible in JUN-JULY. (That would be 2 assignment records with different range of effective start and end date).
We've developed a PL/SQL for scanning the assignment record day by day thru out 2008. See the code below.
The question is, how can we implement same logic (scanning the assignment record day by day) in PL/SQL into SQL?
Thanks!
(seems like this is different forum found in metalink, I'm sorry if I posted twice.)
============================================================
(FETCHING TERMINATED EEs)
for i_counter in 0 .. (to_date('01/01/2008','mm/dd/yyyy')to_date('12/31/2008','mm/dd/yyyy')) loop
check_date := to_date('01/01/2008','mm/dd/yyyy')+i_counter;
--Validate if the assignment is eligible for benefit by using genterated check_date.
v_found :=0;
select 1
into v_found
from per_all_assignments_f
where check_date between effective_start_date and effective_end_date
and group_id = 137 --eligible for benefit
if v_found = 1 then
v_eligible_found := 'YES';
exit;
end if;
end loop;
Edited by: user595907 on Mar 23, 2009 5:10 PM
Edited by: user595907 on Mar 23, 2009 5:11 PMFrank Kulash wrote:
I'm glad it helped, but I still don't understand exactly what you need.
Do you want 366 rows of output?
If so, then yes, this is a good way to get it.
If not, there's probably a better way, as Sy suggested.
Hi Frank
The assignment record we have is date tracked which means each record is tracked by effective_start and effective_end.
An employee could have mutiple assignment record within an year.
User is asking to report any ee who is currently terminated and who was eligible for benefit (a data column in assignment record) anytime in 2008.
The idea I had came from the PL/SQL logic.
By using PL/SQL, I can scan the assignment record day by day. That helped to fetch every assignment record where the check_date falls between the effective_start and effective_end.
Also, reason I asked to use the "CONNECT BY Query" (thanks Frank!) because not all assignment starts and ends between 1/1/2008 and 12/31/2008.
Some ee could have started prior to 2008 and ended mid 2008 and then started the end of 2008.
Please let me know if there is a better way to resolve this problem.
Thanks everyone for all the ideas, I really appreciate it!
Brian. -
Abnormal, Same query get data in sql but not working on Fron-end
Dear,
Version :Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
We have created packed in oracle database befor two months ago & was working fine, but since morning select statment in package is not working while running via application which mentioned below and raise not data found but surprising thing is that same query is getting data when we execut on sql plus return one record.
i don't know either it's abnormal behaviour or sth else becuase the same query run without changing any singl column in where_clause work in sql but not getting data while submition request through oracle application and raising not data found.
--thankse
Edited by: oracle0282 on Dec 29, 2011 2:20 AMActully when i run this query in sql it return one record on the same parameter, while when we exeucte this select in pl/sql on the same parameter oracle raise no data found error.
so i got confused the same query with same parameter retur record in sql but when we call it fron-end through packege raise 'no data found error'.
hope you understand now.
--thanks -
I have a DATA table which consists of Minutely data. For every unique filename, solar_id, year, day there is a unique processing stage that can exist and to find the highest processing stage we select the MAX(date_stamp) for that particular day since it will be the latest touched stage.
So for any given day we can have 1440minutes x (5processing stages on average) = ~7k records.
If I have a query that wants to look at the highest processing stage that exists each day over a TIME FRAME, how can I improve it's performance?
SELECT DISTINCT s.NAME NAME, s.climate_id climate_id, a.solar_id solar_id,
a.processing_stage processing_stage, a.YEAR YEAR, a.DAY DAY,
TO_CHAR (a.date_stamp, 'YYYY-MM-DD HH24:MI:SS') date_stamp, a.user_id
FROM solar_station s, DATA a
WHERE a.solar_id = '636'
AND s.solar_id = '636'
AND s.climate_id = '611KBE0'
AND a.solar_id = s.solar_id
AND a.time_stamp BETWEEN TO_DATE ('2004-9-8', 'YYYY-MM-DD')
AND TO_DATE ('2004-9-20', 'YYYY-MM-DD')
AND (a.solar_id, a.filename, a.YEAR, a.DAY, a.date_stamp) IN (
SELECT b.solar_id, b.filename, b.YEAR, b.DAY, MAX (b.date_stamp)
FROM DATA b
WHERE b.solar_id = '636'
AND b.solar_id = s.solar_id
AND b.time_stamp BETWEEN TO_DATE ('2004-9-8', 'YYYY-MM-DD')
AND TO_DATE ('2004-9-20', 'YYYY-MM-DD')
GROUP BY b.solar_id, b.filename, b.YEAR, b.DAY)
ORDER BY s.NAME, a.YEAR, a.DAY;The data table is partioned by YEAR and sub-partioned by solar_id.Hmm, still a full table scan on data. This is
probably caused by the unnecessary line
AND b.solar_id = s.solar_idWhat happens if you remove this line?
Regards,
Rob.
SELECT DISTINCT s.NAME NAME, s.climate_id climate_id, a.solar_id solar_id,
a.processing_stage processing_stage, a.YEAR YEAR, a.DAY DAY,
TO_CHAR (a.date_stamp, 'YYYY-MM-DD HH24:MI:SS') date_stamp, a.user_id
FROM solar_station s, DATA a
WHERE a.solar_id = '636'
AND s.solar_id = '636'
AND s.climate_id = '611KBE0'
--AND a.solar_id = s.solar_id
and year between 2004 and 2004
AND a.time_stamp BETWEEN TO_DATE ('2004-9-8', 'YYYY-MM-DD')
AND TO_DATE ('2004-12-20', 'YYYY-MM-DD')
AND (a.solar_id, a.filename, a.YEAR, a.DAY, a.date_stamp) IN (
SELECT b.solar_id, b.filename, b.YEAR, b.DAY, MAX (b.date_stamp)
FROM DATA b
WHERE b.solar_id = '636'
--AND b.solar_id = s.solar_id
and year between 2004 and 2004
AND b.time_stamp BETWEEN TO_DATE ('2004-9-8', 'YYYY-MM-DD')
AND TO_DATE ('2004-12-20', 'YYYY-MM-DD')
GROUP BY b.solar_id, b.filename, b.YEAR, b.DAY)
ORDER BY s.NAME, a.YEAR, a.DAY;
PLAN_TABLE_OUTPUT
Plan hash value: 4121458178
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 156 | 19500 | 30539 (4)| 00:06:07 | | |
| 1 | SORT ORDER BY | | 156 | 19500 | 30539 (4)| 00:06:07 | | |
| 2 | HASH UNIQUE | | 156 | 19500 | 30538 (4)| 00:06:07 | | |
|* 3 | HASH JOIN | | 156 | 19500 | 30537 (4)| 00:06:07 | | |
| 4 | NESTED LOOPS | | 1401 | 95268 | 15279 (4)| 00:03:04 | | |
|* 5 | TABLE ACCESS BY INDEX ROWID| SOLAR_STATION | 1 | 27 | 1 (0)| 00:00:01 | | |
|* 6 | INDEX UNIQUE SCAN | SOLAR_STATION_PK | 1 | | 0 (0)| 00:00:01 | | |
| 7 | VIEW | VW_NSO_1 | 1401 | 57441 | 15278 (4)| 00:03:04 | | |
| 8 | SORT GROUP BY | | 1401 | 82659 | 15278 (4)| 00:03:04 | | |
| 9 | PARTITION RANGE ITERATOR | | 272K| 15M| 15245 (3)| 00:03:03 | 10 | 11 |
| 10 | PARTITION HASH SINGLE | | 272K| 15M| 15245 (3)| 00:03:03 | 1 | 1 |
|* 11 | TABLE ACCESS FULL | DATA | 272K| 15M| 15245 (3)| 00:03:03 | | |
| 12 | PARTITION RANGE ITERATOR | | 272K| 14M| 15254 (4)| 00:03:04 | 10 | 11 |
| 13 | PARTITION HASH SINGLE | | 272K| 14M| 15254 (4)| 00:03:04 | 1 | 1 |
|* 14 | TABLE ACCESS FULL | DATA | 272K| 14M| 15254 (4)| 00:03:04 | | |
Predicate Information (identified by operation id):
3 - access("A"."SOLAR_ID"="$nso_col_1" AND "A"."FILENAME"="$nso_col_2" AND "A"."YEAR"="$nso_col_3" AND
"A"."DAY"="$nso_col_4" AND "A"."DATE_STAMP"="$nso_col_5")
5 - filter("S"."CLIMATE_ID"='611KBE0')
6 - access("S"."SOLAR_ID"='636')
11 - filter("B"."TIME_STAMP">=TO_DATE('2004-09-08 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND "YEAR"=2004 AND
"B"."SOLAR_ID"='636' AND "B"."TIME_STAMP"<=TO_DATE('2004-12-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss'))
14 - filter("A"."TIME_STAMP">=TO_DATE('2004-09-08 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND "YEAR"=2004 AND
"A"."SOLAR_ID"='636' AND "A"."TIME_STAMP"<=TO_DATE('2004-12-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss'))>
Message was edited by:
Rob van Wijk
ddition questions:
What is the outcome of:
select count(*)
from ( SELECT s.NAME NAME
, s.climate_id climate_id
, a.solar_id solar_id
, a.processing_stage processing_stage
, a.YEAR YEAR
, a.DAY DAY
, TO_CHAR (a.date_stamp, 'YYYY-MM-DD HH24:MI:SS')
date_stamp
, a.user_id
, max(a.date_stamp) over (partition by a.solar_id,
a.file_name, a.year, a.day) max_date_stamp
FROM solar_station s
, DATA a
WHERE a.solar_id = '636'
AND s.solar_id = '636'
AND s.climate_id = '611KBE0'
AND a.solar_id = s.solar_id
AND year between 2004 and 2004
AND a.time_stamp BETWEEN TO_DATE ('2004-9-8',
'YYYY-MM-DD') AND TO_DATE ('2004-12-20',
'YYYY-MM-DD')
Here we get:
COUNT(*)
1266111
1 row selected.
You might remove the subquery by issuing:
select distinct *
from ( SELECT s.NAME NAME
, s.climate_id climate_id
, a.solar_id solar_id
, a.processing_stage processing_stage
, a.YEAR YEAR
, a.DAY DAY
, TO_CHAR (a.date_stamp, 'YYYY-MM-DD HH24:MI:SS')
date_stamp
, a.user_id
, max(a.date_stamp) over (partition by a.solar_id,
a.file_name, a.year, a.day) max_date_stamp
FROM solar_station s
, DATA a
WHERE a.solar_id = '636'
AND s.solar_id = '636'
AND s.climate_id = '611KBE0'
AND a.solar_id = s.solar_id
AND year between 2004 and 2004
AND a.time_stamp BETWEEN TO_DATE ('2004-9-8',
'YYYY-MM-DD') AND TO_DATE ('2004-12-20',
'YYYY-MM-DD')
date_stamp = max_date_stamp
ORDER BY s.NAME
, a.YEAR
, a.DAY
select distinct *
from ( SELECT s.NAME NAME
, s.climate_id climate_id
, a.solar_id solar_id
, a.processing_stage processing_stage
, a.YEAR YEAR
, a.DAY DAY
, TO_CHAR (a.date_stamp, 'YYYY-MM-DD HH24:MI:SS') date_stamp
, a.user_id
, max(a.date_stamp) over (partition by a.solar_id, a.filename, a.year, a.day) max_date_stamp
FROM solar_station s
, DATA a
WHERE a.solar_id = '636'
AND s.solar_id = '636'
AND s.climate_id = '611KBE0'
AND a.solar_id = s.solar_id
AND year between 2004 and 2004
AND a.time_stamp BETWEEN TO_DATE ('2004-9-8', 'YYYY-MM-DD') AND TO_DATE ('2004-12-20', 'YYYY-MM-DD')
where date_stamp = max_date_stamp
ORDER BY NAME
, YEAR
, DAY;
PLAN_TABLE_OUTPUT
Plan hash value: 3043498213
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 14702 | 990K| | 20550 (3)| 00:04:07 | | |
| 1 | SORT UNIQUE | | 14702 | 990K| 9608K| 19862 (3)| 00:03:59 | | |
|* 2 | VIEW | | 90794 | 6117K| | 18372 (3)| 00:03:41 | | |
| 3 | WINDOW SORT | | 90794 | 13M| 33M| 18372 (3)| 00:03:41 | | |
|* 4 | HASH JOIN | | 90794 | 13M| | 15251 (3)| 00:03:04 | | |
|* 5 | TABLE ACCESS BY INDEX ROWID| SOLAR_STATION | 1 | 78 | | 1 (0)| 00:00:01 | | |
|* 6 | INDEX UNIQUE SCAN | SOLAR_STATION_PK | 1 | | | 0 (0)| 00:00:01 | | |
| 7 | PARTITION RANGE ITERATOR | | 272K| 20M| | 15245 (3)| 00:03:03 | 10 | 11 |
| 8 | PARTITION HASH SINGLE | | 272K| 20M| | 15245 (3)| 00:03:03 | 1 | 1 |
|* 9 | TABLE ACCESS FULL | DATA | 272K| 20M| | 15245 (3)| 00:03:03 | | |
Predicate Information (identified by operation id):
2 - filter("MAX_DATE_STAMP"=TO_TIMESTAMP("DATE_STAMP"))
4 - access("A"."SOLAR_ID"="S"."SOLAR_ID")
5 - filter("S"."CLIMATE_ID"='611KBE0')
6 - access("S"."SOLAR_ID"='636')
9 - filter("A"."TIME_STAMP">=TO_DATE('2004-09-08 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND "YEAR"=2004 AND
"A"."SOLAR_ID"='636' AND "A"."TIME_STAMP"<=TO_DATE('2004-12-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss'))The result was an error:
Error at line 0
ORA-01843: not a valid month -
DB version 10g
a sample table is
my tab
(dt date,
status varchar2(2))
status in ('M','L','H') (medium,low,high)
sample data are: (date in 'dd-mm-yyy hh24Mi:ss' format)
DT STATUS
02-03-2006 08:16:00 H
02-03-2006 08:20:11 H
02-03-2006 08:22:00 H
02-03-2006 08:23:22 H
02-03-2006 08:24:33 M
02-03-2006 08:25:44 H
02-03-2006 08:26:55 L
02-03-2006 14:27:59 L
03-03-2006 00:00:00 L
I'm interested in find duration for every status:
status H
from 02-03-2006 08:16:00 to 02-03-2006 08:24:33 => duration 8 min 33 secs;
from 02-03-2006 08:25:44 to 02-03-2006 08:26:55 => duration 1 min 11 secs;
status M
from 02-03-2006 08:24:33 to 02-03-2006 08:26:55 => duration 1 min 11 secs;
status L
from 02-03-2006 08:26:55 to 03-03-2006 00:00:00 => duration 15 h ....
there are a complex query for this purpose without using a procedure that scan all records to find the chenges of status?Not very elegant:
SQL> select * from my_tab;
DT ST
02-03-2006 08:16:00 H
02-03-2006 08:20:11 H
02-03-2006 08:22:00 H
02-03-2006 08:23:22 H
02-03-2006 08:24:33 M
02-03-2006 08:25:44 H
02-03-2006 08:26:55 L
02-03-2006 14:27:59 L
8 rows selected.
SQL> select status, min(dt), max(ld) from (
2 select status, dt,
3 case when lead(dt) over(order by dt) is null then
4 trunc(dt) + 1 else lead(dt) over(order by dt) end ld,
5 (row_number() over(order by dt) -
6 row_number() over(partition by status order by dt)) gr
7 from my_tab
8 ) group by status, gr
9 /
ST MIN(DT) MAX(LD)
H 02-03-2006 08:16:00 02-03-2006 08:24:33
H 02-03-2006 08:25:44 02-03-2006 08:26:55
L 02-03-2006 08:26:55 03-03-2006 00:00:00
M 02-03-2006 08:24:33 02-03-2006 08:25:44Probably a mistake in M example - if I interpret your question right,
M has to be from 02-03-2006 08:24:33 to the next "H" e.g. 2006 08:25:44 ?
Rgds. -
Querying multiple date ranges in one select
Hello guys,
DDL:
CREATE TABLE DBO.SALES (
TYPE VARCHAR(50)
, REVENUE DECIMAL(19,2)
, SALE_DATE_TIME DATETIME)
What I'm trying to figure out is how I can pass two dates to a where clause querying this table, a start date and an end date. From this I can group up the REVENUE for each TYPE (but there's more):
SELECT TYPE, COUNT(*) AS NBR_OF_SALES, SUM(REVENUE) AS TOTAL_REVENUE
FROM REPORTS.DBO.SALES
WHERE SALE_DATE_TIME BETWEEN @STARTPERIOD AND @ENDPERIOD
GROUP BY TYPE
I'd now like to add a couple of extra columns in the select that calculates the TOTAL_REVENUE equal to the time period of the two dates, but offset by 1 week, 1 month and 1 year, so that would be three columns. I'm guessing this would be possible with a
CROSS APPLY, but I just can't get my head around it at this time. Just for clarity, the expected output would contain these columns:
TYPE, NBR_OF_SALES, TOTAL_REVENUE, TOTAL_REVENUE_LAST_WEEK, TOTAL_REVENUE_LAST_MONTH, TOTAL_REVENUE_LAST_YEAR
Any help is greatly appreciated.SELECT TYPE,
SUM(CASE WHEN SALE_DATE_TIME BETWEEN @STARTPERIOD AND @ENDPERIOD THEN 1 ELSE 0 END) AS NBR_OF_SALES,
SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(WEEK,1,@STARTPERIOD) AND DATEADD(WEEK,1,@ENDPERIOD) THEN 1 ELSE 0 END) AS NBR_OF_SALES_WeekOffset,
SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(MONTH,1,@STARTPERIOD) AND DATEADD(MONTH,1,@ENDPERIOD) THEN 1 ELSE 0 END) AS NBR_OF_SALES_MonthOffset,
SUM(CASE WHEN SALE_DATE_TIME BETWEEN @STARTPERIOD AND @ENDPERIOD THEN REVENUE ELSE 0 END) AS TOTAL_REVENUE,
SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(WEEK,1,@STARTPERIOD) AND DATEADD(WEEK,1,@ENDPERIOD) THEN REVENUE ELSE 0 END) AS TOTAL_REVENUE_WeekOffset,
SUM(CASE WHEN SALE_DATE_TIME BETWEEN DATEADD(MONTH,1,@STARTPERIOD) AND DATEADD(MONTH,1,@ENDPERIOD) THEN REVENUE ELSE 0 END) AS TOTAL_REVENUE_MonthOffset
FROM REPORTS.DBO.SALES
WHERE SALE_DATE_TIME BETWEEN @STARTPERIOD AND DATEADD(MONTH,1,@ENDPERIOD)
GROUP BY TYPE
Is this what you want?
We have three total columns per metric, one for the period, one for the period offset by a week, and one for the period offset by a month.
We've also adjusted the where so all the data for the offset period is included. -
Need an MDX query for Date range
Hi there,
My requirement is to filter sales from 1st of month to until holiday of the month. I am able to use to ":" range function for filtering, but I am not able to filter on holiday date. Becuase both filter and exists are giving set results, I need
member expression to use range function. Please let me know ASAP.
Thanks,
ATRSAMSHi ATRSAMS ,
You can get the first member of the month with the OpeningPeriod function, and item(#) to have a specific member from a set .
So if you want first member from your set : filter(.....).ITEM(0)
Regards, David . -
I have a 2 tables table like this:
Employee Table
emp_id region active_date
A100 01 8/19/2009
A100 01 8/21/2009
A100 01 8/27/2009
A100 01 9/2/2009
A100 01 10/1/2009
A100 01 3/27/2010
Date_Lkp Table
Date_sk Start_dt End_date
1 7/15/2009 8/14/2009
2 8/15/2009 9/14/2009
3 9/15/2009 10/14/2009
4 10/15/2009 11/14/2009
5 11/15/2009 12/14/2009
6 12/15/2009 1/14/2010
7 1/15/2010 2/14/2010
8 2/15/2010 3/14/2010
9 3/15/2010 4/14/2010I need an ouput like this:
emp_id region Start_dt End_date Date_sk
A100 01 8/19/2009 9/14/2009 2
A100 01 9/15/2009 10/14/2009 3
A100 01 3/15/2010 4/14/2010 9Records with SK 1,4-8 are not there because we dont have the employee active during that time. Also pls note that start date of the 1st record in output is same as the employee's Active start date.
Thanks.
Edited by: user572405 on Dec 1, 2010 1:28 PMSo you want Active_Date be selected as the start date for all records ?
select empid, region, case when c > 1 then min(active_date)
else min(start_dt) end as start_dt,
end_dt, date_sk
from (
select empid, region, start_dt, end_dt, date_sk, active_date, count(date_sk) over (partition by empid, region, start_dt, end_dt) c
from (
select 'A100' empid, '01' region, to_date('8/19/2009', 'MM/DD/RRRR') active_date from dual union all
select 'A100', '01', to_date('8/21/2009', 'MM/DD/RRRR') from dual union all
select 'A100', '01', to_date('8/27/2009', 'MM/DD/RRRR') from dual union all
select 'A100', '01', to_date('9/2/2009', 'MM/DD/RRRR') from dual union all
select 'A100', '01', to_date('10/1/2009', 'MM/DD/RRRR') from dual union all
select 'A100', '01', to_date('3/27/2010', 'MM/DD/RRRR') from dual
) m,
select 1 date_sk, to_date('7/15/2009', 'MM/DD/RRRR') start_dt, to_date('8/14/2009', 'MM/DD/RRRR') end_dt from dual union all
select 2, to_date('8/15/2009', 'MM/DD/RRRR'), to_date('9/14/2009', 'MM/DD/RRRR') from dual union all
select 3, to_date('9/15/2009', 'MM/DD/RRRR'), to_date('10/14/2009', 'MM/DD/RRRR') from dual union all
select 4, to_date('10/15/2009', 'MM/DD/RRRR'), to_date('11/14/2009', 'MM/DD/RRRR') from dual union all
select 5, to_date('11/15/2009', 'MM/DD/RRRR'), to_date('12/14/2009', 'MM/DD/RRRR') from dual union all
select 6, to_date('12/15/2009', 'MM/DD/RRRR'), to_date('1/14/2010', 'MM/DD/RRRR') from dual union all
select 7, to_date('1/15/2010', 'MM/DD/RRRR'), to_date('2/14/2010', 'MM/DD/RRRR') from dual union all
select 8, to_date('2/15/2010', 'MM/DD/RRRR'), to_date('3/14/2010', 'MM/DD/RRRR') from dual union all
select 9, to_date('3/15/2010', 'MM/DD/RRRR'), to_date('4/14/2010', 'MM/DD/RRRR') from dual
) d
where active_date between start_dt and end_dt
group by empid, region, end_dt, date_sk, c
order by 3And the result is:
DATE_SK REGION START_DT END_DT DATE_SK
A100 01 8/19/2009 9/14/2009 2
A100 01 9/15/2009 10/14/2009 3
A100 01 3/15/2010 4/14/2010 9Edited by: Zaafran Ahmed on Dec 1, 2010 4:04 PM
Maybe you are looking for
-
I'm having trouble with my Ipod- I tried to restore it but keep getting a message - the ipod could not be restored. an unknown error occurred(1413) Mt computer has been freezing up on me lately and I think it messed up my Ipod when it quit right in t
-
My mac mini keeps going to sleep
Ok my Mac mini is about 3 years old and well whenever I'm doing most anything It will make a noise as if running hard then turn itself on sleep and no Its not doing it due to inactivity cause after I turn it back on using the keyboard it puts itself
-
Error code 1074396120 appers when using IMAQ WriteFile.vi
When this VI is run, the error message 1074396120, "not an image", appears. If the VI is run with highlight button ON, the error message doesn't appear and the images are properly saved. I want to correct this VI. Attachments: 110421grab_and_save.vi
-
I have an iPhone 3gs... The only thing that will come up on my screen is the apple logo with the little loading circle thingy... I have plugged it into iTunes and it updated and synced with no problems but still nothing on the screen... PLEASE HELP!!
-
mail icon not in dock- mail won't send - a drop down small box appears and when a message is typed in to sent the send icon goes grey and a black dot appears on the red dot in top left side of box and the message will be sent to drafts only