How to get records with time close to Minutes = 59 And Second "close" to 59 from BD using EF - entity-framework

Imagine the service that writes temperature samples from sensor into DB each 10 seconds. Now I need to display these temperature values in 24-hrs report displaying only values closest to 59th minute and 59th second, neglecting the rest records stored in DB.
I started with
var query = x.Discretes.AsQueryable();
query = query.Where(dcrts => dcrts.CreationTimestamp >= startTime && dcrts.CreationTimestamp <= endTime);
query = query.Where(hrsDiscretes => hrsDiscretes.CreationTimestamp.Minute == 59);
But don't know how to make EF select the single record with max Second value for each of these Hour's records in the query...

Related

Timestamp where clause that is not working

My query is to return all the records between two timestamp date and times.
This is being used for 30 minute intervals in time. My query example that is failing should return 27 rows of data between 2/1/2019 1:30:00 and 2/1/2019 01:59:00 returns 14582 rows with the date ranges from 2/1/2019 13:04:00 to 2/1/2019 01:59:00.
I have tried using <= and >= for the ranges and have tried using between. I get the same number of records returned wither way.
select "ACD Login ID", "Start"
from prdintfeb where
"Start"::timestamp>=timestamp '2/1/219 01:30:00' and "Start"::timestamp <= timestamp '2/1/2019 01:59:00'
order by "Start"

SSRS Count Records expression with date less than group by date

I want to count records that are not marked as completed with a Received date less than the Received date of the row (group by Received date "Details") This will be the Start of Day column showing how many records are in queue.
I have a tablix in VS 2017 SSDT.
Tablix is grouped by Received Date
COLUMNS
Received Date (group by Details) another column same field (Textbox5)
Start of Day
New Tasks
Completed
I'm having an issue with the code logic for Start of Day column field.
I want to count records that are not marked as completed with a Received date less than the Received date of the row (group by Received date "Details")
This code works for New Tasks column.
=COUNT(IIF(Fields!Received.Value < Fields!Received.Value AND
Fields!Completed.Value = "NO", 1,0),"Details")
When I attempt the Start of Day expression I get errors. Textbox5 is the same dataset field used in group by field (Received). I added it to test different approach.
=COUNT(IIF(Fields!Received.Value < ReportItems!Textbox5.Value AND
Fields!Completed.Value = "NO", 1,0),"Details")
Error: rsAggregateReportItemlnBody aggregate functions can be used
only on report items contained in page headers and footers.
Sample data and expected output for Start of Day Column: it should count records in the group by row if they were in queue prior to start of day (yesterday).
You can use running value to calculate the total. To get the prior date total you subtract the group total value
Start Of Day
= RunningValue( 1, SUM, "Tablix1")- SUM( 1)
New Task
= SUM(1) or COUNT(1)
Complete
= RunningValue( Iif(Fields!CompYN.Value = "YES",1,0), SUM, "Tablix1")- SUM( Iif(Fields!CompYN.Value = "YES",1,0))
New Tasks column: =COUNT(IIF(Fields!Received.Value = Fields!Received.Value AND Fields!Completed.Value = "NO", 1,0),"Details")
Start of day column: =COUNT(IIF(Fields!Received.Value < Fields!Received.Value AND Fields!Completed.Value = "NO", 1,0),"Details")

How do I group records by epoch time intervals in Hive table?

I have time stamp column values in epoch ( ex. min value = 1276570880, max value = 1276657260). How do I group records in my Hive table based on 30 min intervals.
I need count a value for every 30 min starting from the min time stamp value until the max time stamp value in the time stamp column.
I have tried the following query, but it has not resulted any results.
SELECT COUNT(method) AS mycount, FROM_UNIXTIME(floor(UNIX_TIMESTAMP(ts)/1800)*1800)
FROM http
WHERE ts >= '2010-06-14 20:01:20'
AND ts <= '2010-06-14 22:01:20'
AND method='GET'
GROUP
BY FROM_UNIXTIME(
floor(UNIX_TIMESTAMP(ts)/1800)*1800)
This should work. Using round on the timestamps is important for the grouping to work correctly. Here is a SQLfiddle example which shows your specific example.
select count(method) as mycount,
from_unixtime(round(unix_timestamp(ts))
from http
where ts >= '2010-06-14 20:01:20'
and ts <= '2010-06-14 22:01:20'
and method='GET'
group by round(ts/1800)

Aggregating date data with entity framework grouped by day, month, qtr, year, etc

I have a table that records activities. All activities have an activitydate. I want to count how many activities for a given period of time (day, month, qtr, etc.). I want to include all dates even those that have zero activities. I could do this in the Data Tier with a DateDimension table where the date table has a single column called day containing one row for each calendar day and a outer join, group by query:
DateDimension Table
| Day |
|1/1/2013 00:00:00 |
|1/1/2013 00:00:00 |
|1/1/2013 00:00:00 |
Query
SELECT CAST(Day AS DATE), COUNT() AS CountOfActivities
FROM DateDimension dd LEFT OUTER JOIN Activities a
ON CAST(dd.Day AS DATE) = CAST(a.ActivityDate AS DATE)
WHERE Day BETWEEN MyStartDate AND MyEndDate
GROUP BY CAST(Day AS DATE)
ORDER BY CAST(Day AS DATE)
I'm using EntityFramework so I'd like to execute this query using Linq. The DateDimension table has no business value residing in the database. It exists only to support these aggregate queries by providing a list of dates so I can ensure a row is returned if no activities exist for a given day.
I have the idea that I could manufacture a list of days in memory and weave them in to the results of a much simpler database query at runtime. By perhaps Concatenating the results from 2 IEnumerables - 1 from the in memory enemurable of dates and the other from the database results. How could I do that? Should I do that?
How about something like this:
Example date range:
var from = DateTime.Today.AddDays(-30);
var to = DateTime.Today;
Dictionary to hold your tally of activities per day:
var activityCounts = new Dictionary<DateTime, int>();
Seed with a zero count for each day in the range (this is equivalent to setting up your date dimensions table):
Enumerable.Range(0, (to - from).Days + 1)
.ToList()
.ForEach(x => activityCounts[from.AddDays(x)] = 0);
Add in the real activity counts for each day in the range:
context.Activities.Where(a => a.DateTime >= from && a.DateTime <= to)
.GroupBy(a => a.DateTime)
.ToList()
.ForEach(x => activityCounts[x.Key] = x.Count());
In this way, you only hit the database for the aggregation of activities for dates with activities. The padding out of the resultset with contiguous dates within the date range is then performed on the app server.
Just need to be careful how your dates are stored in the database. This code example is expecting to be able to match keys in the activity dictionary based on the the format of the calls to DateTime.Today. You will need to shape your dates in your database query accordingly.

get minutes and seconds from query in postgresql

I need a time in minutes and seconds. If minutes is greater than 60 I then add to minutes, but do not convert hours
Query :
TO_CHAR((sum(seconds)|| ' second')::interval,'HH24:MI:SS')
as duration from table;
Output:
DURATION
02:50:21
Required Output:
DURATION
170:21
even i have tried another query (without HH24) but i get below out put:
Query :
TO_CHAR((sum(seconds)|| ' second')::interval,'MI:SS') as duration from table;
Output:
DURATION
50:21
Here we can see from 1st query output 02 hrs means 120 minutes + 50minutes =170 minutes and seconds same as it is.
is it possible directly getting from query or not?
you don't need to_char here:
select (sum(seconds) / 60)::text || ':' || (sum(seconds) % 60) ...;