【问题标题】:Trouble with Date Functions日期函数的问题
【发布时间】:2021-11-20 22:05:30
【问题描述】:

我是 SQL 的初学者,正在研究 SQLite。

当我在 Dates 旁边使用 between 或 >=/

预期结果是满足条件且介于所列天数之间的值的计数。条件还包括所选日期之间的聚合。

作为参考,我的日期值以 DD/MM/YYYY 格式插入,但即使我在查询中将 DD/MM/YYYY 与 YYYY/MM/DD 或 YYYY-MM-DD 交换,问题仍然存在。 编辑:我的日期值的数据类型是“日期”,这实际上可能是一个问题,因为我认为我可能犯了一个语法错误,将行命名为与函数和数据类型相同?

SELECT COUNT (*) as "Number of Games",
    CASE
        WHen ftp > .968077144 then "Signifigantly Above League Average"
        WHEN ftp > .884497663 then "Firmly Above League Average"
        WHEN ftp > .800918182 then "Slighty Above League Average"
        WHEN ftp > .717338701 then "Slightly Below League Average"
        WHen ftp > .63375922 then "Firmly Below League Average"
        when ftp > .565 then "Signifigantly Below League Average"
        Else "Hack A Simmons"
        End AS "Free Throw Percentage"
        from BStats
        Where GS = 1 and date BETWeen '17/10/2017' and '18/04/2018'
        GROUP BY "Free Throw Percentage"
        HAVING ast >= (SELECT avg(ast) from BStats where GS = 1 and date BETWEEN '17/10/2017' and '18/04/2018');

修改后的代码:我将 Dates 的列名改为 playdate 以解决任何保留名称问题。我还从 between 函数切换到 >= 和 >。我应该将日期的数据类型更改为其他类型吗?我仍然得到一个空回复。感谢您的帮助!

SELECT COUNT (*) as "Number of Games",
    CASE
        WHen ftp > .968077144 then "Signifigantly Above League Average"
        WHEN ftp > .884497663 then "Firmly Above League Average"
        WHEN ftp > .800918182 then "Slighty Above League Average"
        WHEN ftp > .717338701 then "Slightly Below League Average"
        WHen ftp > .63375922 then "Firmly Below League Average"
        when ftp > .565 then "Signifigantly Below League Average"
        Else "Hack A Simmons"
        End AS "Free Throw Percentage"
        from BStats
        Where GS = 1 and playdate >= '2017-10-17' and playdate < '2018-04-18'
        GROUP BY "Free Throw Percentage"
        HAVING ast >= (SELECT avg(ast) from BStats where GS = 1 and playdate >= '2017-10-17' and playdate < '2018-04-18');

【问题讨论】:

  • 日期不会以特定格式在内部存储;如果您以 ISO 格式“YYYYMMDD”指定日期会怎样?
  • @Stu 我得到一个语法错误;我将编辑上面的帖子以提供更多信息。
  • @shawnt00 列注册为的数据类型称为 DATE 以获得更多上下文。我尝试了您的解决方案,但似乎没有解决问题。不过谢谢。

标签: sql sqlite date


【解决方案1】:

这实际上取决于您的“日期”列的数据类型。另外,您的专栏名称是“日期”吗?它是 SQL 中的保留字,因此也可能会造成一些问题。如果给您带来麻烦,请尝试将其括在括号中。

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多