SQLite ISO(即欧洲风格)周数

发布于 2025-01-07 09:25:53 字数 105 浏览 0 评论 0原文

我想返回某个日期的 ISO 标准周数(即第 1-52/53 周)。 我尝试过使用内置函数 strftime,但没有成功。

任何人都可以建议一种无需编写自定义 C 或其他函数的方法。

I want to return ISO standard week numbers (ie week 1-52/53) from a date.
I have tried using the built in function strftime, but with no success.

Can anyone suggest a way without having to write a custom C or other function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千秋岁 2025-01-14 09:25:53

我知道这是一个老问题,但最近我正在寻找同一问题的有效解决方案,这就是我想到的:

SELECT
    my_date,
    (strftime('%j', date(my_date, '-3 days', 'weekday 4')) - 1) / 7 + 1 as iso_week
FROM my_table;

基本思想是通过简单地执行日期的整数除法来计算 ISO 周数查找日期 (my_date) 的星期四的年份乘以 7,得到介于 0 和 52 之间的结果,然后加 1。在除法之前减 1 只是为了对齐除法:例如,第一周的星期四,一年中的某一天可以在 1 到 7 之间,我们希望除法的结果为 0所有情况,因此我们需要从一年中的某一天减去 1,然后再除以 7。

I know this is an old question, but recently I was looking for an efficient solution for the same problem, and this is what I came up with:

SELECT
    my_date,
    (strftime('%j', date(my_date, '-3 days', 'weekday 4')) - 1) / 7 + 1 as iso_week
FROM my_table;

The basic idea is to calculate the ISO week number by simply performing integer division of the day of year of the Thursday of the date being looked up (my_date) by 7, giving a result between 0 and 52, and then adding 1 to it. And the subtraction by 1 just before the division is there just for the alignment of the division: Thursday of week 1, for example, can have a day of year between 1 and 7, and we want the result of the division to be 0 in all cases, so we need to subtract 1 from the day of year before dividing it by 7.

满天都是小星星 2025-01-14 09:25:53

您具体尝试了什么?
这对我有用(使用 Python REPL 中的内置 SQLite):

import sqlite3
c = sqlite3.connect(':memory:')
c.execute('''create table t (c);''')
c.execute('''insert into t values ('2012-01-01');''')
c.execute('''select c, strftime('%W',c) from t;''').fetchone()
# -> (u'2012-01-01', u'00')

What specifically did you try?
This works for me (using built-in SQLite from Python REPL):

import sqlite3
c = sqlite3.connect(':memory:')
c.execute('''create table t (c);''')
c.execute('''insert into t values ('2012-01-01');''')
c.execute('''select c, strftime('%W',c) from t;''').fetchone()
# -> (u'2012-01-01', u'00')
没有伤那来痛 2025-01-14 09:25:53

好的,所以我设法回答我自己的问题。对于任何可能需要类似解决方案的人,这就是我想出的。请注意,我没有 IT 背景,我的 SQL 是自学的。

一般过程

  1. 获取该日期所属星期的星期四。
  2. 获取当年 1 月 4 日的那个星期四。
  3. 获取 1 月 4 日日期也属于该周的星期四。
  4. 用步骤 3 减去步骤 2,除以 7 再加 1。

翻译成 SQLite...

SELECT
date,
CASE CAST (strftime('%w', wknumjanfourth) AS INTEGER)
  WHEN 0 THEN ((JULIANDAY(datesThur) - JULIANDAY(strftime("%s", wknumjanfourth) - 259200, 'unixepoch')) / 7) + 1   
  ELSE ((JULIANDAY(datesThur) - JULIANDAY(DATE(strftime("%s", wknumjanfourth) - (86400 * (strftime('%w', wknumjanfourth) - 1)), 'unixepoch'), '+3 day')) / 7) + 1
END AS weeknum
FROM
(
  SELECT
  date,
  datesThur,
  DATE(datesThur,'start of year','+3 day') AS wknumjanfourth
  FROM
  (SELECT 
    date,
    CASE CAST (strftime('%w', date) AS INTEGER)
      WHEN 0 THEN DATE(strftime("%s", date) - 259200, 'unixepoch')
      ELSE DATE(DATE(strftime("%s", date) - (86400 * (strftime('%w', date) - 1)), 'unixepoch'), '+3 day')
      END AS datesThur
   FROM TEST
))

如果有人可以改进这个 SQL,我将不胜感激。

OK, so I managed to answer my own question. For anyone who might need a similar solution, this is what I came up with. Please note, I do not have an IT background and my SQL is self taught.

General process

  1. Get the Thursday of the week the date belongs too.
  2. Get the 4th of January of the year of that Thursday.
  3. Get the Thursday of the week the 4th January date belongs too.
  4. Subtract Step 2 with Step 3, divide by 7 and add 1.

Translated into SQLite....

SELECT
date,
CASE CAST (strftime('%w', wknumjanfourth) AS INTEGER)
  WHEN 0 THEN ((JULIANDAY(datesThur) - JULIANDAY(strftime("%s", wknumjanfourth) - 259200, 'unixepoch')) / 7) + 1   
  ELSE ((JULIANDAY(datesThur) - JULIANDAY(DATE(strftime("%s", wknumjanfourth) - (86400 * (strftime('%w', wknumjanfourth) - 1)), 'unixepoch'), '+3 day')) / 7) + 1
END AS weeknum
FROM
(
  SELECT
  date,
  datesThur,
  DATE(datesThur,'start of year','+3 day') AS wknumjanfourth
  FROM
  (SELECT 
    date,
    CASE CAST (strftime('%w', date) AS INTEGER)
      WHEN 0 THEN DATE(strftime("%s", date) - 259200, 'unixepoch')
      ELSE DATE(DATE(strftime("%s", date) - (86400 * (strftime('%w', date) - 1)), 'unixepoch'), '+3 day')
      END AS datesThur
   FROM TEST
))

If anyone can improve this SQL, I would appreciate the feedback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文