August 1, 2007 at 5:14 pm
writing a query that can work back from now() to now()-1min add collect data
I would like to write a query that can work back from the current time to current time - 1 min and than return the total of a field and count the number of records selected
Any help please
August 1, 2007 at 8:27 pm
This looks a lot like homework or an interview question... please post what you've tried and we'll go from there.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 2, 2007 at 7:00 am
select ESDSTotal from DEMANDSPREAD WHERE TIMESTAMP=(SELECT MAX(TIMESTAMP)FROM DEMANDSPREAD) to (SELECT MAX(TIMESTAMP-00:01:00)FROM DEMANDSPREAD)
ESDStotal(int) and Timestamp(datetime) are in my table
thats what i am trying so far to get the time at least
August 2, 2007 at 8:41 am
Maybe:
SELECT SUM(S.ESDS) AS ESDSTotal
,COUNT(S.ESDS) AS ESDSCount
,COUNT(*) AS ESDSCountWithNulls
FROM DEMANDSPREAD S
CROSS JOIN (
SELECT MAX(S1.[TimeStamp]) AS MaxTimeStamp
FROM DEMANDSPREAD S1
) D
WHERE S.[TimeStamp] BETWEEN DATEADD(minute, -1, D.MaxTimeStamp) AND D.MaxTimeStamp
August 2, 2007 at 6:10 pm
Heh... although they both have the same execution plan, I'm not sure why you wrote it as a Cross-Join that works like an Inner Join... why not just write it as the Inner Join that it really is?
SELECT SUM(s.ESDSTotal) AS ESDSTotal,
COUNT(s.ESDSTotal) AS ESDSCount,
COUNT(*) AS ESDSCountWithNulls
FROM dbo.DemandSpread s
INNER JOIN (--==== Derived table "d" finds latest date in DemandSpread
SELECT MAX(s1.[TimeStamp]) AS MaxTimeStamp
FROM dbo.DemandSpread s1) d
ON s.[TimeStamp] BETWEEN DATEADD(mi, -1, d.MaxTimeStamp) AND d.MaxTimeStamp
Jon,
If you want the count for the last minute, we need to make a mod to either the code above or to Ken's good code as follows...
SELECT SUM(s.ESDSTotal) AS ESDSTotal,
COUNT(s.ESDSTotal) AS ESDSCount,
COUNT(*) AS ESDSCountWithNulls
FROM dbo.DemandSpread s
WHERE s.[TimeStamp] BETWEEN DATEADD(mi, -1, GETDATE()) AND GETDATE()
--Jeff Moden
Change is inevitable... Change for the better is not.
August 3, 2007 at 3:11 am
Sorry about the CROSS JOIN. I do try to suppress my urge to put all the join conditions in the WHERE clause!
August 3, 2007 at 7:21 am
LOL... I know what you mean... probably a really bad habit but the only time I normally use the ANSI "Join" statements is when an outer join is present.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 3, 2007 at 4:02 pm
Thank you guys so much, my knowledge on sql is very small right now,you have a been great help, and I dont want to push it, but i am calling this statement from a 4 pc's and have alot of data running thru it, really new to sql maybe using it a month or so. its really great tool so I got less knowledge on it that and need to get it real fast.
I am posting to it ever 2 seconds is there away of making a new field to calculate this information when data arrives? of course I clear the data base every 2-3d ays or so, so there wont be t-1 when i start sending the data from excel, any help or is this the best way?
Is the data being processed whtin the remote PC if so thats fine
Thanks again
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply