Timestamp where clause that is not working - postgresql

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"

Related

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.

Insert Column with value (#days) between dates in other columns

I have two columns; CommencementDate and ExpectedCompletionDate. I would like to insert a column (Days) which gives the number of days between the two date columns in my table.
I'm not sure where to start. I'm on day 5 of writing queries!
You just have to substract ExpectedCompletionDate and CommencementDate and return the days of that.
SELECT DATEDIFF(Day, ExpectedCompletionDate, CommencementDate);
Also you can return the absolute value to ensure this is a positive value.
SELECT ABS(DATEDIFF(Day, ExpectedCompletionDate, CommencementDate));
You can see the documentation in microsft
http://technet.microsoft.com/en-us/library/ms189794.aspx -> Datediff
http://technet.microsoft.com/en-us/library/ms189800.aspx -> ABS

Postgresql Query very slow with ::date, ::time, and interval

I have a sql query that is very slow:
select number1 from mytable
where symbol = 25
and timeframe = 1
and date::date = '2008-02-05'
and date::time='10:40:00' + INTERVAL '30 minutes'
The goal is to return one value, and postgresql takes 1.7 seconds to return the desired value(always a single value). I need to execute hundreds of those queries for one task, so this gets extremely slow.
Executing the same query, but pointing to the time directly without using interval and ::date, ::time takes only 17ms:
select number1 from mytable
where symbol = 25
and timeframe = 1
and date = '2008-02-05 11:10:00'
I thought it would be faster if I would not use ::date and ::time, but when I execute a query like:
select number1 from mytable
where symbol = 25
and timeframe = 1
and date = '2008-02-05 10:40:00' + interval '30 minutes'
I get a sql error (22007). I've experimented with different variations but I couldn't get interval to work without using ::date and ::time. Date/Time Functions on postgresql.org didn't help me out.
The table got a multi column index on symbol, timeframe, date.
Is there a fast way to execute the query with adding time, or a working syntax with interval where I do not have to use ::date and ::time? Or do I need to have a special index when using queries like these?
Postgresql version is 9.2.
Edit:
The format of the table is:
date = timestamp with time zone,
symbol, timeframe = numeric.
Edit 2:
Using
select open from ohlc_dukascopy_bid
where symbol = 25
and timeframe = 1
and date = timestamp '2008-02-05 10:40:00' + interval '30' minute
Explain shows:
"Index Scan using mcbidindex on mytable (cost=0.00..116.03 rows=1 width=7)"
" Index Cond: ((symbol = 25) AND (timeframe = 1) AND (date = '2008-02-05 11:10:00'::timestamp without time zone))"
Time is now considerably faster: 86ms on first run.
The first version will not use a (regular) index on the column named date.
You didn't provide much information, but assuming the column named date has the datatype timestamp (and not date), then the following should work:
and date = timestamp '2008-02-05 10:40:00' + interval '30 minutes'
this should use an index on the column named date (but only if it is in fact a timestamp not a date). It is essentially the same as yours, the only difference is the explicit timestamp literal (although Postgres should understand '2008-02-05 10:40:00' as a timestamp literal as well).
You will need to run an explain to find out if it's using an index.
And please: change the name of that column. It's bad practise to use a reserved word as an identifier, and it's a really horrible name, which doesn't say anything about what kind of information is stored in the column. Is it the "start date", the "end date", the "due date", ...?