【问题标题】:Error in sql query Incorrect syntax nearsql查询出错 附近语法不正确
【发布时间】:2017-02-26 10:25:42
【问题描述】:

我是 Microsoft Visual Studio 2008 的新手。我有一个 SQL 查询,它显示了解决每位员工的每个请求所花费的时间。数据库是 Windows Server 2008 上的 Microsoft SQL Server。

我想找出在 6 小时及以下时间内解决的请求的百分比,以及每个员工的所有已解决请求的总和

低于和高于 6 小时。

这是我的 SQL 查询,但它在运行时会产生错误:

'>' 附近的语法不正确 'tmp_table' 附近的语法不正确。

SQL 查询:

SELECT id, fio, date_s, tline
         , ( cast ( tline as int) > 6 ) as 'tv' 
        , count (distinct id) as 'cid'
FROM(SELECT id, fio, date_s
     , dbo.get_work_time(date_s, date_f, '12345', '09:00', '18:00', '0')/60 AS 'tline'
     FROM Zno_speed WHERE (date_f > @date)
    GROUP BY fio  
) tmp_table
GROUP BY id, fio, date_s, tline, ( cast ( tline as int) > 6 )

【问题讨论】:

  • but while it works it produces an error - 如果它产生错误,它究竟是如何工作的......?
  • 只需在 tmp_table 之前添加 'AS' 关键字。即作为tmp_table
  • ANSI SQL 有用于分隔标识符的双引号,例如"tline",SQL Server 也支持方括号,例如[tline].

标签: sql sql-server sql-server-2008


【解决方案1】:

SQL Server 没有真正的布尔数据类型,因此不支持像 cast ( tline as int) > 6 这样的布尔表达式

您需要将其重写为 case 语句:

case when cast ( tline as int) > 6 then 1 else 0 end as tv

【讨论】:

    【解决方案2】:

    CASE WHEN 用于( cast ( tline as int) > 6 )

    CASE
        WHEN ( cast ( tline as int) > 6 ) THEN 'Your Text'
        ELSE 'Your Text' END
    

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      相关资源
      最近更新 更多