django order by date in datetime / extract date from datetime - sql

I have a model with a datetime field and I want to show the most viewed entries for the day today.
I thought I might try something like dt_published__date to extract the date from the datetime field but obviously it didn't work.
popular = Entry.objects.filter(type='A', is_public=True).order_by('-dt_published__date', '-views', '-dt_written', 'headline')[0:5]
How can I do this?

AFAIK the __date syntax is not supported yet by Django. There is a ticket open for this.
If your database has a function to extract date part then you can do this:
popular = Entry.objects.filter(**conditions).extra(select =
{'custom_dt': 'to_date(dt_published)'}).order_by('-custom_dt')

In the new Django, it should work out of the box [tested on 3.2] with Mysql 5.7
Dataset
[
{ "id": 82148, "paid_date": "2019-09-30 20:51:11"},
{ "id": 82315, "paid_date": "2019-09-30 00:00:00"},
]
Query
Payment.objects.filter(order_id=135342).order_by('paid_date__date', 'id').values_list('id', 'paid_date__date')
Results
`<QuerySet [(82148, datetime.date(2019, 9, 30)), (82315, datetime.date(2019, 9, 30))]>`

Related

Spark SQL change date format

I'm trying to change the date format of my data from (11 20, 2014) to 2014-11-20.
I tried this:
df.withColumn("newDate", to_date(col("reviewTime"),("mm dd, yyyy")))
Because the days with single digits appear as 1,2,8 instead of 01,02,08 I got this message:
SparkUpgradeException: You may get a different result due to the upgrading of Spark 3.0: Fail to parse '09 1, 2014' in the new parser. You can set spark.sql.legacy.timeParserPolicy to LEGACY to restore the behavior before Spark 3.0, or set to CORRECTED and treat it as an invalid datetime string.
Caused by: DateTimeParseException: Text '09 1, 2014' could not be parsed at index 3
Is there a way to fix this?
Thanks!
Some of your data date rows were written in an old spark version. You should add your spark configuration.
spark.sql.parquet.int96RebaseModeInRead = "LEGACY"
or
spark.sql.parquet.int96RebaseModeInRead = "CORECTED"
according to your requirements, they explain to you the differences between those two options in your error.
You can use format (M d, yyyy) to deal with it
Example (scala spark):
Seq(
"(11 20, 2014)",
"(1 3, 2013)",
"(2 20, 2012)",
"(4 22, 2014)"
).toDF("ugly_date")
.withColumn("date", to_date($"ugly_date", "(M d, yyyy)"))
.show(false)
Output:
+-------------+----------+
|ugly_date |date |
+-------------+----------+
|(11 20, 2014)|2014-11-20|
|(1 3, 2013) |2013-01-03|
|(2 20, 2012) |2012-02-20|
|(4 22, 2014) |2014-04-22|
+-------------+----------+
For more information about Datetime Patterns see https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html
UPD:
Screenshot with the result
Spark: 3.1.2
Scala: 2.12.10
Running on Zeppelin 0.9.0
screenshot

Query items of a collection before a certain date on Fauna Db?

I looked on google and tried different thing but I cannot figure out how to query all the item of a collection that have the key dueDate before the a certain Date.
On Mysql I would do something like :
select * from table_name where dueDate < "2001-01-01 00:00:00"
That query on mysql would return the items with the Date Inferior to 2001-01-01
I tried to do use that query on Fauna :
q.Map(
q.Paginate(
q.Match(q.Index(indexesQuery1)),
{ before: Date('2021-01-15T17:34:00+08:00')
} ),
q.Lambda("X", q.Get(q.Var("X"))) ) )
indexQuery1 is : getNewWordDemoIso(dueDate: Date!)
But It returns an empty Array,
Also I saved all my Date all Fauna the date format called : iso 8601
Also Im using javascript
Any ideas !?
thanks !!
When you perform an "Create", you need parse your ISOString Date for an FaunaDB Date format.
You migth need something like this:
const newTransaction = await fauna.query<TransactionObject>(
q.Create(q.Collection("transactions"), {
data: {
...transaction,
firstDueDate: q.Date(transaction.firstDueDate),
lastDueDate: q.Date(transaction.lastDueDate),
createdAt: q.ToDate(q.Now())
}
})
);
This will work for you as you said you are using JS/TS.
ps: (import {query as q} from 'faunadb')

Database independent grouped count distinct "days ago" in Rails 6

