ASP.NET MVC linq-to-entities DateTime - asp.net-mvc

I am having four fields in my LOGIN table , they are userid,username,password,datetime...
In date time field, I set default value to Getdate() ... Now i want to retrive recently registered members to last ...
I mean ,
ex..
uid ... datetime
1 1/21/2011 12:40:12 PM
2 1/23/2011 6:40:12 PM
3 1/24/2011 3:40:12 PM
4 1/24/2011 5:40:12 PM
I need to retrive values as ...
uid ... datetime
4 1/24/2011 5:40:12 PM
3 1/24/2011 3:40:12 PM
2 1/23/2011 6:40:12 PM
1 1/21/2011 12:40:12 PM
How to do this using LINQ query ???
Thank you

This will get you all the users that have logged in in the last 3 day:
(
from l in Logins
where
l.datetime >= System.Data.Objects.SqlClient.SqlFunctions.DateAdd("day", -3, DateTime.Now)
select l
).OrderByDescending(l => l.Data);
If you only want the last 10 users that have logged in, for instance, try this:
(
from l in Logins
select l
).OrderByDescending(l => l.Data).Take(10);

Related

Informix today operator

I have a condition qa.actual_date >= today - 1 in a informix query.
Does it fetch the records exactly from the past 24 hours?
Eg:
Curent date and time is 13 Jun 2019 12:45 PM
does qa.actual_date >= today - 1 will fetch the records from 12 Jun 2019 12:45 PM or 12 Jun 2019 12:00 AM
Under Informix, a DATE value refers to a day, and has no explicit time component. Given a current date of 2019-06-13 and assuming the type of qa.actual_date is DATE (and not a DATETIME type), the condition:
qa.actual_date >= TODAY - 1
selects all records where the qa.actual_date value is (any time on) 2019-06-12 or later.
If qa.actual_date is of type DATETIME YEAR TO SECOND or any other type that has an hour, minute, or second component (as well as day, month, year components), then the value of TODAY - 1 will be converted (extended) to that type, and the missing time components will be treated as zeroes.
SELECT EXTEND(TODAY - 1, YEAR TO SECOND) FROM sysmaster:sysdual;
That will return 2019-06-12 00:00:00.

Query all records from a week in a month

I am creating a Rails 5 app.
In this app I got Survey model. I am able to run queries (using scopes) to get all surveys from a specific month and all from a specific quarter but I want to get all from a specific week in a month too, how can I do that?
These are my quarter and month scopes
scope :period_quarter, -> (year, quarter) { where(created_at: Date.new(year.to_i, 3 * quarter.to_i - 2).all_quarter) }
scope :period_month, -> (year, month) { where(created_at: Date.new(year.to_i, month.to_i).all_month) }
How can I add a scope to get all surveys from a specific week in a month. I will provide year, month and week (1-5).
Consider the same approach you've been using, only with all_week and perhaps beginning_of_week as needed to get the correct start date.
To get a date in a specific week, you could use something like this:
def week(year, month, week_num)
Date.new(year, month, 7 * week_num - 6)
end
week(2018, 8, 1).beginning_of_week # => Mon, 30 Jul 2018
week(2018, 8, 1).end_of_week # => Sun, 05 Aug 2018
week(2018, 8, 1).all_week # => Mon, 30 Jul 2018..Sun, 05 Aug 2018
To use this in a query, you could use it like this:
where(created_at: week(2018, 8, 1).all_week)
This will count the first week in a month as the week where the 1. is. However, if you want it to use the first full week in a month, you can remove the -6.

LEFT JOIN in SAS using PROC SQL

