Find totals of a date range and sum
How do I find totals between a date range like from 0 up to 60 days from today, 61 to 90 days from today, 91-120 days, 120 and above. Please advise.
Do I use a Add_Months function to convert to days?
Try this,
This may help you
SQL> select sysdate,sysdate+interval '60' day from dual;
SYSDATE SYSDATE+INTERVAL'60'DAY
5/5/2008 2: 7/4/2008 2:17:16 PM
SQL>G.
Similar Messages
-
Difficulty working with overlapping date ranges and sums
I have data like the following and need help querying it for a specific result:
startdate enddate place class number
03-30-2006 04-18-2006 A 22 2000
04-01-2006 04-30-2006 A 22 6000
03-30-2006 04-15-2006 B 11 1700
03-15-2006 04-12-2006 B 11 2900
I'd like to return the sum of the averages for any given day, by place, and by class, like this:
day place class average
03-15-2006 B 11 100
03-16-2006 B 11 100
03-30-2006 A 22 100
03-30-2006 B 11 200
03-31-2006 A 22 100
03-31-2006 B 11 200
04-1-2006 A 22 300
04-01-2006 B 11 200
04-30-2006 A 11 200
I'm stuck, any help would be GREATLY appreciated.307b218a-cebe-46f1-87b6-946b4076ce79 wrote:
Here is what I said in my original post:
I'd like to return the sum of the averages for any given day, by place, and by class.....Then gave an example of output.
I think that sentence describes the relations perfectly succinctly.
How can it, when there's no SUM involved
307b218a-cebe-46f1-87b6-946b4076ce79 wrote:
For the date of 3-30-2006, the sum of the averages for region A in class 22 is 100, since from 3-30-2006 to 4-18-2006 is 20 days, and 2000/20 = 100, and there are no other class 22's in place A in that date range. Same goes for 3-31-20006.
However for 4-1-2006 the sum of the averages is 300, since I have one record who's average is 100 (2000/20), and another (6000/30 = 200) and 200 + 100 = 300.
For 4/30, I have one average in that range for place A class 22, and its average for that period is 200.
Does that help?
Much better, thank-you ..
try this:
WITH data AS (
SELECT TO_DATE('03-30-2006','mm-dd-yyyy') startdate, TO_DATE('04-18-2006','mm-dd-yyyy') enddate, 'A' place, 22 class, 2000 cnumber FROM DUAL UNION ALL
SELECT TO_DATE('04-01-2006','mm-dd-yyyy') startdate, TO_DATE('04-30-2006','mm-dd-yyyy') enddate, 'A' place, 22 class, 6000 cnumber FROM DUAL UNION ALL
SELECT TO_DATE('03-30-2006','mm-dd-yyyy') startdate, TO_DATE('04-15-2006','mm-dd-yyyy') enddate, 'B' place, 11 class, 1700 cnumber FROM DUAL UNION ALL
SELECT TO_DATE('03-15-2006','mm-dd-yyyy') startdate, TO_DATE('04-12-2006','mm-dd-yyyy') enddate, 'B' place, 11 class, 2900 cnumber FROM DUAL ),
data2 AS ( SELECT startdate, enddate, enddate-startdate+1 numdays, place, class, cnumber
FROM data d
SELECT startdate+x.lvl day, place, class, cnumber/numdays avg
FROM data2 d,
( SELECT level lvl FROM dual CONNECT BY level <= 10000 ) x
WHERE x.lvl <= numdays
ORDER BY d.startdate, x.lvl
It helps to be specific and detailed -
Mailbox Export - Filtered for Date Range and Body Content
I have been pulling my hair out with this one and hopefully someone can help.
I need to search a mailbox and export all (sent & received) messages that fit in a date range and also have a specific word in the body. The query I am using doesn't seem to find the term at all, but the messages it does find fit within my date range,
so part of it seems to work. The request was to not use wildcards too, only looking for the specific word in the body. Here is my one-liner...
New-MailboxExportRequest -ContentFilter {(Body -like "Something") -and (Received -gt "07/01/2012") -and (Received -lt "07/31/2012") -or (Sent -gt "07/01/2012") -and (Sent -lt "07/31/2012")} -Mailbox "SearchMailbox"
-Name "Label1" -FilePath \\server\temp\pst\Export.pst
I've also tried to search for a common word like "the" just to make sure it wasn't a case of the term simply not being in mailbox. No dice.Hi,
Agree with Nickeyclayton, you can use (body -like "*something*") instead of (body -like "something").
I recommend you refer to the following article:
Filterable Properties for the -ContentFilter Parameter
Property
Description
Values
Example syntax
Body
This property returns messages that have the specified string within the message body.
String
Wildcard
-ContentFilter {Body -like "*prospectus*"}
Hope this helps!
Thanks.
Niko Cheng
TechNet Community Support -
Dear all,
I am very new to Sharepoint designer 2010. Its better to say, i am just stepping in.
I have sharepoint site which is accessible for all the users to update few information. But i want this site not to be accessible during a date range, say from 15th to 20th of every month.
I am planning to have something like this: If any user tries to access this sharepoint site between 15th to 20th date range of everymonth, then the browser must show an pop-up alert saying "This site is not accessible during this date range" and Redirect
this sharepoint site to someother site (Say http:// somesitename.xx.com) immidiately.
I know very little about how to add Javascript in sharepoint webpart.
Please provide the Javascript or any best alternate solution to my concern.
Thanks in advance.Hi,
From your description, my understanding is that you want to redirect sharepoint site to another site between 15th and 20th every month.
I agree with what Sudip says. If you still want to use JS code,You could try these steps
below:
Open your site with SharePoint Designer.
Click Master Pages in left navigation.
Find file v4.master, and backup it(it is very important).
Right click your v4.master,
choose "Edit File in Adavance Mode".
Add the code below into the <head/> tag.
<script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script><script
type="text/javascript">
$(document).ready(function(){
var today = new Date();
var day = today.getDate(); // get current day
var id = _spPageContextInfo.userid; // get the id of current logon in user
var boolFlg = false;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/SiteGroups/GetByName(\'"+"sharepoint Owners"+"\')/Users", //get users in a specified group can access the sharepoint site from 15th to 20th every
month
type: "GET",
headers: {"accept": "application/json;odata=verbose"},
success: function (data) {
if (data.d.results) {
var src = data.d.results;
for(var i = 0; i < src.length; i++){
if(src[i].userid==id){ //check the logon in user in the specified group or not
boolFlg = false;
break;
if(!boolFlg){
if(day <= 20 && day >=15){
alert("This site is not accessible during this date range"); // alert the message
location.href = "http:// somesitename.xx.com"; // redirect to another site
error: function (xhr) {
alert(xhr.status + ': ' + xhr.statusText);
})</script>
Best Regards
Vincent Han
TechNet Community Support -
Moving multiple files by date range and/or file name filtered
I need to move multiple files by date range and/or by filename (using a filter) to another directory.
OR
How can I get the attributes of files in a directory, something you would see when typing in "dir" in a DOS or ls in UNIX. I can parse this info and then make a array of files to move.
The reason for needing this is that I need to move files that are located in another country, and sort them into different directories based on date. Having to read each file for it's file information adds wasted time that I can't afford. I am about to create a dos terminal to do a "dir" command to get the information and parse it out.. but I was hoping LV had a function that can get me that info so I don't have build it.
THanks.You should use the 'File/Directory Info' functionality.
This will return the last modificatoin of the file.
Ton
Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas
LabVIEW, programming like it should be! -
Find intersections in date ranges and concatenate aggregated labels
Hello All,
The original source of data will be a table of persons with their member ship:
Description StartDate EndDate
18 Smith John Poker Club 01/01/2009 NULL
18 Smith John Library 05/01/2009 18/01/2009
18 Smith John Gym 10/01/2009 28/01/2009
26 Adams Jane Pilates 03/01/2009 16/02/2009
The challenge will be to find each intersection of membership date ranges for each person and produce for this each unique range:
a new line with the StartDate and EndDate of the range
Concatenate in the description the descriptions of the memberships for this range with:
“/” as separator
order by membership StartDate
This is the table of results expected:
18 Smith John Poker Club 01/01/2009 04/01/2009
18 Smith John Poker Club / Library 05/01/2009 09/01/2009
18 Smith John Poker Club / Library / Gym 10/01/2009 18/01/2009
18 Smith John Poker Club / Gym 19/01/2009 28/01/2009
18 Smith John Poker Club 29/01/2009 NULL
26 Adams Jane Pilates 03/01/2009 16/02/2009
Sample Script:
SET DATEFORMAT dmy
DECLARE @Membership TABLE (
PersonID int,
Surname nvarchar(16),
FirstName nvarchar(16),
Description nvarchar(16),
StartDate datetime,
EndDate datetime)
INSERT INTO @Membership VALUES (18, 'Smith', 'John',
'Poker Club', '01/01/2009', NULL)
INSERT INTO @Membership VALUES (18, 'Smith', 'John',
'Library', '05/01/2009', '18/01/2009')
INSERT INTO @Membership VALUES (18, 'Smith', 'John',
'Gym', '10/01/2009', '28/01/2009')
INSERT INTO @Membership VALUES (26, 'Adams', 'Jane',
'Pilates', '03/01/2009', '16/02/2009')
Note:Make sure that your solution uses a single query that starts either with "SELECT" or "WITH" or ";WITH".
Can you please share suggestions.
Regards,
VaishuThis query
;WITH RS AS -- RangeStart
SELECT PersonID, StartDate AS RangeStart
FROM @Membership
UNION
SELECT PersonID, DATEADD(day, 1, EndDate) AS RangeStart
FROM @Membership
WHERE EndDate IS NOT NULL
RE AS -- RangeEnd
SELECT PersonID, DATEADD(day, -1, StartDate) AS RangeEnd
FROM @Membership
WHERE StartDate IS NOT NULL
UNION
SELECT PersonID, EndDate AS RangeEnd
FROM
@Membership
R AS -- Ranges
SELECT RS.PersonID, RS.RangeStart, RE.RangeEnd
FROM
RS CROSS APPLY
SELECT TOP (1) RE.RangeEnd
FROM RE
WHERE
RE.PersonID = RS.PersonID
AND (
RE.RangeEnd >= RS.RangeStart
OR RS.RangeStart IS NOT NULL AND RE.RangeEnd IS NULL
ORDER BY
ISNULL(RE.RangeEnd, '99991231')
) RE
SELECT R.*,
STUFF( (
SELECT '/' + m.Description
FROM
@Membership m
WHERE
m.PersonID = R.PersonID
AND (m.StartDate <= R.RangeEnd OR R.RangeEnd IS NULL) AND (m.EndDate >= R.RangeStart OR m.EndDate IS NULL)
FOR XML PATH('')
1, 1, '') AS Description
FROM
R
Returns this result:
PersonID
RangeStart
RangeEnd
Description
18
2009-01-01
2009-01-04
Poker Club
18
2009-01-05
2009-01-09
Poker Club/Library
18
2009-01-10
2009-01-18
Poker Club/Library/Gym
18
2009-01-19
2009-01-28
Poker Club/Gym
18
2009-01-29
NULL
Poker Club
26
2009-01-03
2009-02-16
Pilates
EntityLite: A Lightweight, Database First, Micro ORM -
SUMIFS with Date Range and Condition
Hi, I have search in the forums and read the online numbers guide but can't seem to get the formular working in Numbers.
I want to sum up a column of durations, if they are in between 2 dates and meet the condition to belong to one project. I have the following table:
project
total duration
project
start
end
duration
date range start
date range end
one
0
one
02.01.2013
02.01.2013
15:30
01.01.2013
31.01.2013
two
0
one
03.01.2013
03.01.2013
15:30
three
two
04.01.2013
04.01.2013
15:30
four
two
05.01.2013
05.01.2013
15:30
five
three
06.01.2013
06.01.2013
15:30
six
three
07.01.2013
07.01.2013
15:30
seven
three
08.01.2013
08.01.2013
15:30
I have the following formular but it does not give me any value: =SUMIFS(F,C,A5,D"≥"$G$2,E"≤"$H$2)
What am I doing wrong
Thank you for your help.Hi Barry,
thank you very much for your detailed reply.
The first difference I spot is that my duration is in hours:minutes 00:00 but this should not make a difference, right? I attach you here some sample data:
Project
Start date
Start time
End date
End time
Duration
Project 1
07.01.2013
11:00
07.01.2013
11:42
00:42
PINC: 1
07.01.2013
16:11
07.01.2013
17:11
01:00
Theatro
07.01.2013
19:50
07.01.2013
19:54
00:03
Project 3
08.01.2013
11:19
08.01.2013
13:30
02:11
Project 1
09.01.2013
12:32
09.01.2013
13:02
00:30
PINC: 1
09.01.2013
17:55
09.01.2013
18:29
00:34
Project 3
09.01.2013
18:29
09.01.2013
19:36
01:06
And I will go through my formular again and check if everything is correct.
Many greets, Andreas -
Support date range and as of date in Universe
Hi,
I have some questions on Universe design best practices. Can you please help ?
We are using BO4 SP7 and UDT for universe design.
Little bit on our schema: snow flake schema with multiple fact tables. Most of our table are 'Pure Type 6' dimension tables in which start date and end date are used to track historical data. We also have date dimension table. We want to support both 'As of date' and custom 'date range' type reporting.
Out typical use cases are like
1. All items As of date
2. All items between dates etc.
How can we model it in the Universe ?
1. We can not join every dimension with date_d dimension. So we have decided to put default conditions at Folder level. Our typical date conditions as below.
date_d.end_full_date_utc between diminetion1_d.start_date AND diminetion1_d.end_date
But problem is, table condition are not always applied. If we have A, B and C folders and if I create report by using objects from A and C then non of the condition applied to B appears in the query. This is problem for us.
2. Other approach we thought is to define start and end prompt and define a restriction condition for each and every table.
diminetion1_d.start_date > [start_date_prompt] AND diminetion1_d.end_date < [end_date_prompt]
We thought it will be too munch work as we have more than 150 tables and also 2 prompts will always appear, even of as of date, which may not look good.
3. Is there a way apply to apply these date conditions at the individual table level so that they will appear in Query when ever the table is involved in the join path ?
3. What is the best way to handle this kind of requirements, please suggest ?
ThanksHi,
I have some questions on Universe design best practices. Can you please help ?
We are using BO4 SP7 and UDT for universe design.
Little bit on our schema: snow flake schema with multiple fact tables. Most of our table are 'Pure Type 6' dimension tables in which start date and end date are used to track historical data. We also have date dimension table. We want to support both 'As of date' and custom 'date range' type reporting.
Out typical use cases are like
1. All items As of date
2. All items between dates etc.
How can we model it in the Universe ?
1. We can not join every dimension with date_d dimension. So we have decided to put default conditions at Folder level. Our typical date conditions as below.
date_d.end_full_date_utc between diminetion1_d.start_date AND diminetion1_d.end_date
But problem is, table condition are not always applied. If we have A, B and C folders and if I create report by using objects from A and C then non of the condition applied to B appears in the query. This is problem for us.
2. Other approach we thought is to define start and end prompt and define a restriction condition for each and every table.
diminetion1_d.start_date > [start_date_prompt] AND diminetion1_d.end_date < [end_date_prompt]
We thought it will be too munch work as we have more than 150 tables and also 2 prompts will always appear, even of as of date, which may not look good.
3. Is there a way apply to apply these date conditions at the individual table level so that they will appear in Query when ever the table is involved in the join path ?
3. What is the best way to handle this kind of requirements, please suggest ?
Thanks -
Post Author: cht104
CA Forum: Formula
I'm new to Crystal Reports so please bear with me. I know this is easy and I'm just overlooking something obvious. I've got my report formatted and working except for the getting the date range or prompt to work and the format of the date itself. In my db, I"ve got a business_date field. I've got that showing line by line in the report as yyyymmdd, but would like to be formatted as "Sept 11, 2007" if possible. Also, for the date range, do I create a new formula or a new parameter field? How exactly is that written out? What other fields do I need to make this work?
ThanksPost Author: cht104
CA Forum: Formula
{svc_at_dtl.svan} in ["6036281297496447218", "6036281364474453041", "6036281475774461110", "6036282939574448316", "6036283115974448365", "6036283168796454200", "6036283395374462621", "6036283428674464130", "6036283753674445017", "6036283917774448418", "6036284153974449933", "6036284173996439566", "6036284257574445663", "6036284361474459493", "6036284371896470723", "6036284564874451909", "6036284758774459807", "6036284826974450872", "6036285282174468325", "6036285293874448033", "6036285529874453024", "6036285878174449855", "6036285999874448376", "6036286146296452491", "6036286186574458053", "6036286295596460760", "6036286335874446410", "6036286646674450703", "6036286674874445497", "6036286684874459826", "6036286879696436611", "6036286931974449860", "6036287191974445657", "6036287211774459063", "6036287254874446061", "6036287383874459108", "6036287395174475302", "6036287623696439776", "6036287911274449966", "6036287957774450009", "6036287972296473818", "6036289199196440994", "6036289336796478839", "6036289461796466359", "6036289792974471912", "6036289991196439695", "6275291111939468538", "6275291157455722999", "6275291342555724487", "6275291349939479559", "6275291437655724303", "6275291762455755275", "6275291867155724609", "6275291881655723946", "6275291886155724011", "6275292136155723957", "6275292195855724215", "6275292343855800229", "6275292363155724577", "6275292365155724637", "6275292431555731180", "6275292566755724381", "6275292632355723225", "6275292782455724172", "6275292851255723933", "6275292878655723921", "6275292953555723341", "6275293164155723919", "6275293169955723998", "6275293186455724649", "6275293318855723908", "6275293356655724478", "6275293373655724684", "6275293393739479064", "6275293553155724455", "6275293563655724004", "6275293586555778116", "6275293685955724201", "6275293757339475631", "6275293831955724141", "6275293834955730584", "6275293839562464447", "6275293884455723803", "6275293891639475878", "6275294142955724627", "6275294225939469203", "6275294244755724273", "6275294288755724162", "6275294326839466482", "6275294372355724667", "6275294446455724442", "6275294544655723239", "6275294595362458066", "6275294789855747474", "6275294965255727665", "6275295235155724432", "6275295284562503051", "6275295464555724584", "6275295543955724533", "6275295627755723584", "6275295787355723841", "6275295794955723386", "6275295812555723983", "6275295843555791857", "6275295862755722759", "6275295886755724259", "6275295914155723433", "6275295926255723304", "6275296134855724618", "6275296221455724401", "6275296317755723398", "6275296367355723312", "6275296369339471746", "6275296395339476301", "6275296663855724390", "6275296685255723246", "6275296714755724469", "6275296726455767720", "6275296746555724281", "6275296765355724086", "6275296772362502797", "6275296786862497240", "6275296997255724379", "6275297253355723475", "6275297266655724182", "6275297333955724555", "6275297497155723555", "6275297524455723179", "6275297573255724675", "6275297577455724220", "6275297741455812692", "6275297792755724410", "6275297869455724110", "6275297869455724110", "6275297899755723655", "6275297937155723570", "6275298123755730444", "6275298141555724499", "6275298269755791964", "6275298321455723972", "6275298361655724339", "6275298483355724658", "6275298658455722739", "6275298713155724108", "6275298747639512868", "6275298786555723464", "6275298854355724511", "6275298949755724320", "6275298993755724244", "6275299172655724547", "6275299224655724095", "6275299261439479425", "6275299271562505976", "6275299287155767711", "6275299417555724234", "6275299445555724363", "6275299463755727679", "6275299497755724052", "6275299561855724508", "6275299721255723338", "6275299743555724319", "6275299745855724293", "6275299755255769069", "6275299792555723668"]anddate(tonumber({svc_at_dtl.business_date}[1 to 4]), tonumber({svc_at_dtl.business_date}[5 to 6]), tonumber({svc_at_dtl.business_date}[7 to 8])) in {?date_range}
I dont' know if my problem is with the "and", running 2 different things in here or just the date prompt. Without the date prompt, I get my number information correctly -
How to find the last update date time and user of record field peoplecode
how to find the last update date time record field peoplecode?
Thank you.One can check the last update date time using the following query
SELECT LASTUPDDTTM FROM PSPCMPROG WHERE OBJECTVALUE1 LIKE 'RECNAME' AND OBJECTVALUE2 LIKE 'FIELDNAME' -
Restrict Query Data based on Date range and Users
Hi All,
I have a few web reports that I need do restrict data based on Users.
In all the queries i have an infoobject 0CALDAY, and a User Entry range variable on it. Because of performance issues I need to restrict the range of dates a User can see. Typically most of users could go to a max date range of 1 month back. But some others would need the ability to see data for much bigger range of dates.
Pls suggest how should i go about with this. Should I have to enforce this at Variable level(user exit).... but then i might have to maintain a table for the users.. Is there any other way of doing it.
thanks
RajAny thoughts ?
-
Defining Date range and using the same range to search the documents.
Hi Sap Gurus,
in our business there are some documents which do have Validity period, like say Inspection certificates and valid for say 10 months, ex.. JAN 25th 2009 to Oct 24th 2009
we store this certificate in DMS and link to some SAP object say an Asset,
we have a requirement to list all the documents based on the expiry date range. say to get a list of documents which are about to expire in 15 days, or the list of documents which have already expired
I tried to define a characteristic in date format, but it is a single value field, and search is only supported on that specific value only.
like we cannot serch the DIR falling in between this Date Ranges.
also report of Documents which are to expire in 15 days could not be got.
is there a way to meet this requirement , if it is through a development also please tell me how the logic could be built
Thanks and regards
Priya SHi SAP Gurus,
Thanks much Thomas for your Promt reply.
The solution provided by Thomas really works but the same thing is not possible in EDMS,
in Easy DMS we cannot manually give any value in the date characteristics field, the system will ask to fill from the date window format.
so we cannot enter < or > <> value in characteristics, hope there is any note or other procedure to get the same funtionality.
please if any one worked on the same with EDMS let me know
Thanks and kind regards
Priya S -
Finding duplicates within a date range. SQL help please!!
I have a table of records and I am trying to query the duplicate emails that appear within a given date range but cant figure it out.
There records that it returns are not all duplicates withing the given date range. HELP!!
Here is my query.
Thanks in advance.
SELECT cybTrans.email, cybTrans.trans_id, cybTrans.product_number, cybTrans.*
FROM cybTrans
WHERE (((cybTrans.email) In (SELECT [email] FROM [cybTrans] As Tmp GROUP BY [email] HAVING Count(*)>1 ))
AND ((cybTrans.product_number)='27')
AND ((cybTrans.appsystemtime)>'03-01-2010')
AND ((cybTrans.appsystemtime)<'03-05-2010')
ORDER BY cybTrans.email;Yet another method...
<cfset start_date = DateFormat('01/01/2007',
'mm/dd/yyyy')>
<cfset end_date = DateFormat('09/30/2009',
'mm/dd/yyyy')>
<cfset start_year = DatePart('yyyy', start_date)>
<cfset end_year = DatePart('yyyy', end_date)>
<cfset schoolyear_start = '09/01/'>
<cfset schoolyear_end = '06/30/'>
<cfset count = 0>
<cfloop index="rec" from="#start_year#"
to="#end_year#">
<cfset tmp_start = DateFormat('#schoolyear_start##rec#',
'mm/dd/yyyy')>
<cfset tmp_end = DateFormat('#schoolyear_end##rec + 1#',
'mm/dd/yyyy')>
<cfif DateCompare(tmp_start,start_date) gt -1 and
DateCompare(tmp_end, end_date) eq -1>
<cfset count = count + 1>
</cfif>
</cfloop>
<cfoutput>
<br>There are #count# school year periods between
#start_date# and #end_date#
</cfoutput> -
To find quarters in given date ranges
Hi All,
I need help on this..
I need to display the quarter dates in a given date ranges.i.e the start date is 06-nov-2008 and the last date is 12-dec-2009.Hi,
Something like:
WITH d AS
SELECT TRUNC (TO_DATE (:from_dt_var, 'DD-mon-YYYY'), 'Q') AS from_dt
, TRUNC (TO_DATE (:to_dt_var, 'DD-mon-YYYY'), 'Q') AS to_dt
FROM dual
SELECT ADD_MONTHS (from_dt, 3 * (LEVEL - 1)) AS dt
FROM d
CONNECT BY LEVEL <= 1 + (MONTHS_BETWEEN (to_dt, from_dt) / 3);where :from_ and :to_dt_var are strings like '06-nov-2008'.
Edited by: Frank Kulash on Nov 6, 2008 9:52 AM
Added 1 to CONNECT BY condition. -
Min,Max for a time Range and Sum of Records for the last date/time
Hi All,
I have a table with the following structure/data:
create table Events (
[EventID]
int NOT NULL,
[Title]
nvarchar(200) NOT NULL,
[SourceName] nvarchar(20) NOT NULL,
[Type]
int NOT NULL,
[eDate]
datetime
insert into Events values(100, 'Event 1', 'S01', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S07', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S08', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S09', 3,'2014-01-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S010', 3,'2014-01-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S03', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S04', 3,'2014-01-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S05', 3,'2014-01-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S06', 3,'2014-01-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S01', 3,'2014-02-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S02', 3,'2014-02-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S03', 3,'2014-02-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S04', 3,'2014-02-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S05', 3,'2014-02-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S06', 3,'2014-02-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S01', 3,'2014-03-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S02', 3,'2014-03-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S03', 3,'2014-03-01 00:00:00.000')
insert into Events values(100, 'Event 1', 'S04', 3,'2014-03-01 00:00:00.000')
insert into Events values(101, 'Event 2', 'S05', 3,'2014-03-01 00:00:00.000')
insert into Events values(102, 'Event 3', 'S06', 3,'2014-03-01 00:00:00.000')
And I wrote the following query:
select EventID as [Event ID],
Title,
count(distinct(SourceName)) as [Instances],
Type,
min(eDate) as [First Detected],
max(eDate) as [Last Detected],
datediff(d,min(eDate),max(eDate)) as [Delta (days)]
from Events
where type = 3
group by EventID, Title, Type
having max(eDate) <> min(eDate)
and max(eDate) =(select top 1 eDate from Events order by eDate desc)
and I get the following results (see the instance number)
Event ID Title Instances Type First Detected Last Detected
Delta (days)
=============================================================================================================================
100 Event 1 5 3 2014-01-01 00:00:00.000 2014-03-01 00:00:00.000
59
101 Event 2
3 3 2014-01-01 00:00:00.000 2014-03-01 00:00:00.000 59
102 Event 3
2 3 2014-01-01 00:00:00.000 2014-03-01 00:00:00.000 59
This is normal for this query however what I need to do is a little different.
In other words, while I need to show when we recorded a specific event first and last time,
I need to display the results for the last date/time when it was recorded.
For example what I need to provide should look like this:
Event ID Title Instances Type First Detected
Last Detected Delta (days)
=============================================================================================================================
100 Event 1 2 3
2014-01-01 00:00:00.000 2014-03-01 00:00:00.000 59
101 Event 2 2 3
2014-01-01 00:00:00.000 2014-03-01 00:00:00.000 59
102 Event 3
2 3 2014-01-01 00:00:00.000 2014-03-01 00:00:00.000
59
Could you please help me to fix this query?
TIA,
John;With cte As
(Select EventID as [Event ID],
Title,
SourceName,
Type,
min(eDate) Over(Partition By EventID, Title, Type) as [First Detected],
max(eDate) Over(Partition By EventID, Title, Type) as [Last Detected],
eDate,
datediff(d,min(eDate) Over(Partition By EventID, Title, Type),max(eDate) Over(Partition By EventID, Title, Type)) as [Delta (days)],
max(eDate) Over() As MaxEDate
from Events
where type = 3)
Select [Event ID],
Title,
COUNT(Distinct SourceName) As Instances,
Type,
[First Detected],
[Last Detected],
[Delta (days)]
From cte
Where eDate = MaxEDate And [First Detected] <> [Last Detected]
Group By [Event ID],
Title,
Type,
[First Detected],
[Last Detected],
[Delta (days)];
Tom
P.S. Thanks for providing the DDL and data. That is always very helpful.
Maybe you are looking for
-
Error -42408 when open Itunes 10.7
is made it everytime I open it, but everything seem to work great. Someone can help me ? osx 10.6.8
-
Can anybody tell what are the 13 default packages in JSP. Thanks in advance!!!
-
Any way of getting hold of InCopy CS4?
I work for a small publishing department within a much larger institution with no IT budget and no way of upgrading to intel macs so that we can upgrade to CS5 (unless someone wins the lottery). We produce corporate reports to very tight deadlines an
-
How to compile your flex application for Iphone Using Adobe Flash CS5?
How to compile your flex application for Iphone Using Adobe Flash CS5?
-
AE CS5 Help application damaged, won't launch
Installed CS5 last week, on a Mac Book Pro OSX10.6, also with CS3 installed (Master collection) installed. Thought I'd try to get up to speed but when I try to access the AE help files, I get a message that says: Adobe AIR: The installation of this a