I want to compose bigquery in app script with date1 & date2 variable (as mentioned below). What is the format to pass these 2 variable?
var date1="20180601"
var date2="20180606"
var sql = "select * from table_query([project-name:dataset],
'table_id <= "date2" and table_id >= "date1"');";
I could do it in the following way:
var date1="20180601"
var date2="20180606"
var sql = "select * from table_query([project-name:dataset], 'table_id <= \""+date2+"\" and table_id >= \""+date1+"\"');";
I want to know the number of users logged in the last week from current day, that's why I try with this:
class UserRepository extends EntityRepository
{
public function findNumberLastWeek(){
$dql = "SELECT count(p) FROM UserBundle:User p WHERE p.lastLogin >= 'DATEADD(day,-7, GETDATE())'";
return $this->getEntityManager()
->createQuery($dql)
->getSingleScalarResult();
}
but does not work :(
I used this to do a similar feature in my app (counting user which were connected last day) :
/**
* Get the number of users which were connected between two dates
* #param startDate $
* #param endDate $
* #return array
*/
public function countUsersConnectedBetweenDate($startDate, $endDate) {
$qb = $this->createQueryBuilder('u');
$qb
->select('COUNT(u) as numberConnectedUsersLastDay')
->where('u.lastLogin BETWEEN :startDate AND :endDate')
->setParameter("startDate", $startDate)
->setParameter("endDate", $endDate);
return $qb->getQuery()->getSingleScalarResult();
}
and in the controller (it's only to get last day connection) :
$em = $this->getDoctrine()->getManager();
$startYesterday = new \DateTime();
$startYesterday->modify('yesterday');
$endYesterday = new \DateTime();
$endYesterday->modify('1 second ago');
$countConnectedUsersLastDay = $em->getRepository(User::class)->countUsersConnectedLastDay($startYesterday, $endYesterday);
You can adjust the two dates to match what you want, I've not tested it but you can try something like that :
$em = $this->getDoctrine()->getManager();
$lastWeek = new \DateTime();
$lastWeek->modify('-1 week');
$now = new \DateTime();
$countConnectedUsersLastDay = $em->getRepository(User::class)->countUsersConnectedLastDay($lastWeek, $now);
I hope that will help you.
lastLogin should be a DATE type
Try the following: p.lastLogin <= DATE_ADD(CURRENT_DATE(), -7, 'day')
I'm working on something in WebMatrix that runs an SQL Query. I can do that, however, it Selects * From UserProfile WHERE Email = #WebSecurity.CurrentUserName. I have no idea how to get it to read only a column where Email = #WebSecurity.CurrentUserName. I listed my code below.
#{
var db=Database.Open("AeroSC");
var sqlQ = "SELECT * FROM UserProfile";
var data = db.Query(sqlQ);
}
How do I go about doing this?
Thanks!
#{
var db = Database.Open("AeroSC");
var sqlQ = "SELECT Id FROM UserProfile WHERE Email = #0";
var id = db.QueryValue(sqlQ, WebSecurity.CurrentUserName);
}
I am trying to figure out how to best query in NHibernate so that the returned results are between for entries where todays time is >= PublishDateTime and <=ExpiryDateTime
The expiry date can be null so I need to allow for that. I found a couple of examples here and here but they seem to work in a different way and accept 2 values and compare to one DB field. I want the other way wrong really.
Query so far:
var query = _session.CreateCriteria<Message>()
.AddOrder(Order.Desc("PublishedDateTime"))
.List<Message>();
return query;
Any suggestions would be greatly received!
Easiest Linq query:
return _session.Query<Message>()
.Where(m => DateTime.Today >= m.PublishDateTime &&
(m.ExpiryDateTime == null ||
DateTime.Now <= m.ExpiryDateTime)
.OrderByDescending(m => m.PublishDateTime)
.ToList();
Criteria:
return _session.CreateCriteria<Message>()
.Add(Restrictions.Le("PublishedDateTime", DateTime.Today) &
(Restrictions.IsNull("ExpiryDateTime") |
Restrictions.Ge("ExpiryDateTime",
DateTime.Now)))
.AddOrder(Order.Desc("PublishedDateTime"))
.List<Message>();
In c# :
var formato = "dd/MM/yyyy h:mm:ss";
var sDesde = DateTime.Now.ToString("dd/MM/yyyy") + " 0:00:00";
var sHasta = DateTime.Now.ToString("dd/MM/yyyy h:mm:ss");
Viaje vDesde = new Viaje { Viajefecha = DateTime.ParseExact(sDesde, formato , null) };
Viaje vHasta = new Viaje { Viajefecha = DateTime.ParseExact(sHasta, formato, null) };
StringWriter strWriter = new StringWriter();
var resultado = cp.sesion.CreateCriteria<Viaje>().Add(Expression.Between("Viajefecha", vDesde.Viajefecha, vHasta.Viajefecha)).AddOrder(Order.Asc("Viajefecha")).List<Viaje>();
I need to use year() and month() functions in Criteria API to be able to express a business filter constrain. Expressions like
cri.Add(Expression.Ge("year(Duration.DateFrom)", Year.Value));
cri.Add(Expression.Le("year(Duration.DateTo)", Year.Value));
obviously do not work - is there any solution how to achieve this?
I know it's entirely possible in HQL, but I need to construct the query using criteria API because there're some additional processes processing the query adding sorting, paging etc..
sample HQL solution which I'd like to rewrite to Criteria API:
var ym = year * 100 + month;
var hql = ...(:ym between 100 * year(f.Duration.DateFrom) + month(f.Duration.DateFrom) and 100 * year(f.Duration.DateTo) + month(f.Duration.DateTo)";
It's possible to achieve this using Projections.SQLFunction. Working solution:
ISQLFunction sqlAdd = new VarArgsSQLFunction("(", "+", ")");
ISQLFunction sqlMultiply = new VarArgsSQLFunction("(", "*", ")");
var ym = Year.Value * 100 + Month.Value;
var dateFromMonthProj = Projections.SqlFunction("month", NHibernateUtil.Int32, Projections.Property("PurchaseDuration.DateFrom"));
var dateFromYearProj = Projections.SqlFunction("year", NHibernateUtil.Int32, Projections.Property("PurchaseDuration.DateFrom"));
var dateToMonthProj = Projections.SqlFunction("month", NHibernateUtil.Int32, Projections.Property("PurchaseDuration.DateTo"));
var dateToYearProj = Projections.SqlFunction("year", NHibernateUtil.Int32, Projections.Property("PurchaseDuration.DateTo"));
var calculatedYMFrom = Projections.SqlFunction(sqlAdd, NHibernateUtil.Int32, Projections.SqlFunction(sqlMultiply, NHibernateUtil.Int32, dateFromYearProj, Projections.Constant(100)), dateFromMonthProj);
var calculatedYMTo = Projections.SqlFunction(sqlAdd, NHibernateUtil.Int32, Projections.SqlFunction(sqlMultiply, NHibernateUtil.Int32, dateToYearProj, Projections.Constant(100)), dateToMonthProj);
cri.Add(Restrictions.Le(calculatedYMFrom, ym));
cri.Add(Restrictions.Ge(calculatedYMTo, ym));
Would something like this work for you?
cri.Add(Expression.Ge("Duration.DateFrom", new Date(fromYear, 1, 1));
cri.Add(Expression.Le("Duration.DateTo", new Date(toYear, 12, 31));
Note that I changed your expression order -- I'm assuming you made a typo and you want to query for dates between DateFrom and DateTo. If the dates contain time data, the second expression would change to:
cri.Add(Expression.Lt("Duration.DateTo", new Date(toYear + 1, 1, 1));
In response to comment:
cri.Add(Expression.Ge("Duration.DateFrom", new Date(fromYear, fromMonth, 1));
// Actual code needs to get last day of to month since it will not always be 31
cri.Add(Expression.Le("Duration.DateTo", new Date(toYear, toMonth, 31));
Is your user input in the form "YYMM"? If that's the case, then you just have to parse out year and month from that string to create fromYear, fromMonth, etc.
Edit: my 3rd and final attempt:
// First parse the input, e.g: september 2009 into 9 (inMonth) and 2009 (inYear)
var fromDate = new DateTime(inYear, inMonth, 1);
var toDate = fromDate.AddMonths(1).AddDays(-1);
cri.Add(Expression.Ge("Duration.DateFrom", fromDate));
cri.Add(Expression.Le("Duration.DateTo", toDate));
I'm not sure I understod what you mean with your question but I had a similar question, and I solved the problem with:
crit.Add(Expression.Sql("(YEAR({alias}.ObsDatum) = ?)", year, NHibernateUtil.String))
crit.Add(Expression.Sql("(MONTH({alias}.ObsDatum) = ?)", manad, NHibernateUtil.Int32))