I am new to SAS and have this basic problem. I have a list of NYSE trading dates in table A as follows -
trading_date
1st March 2012
2nd March 2012
3rd March 2012
4th March 2012
5th March 2012
6th March 2012
I have another table B that has share price information as -
Date ID Ret Price
1st March 2012 1 … …
3rd March 2012 1 … …
4th March 2012 1 … …
5th March 2012 1 … …
6th March 2012 1 … …
1st March 2012 2 … …
3rd March 2012 2 … …
4th March 2012 2 … …
... has numeric data related to price and returns.
Now I need to join the NYSE Data table to the above table to get the following table -
Date ID Ret Price
1st March 2012 1 … …
2nd March 2012 1 0 0
3rd March 2012 1 … …
4th March 2012 1 … …
5th March 2012 1 … …
6th March 2012 1 … …
1st March 2012 2 … …
2nd March 2012 2 0 0
3rd March 2012 2 … …
4th March 2012 2 … …
i.e. a simple left join. The zero's will be filled with . in SAS to indicate missing values, but you get the idea. But if I use the following command -
proc sql;
create table joined as
select table_a.trading_date, table_b.* from table_a LEFT OUTER join table_b on table_a.trading_date=table_b.date;
quit;
The join happens only for the first ID (i.e. ID=1) while for the rest of the IDs, the same data is maintained. But I need to insert the trade dates for all IDs.
How can get the final data without running a do while loop for all IDs? I have 1000 IDs and looping and joining 1000 times is not an option due to limited memory.
Joe is right, you need to take also ID into consideration, but with his solution you cannot get 2nd March 2012 because no one is trading that day. You can do everything with just one sql step (which will take a bit longer):
proc sql;
create table final as
select d.trading_date, d.ID, t.Price, t.Ret
from
(
select trading_date, ID
from table_a, (select distinct ID from table_b)
) d
left join
(
select *
from table_b
) t
on t.Date=d.trading_date and t.ID=d.ID
order by d.id, d.trading_date;
quit;
Your left join doesn't work since it doesn't take ID into account. SAS (or rather SQL) doesn't know that it should repeat by ID.
The easiest way to get the full combination is PROC FREQ with SPARSE, assuming someone has a trade on every valid trading day.
proc freq data=table_b noprint;
tables id*trading_date/sparse out=table_all(keep=id trading_date);
run;
Then join that to the original table_b by id and date.
Alternately, you can use PROC MEANS, which can get your numerics (it can't get characters this way, unless you can use them as a class value).
Using table_b as created by Anton (With ret and price variables):
proc means data=table_b noprint completetypes nway;
class id trading_date;
var ret price;
output out=table_allmeans sum=;
run;
This will output missing for missing rows and values for present rows, and will have a _FREQ_ variable that allows you to differentiate whether a row is really present in the trading dataset or not.
I suppose there must be something off with the data because your query looks fine and worked on the testing data I generated along the lines you described:
data table_a;
format trading_date date9.;
do trading_date= "01MAR2012"d to "06MAR2012"d;
output;
end;
run;
data table_b;
format date date9.;
ret = 0;
price = 0;
do date= "01MAR2012"d to "06MAR2012"d;
do ID = 1 to 4;
if ranuni(123) < 0.3 then
output;
end;
end;
run;
Below is what I get after running your query copied verbatim:
trading_date date ret price ID
01MAR2012 01MAR2012 0 0 3
02MAR2012 02MAR2012 0 0 2
03MAR2012 03MAR2012 0 0 1
03MAR2012 03MAR2012 0 0 2
04MAR2012 04MAR2012 0 0 2
05MAR2012 05MAR2012 0 0 3
06MAR2012 . . . .
It is worth checking the format of your dates- are they numeric? If they are character, are they formatted the same way? If they are numeric, are they dates or datetimes with some odd format applied?

Rails, postgres, timezones, and ActiveRecord order method

I have a query that orders a group of tasks by their start_date(UTC datetime).
This works perfectly.
The problem comes when displaying the ordered records for the end user in their timezone.
lets say I have 5 tasks (ordered by their utc times)
1 2:00
2 3:00
3 12:00
4 16:00
5 22:00
This is the correct order.
But when I convert to EDT:
1 22:00
2 23:00
4 12:00
5 18:00
Is there a way to specify the timezone you're querying in postgres using activerecord?
Thank you

Rank in-order of relation to the current date. [Rails, Sqlite3]

How do I return information in rank of closeness to the current date irrelevant of the year. If Current date is May 26 2011, the date closeness to May 26 is returned first. My problem was this confusion: I have two dates: Date 1: May 24th 2011 and Date 2: June 1st 2010, ensuring that Date 2 is returned before Date 1. Both because May 24 has passed and also the year factor.
Table:
Name Created_at
Bob 2010-06-01 (YYYY-MM-DD)
Mike 2010-05-07
Fife 2011-05-09
So it would return if current date is May 26 2011:
Bob 2010-06-01 (YYYY-MM-DD)
Mike 2010-05-07
Fife 2011-05-09
What I have so far:
#awesomeDate = Names.order("name").having("date > ?", Date.today)

Resources