I want to find out on how many different 24 hour intervals a word occurred (note that the actual application is different and more complicated, but this is equivalent and needs no context).
I could do that with an SQL query like this in Postgresql:
SELECT
word,
COUNT(DISTINCT FLOOR(EXTRACT(EPOCH FROM AGE(created_at)) / 86400)) AS num_distinct_days_with_occurrence
FROM word_occurrences
GROUP BY word
I even managed to write Rails code that generates this query:
WordOccurrence.select(:word).group(:word).distinct.count('FLOOR(EXTRACT(EPOCH FROM AGE(created_at)) / 86400)')
However, it doesn't work in Sqlite3 with the same code.
I guess if I put in some time, I could probably also find a way to make it work in Sqlite3, but I can't figure out how to get both to work. I saw the count API had more features in past rails versions, but I think even that wouldn't be enough given that my main problem is timestamp handling and not counting. Also, I couldn't figure out where these features went in Rails 6.
Is there a way to make this work for both adapters? And if not, what is the best way to handle such snippets of database dependent code and choose the right one?
Example data:
[
{ word: 'a', created_at: '2020-01-10 22:30' },
{ word: 'a', created_at: '2020-01-10 23:30' },
{ word: 'a', created_at: '2020-01-11 22:30' },
{ word: 'b', created_at: '2020-01-10 22:30' }
]
If I query this at 2020-04-08, I should get this result:
[
{ word: 'a', num_distinct_days_with_occurrence: 2 },
{ word: 'b', num_distinct_days_with_occurrence: 1 }
]

QlikView: Disable Element Reorganization in List Box

I have a filed 'Day_NAM' (contains the names of the days in the week) and a field 'Day_Order' (numeric field, gives each day a number (monday=1, Tuesday=2 etc) so I can sort the days).
I managed to sort the days in the list box the way i wanted. However, when I make selection on the list box, the days change their positions in the list box. What it has to be done so that the elements in the list box do not change position when a user makes selections?
Link to the QVW file: FILE-LINK
In the Sort tab, if you have State = "Auto Ascending" try to uncheck that and see if it helps.
//Micke
The reason it isn't working is because in initiation section (the variable set statements at the beginning of your script) you don't define the day names as the day names that your data has
SET DayNames='Mo;Di;Mi;Do;Fr;Sa;So';
SET LongDayNames='Montag;Dienstag;Mittwoch;Donnerstag;Freitag;Samstag;Sonntag';
To make that sorting work you need to have those values match. That said that method isn't 100% guaranteed to work so you can force it to work (or if you don't want to change those defaults because it looks like that is for your regional settings) by using the dual() function which assigns a number to the values in a field but displays the text. Essentially combining the 2 steps you wanted to do.
Your Capture Day will then look like this:
dual(CaptureDay,
If(CaptureDay = 'Monday', 1,
If(CaptureDay = 'Tuesday', 2,
If(CaptureDay = 'Wednesday', 3,
If(CaptureDay = 'Thursday', 4,
If(CaptureDay = 'Friday', 5,
If(CaptureDay = 'Saturday', 6,
If(CaptureDay = 'Sunday', 7, 8)
)
)
)
)
)
)) as CaptureDay,
Then your sort settings should look like this:

NHibernate varying the way it creates DateTime based queries based on the day of the month

I am getting some weird behaviour on how NHibernate is sending queries to the database on DateTimeOffset fields. Given the following example:
DateTimeOffset? myDate = new DateTimeOffset(2012, 3, 17, 0, 0, 0, new TimeSpan(1, 0, 0));
var test = HibernateSession.Query<ExampleEntity>().Where(c => c.DateTimeOffsetField > myDate).ToList();
DateTimeOffset? myDate2 = new DateTimeOffset(2012, 3, 1, 0, 0, 0, new TimeSpan(1, 0, 0));
var test2 = HibernateSession.Query<ExampleEntity>().Where(c => c.DateTimeOffsetField > myDate2).ToList();
Using NHibernate Profiler to look at the SQL generated, the first query shows up as
exampleentity0_.[DateTimeOffsetField] > '17/03/2012 00:00:00 +01:00' /* #p0 */
the second as
exampleentity0_.[DateTimeOffsetField] > '2012-01-02T23:00:00.00' /* #p0 */
Notice the different formatting on the dates? If the day of the month is greater than 12, it uses the first format, if it is less than 12 it uses the second. This is causing errors when we have dates in the first format as SQL server cannot convert the string to a valid Date as it is looking for month 17 (as this example). This is driving me nuts!!
Has anyone seen this behaviour before?
Is it possible to tell NHibernate to always use the yyyy-MM-dd format?
Thanks
Tom
p.s. using FluentNHibernate for the mapping and configuration. An example of the mapping would be
Map(a => a.DateTimeOffsetField).Not.Nullable();
...i.e nothing unusual..
I have "fixed" this issue by changing the sql server instance to use a British time culture so that is accepts both values created by NHibernate... still be interested to know why these differences occur though.