Error in sql query Incorrect syntax near

2019-09-25 10:13发布

问题:

I newbie in Microsoft Visual Studio 2008. I have a SQL query, which shows a time that had been spendeed on solving each request by every employee. Data base is Microsoft SQL Server on Windows Server 2008.

I want to find a number of requests that had been solved in a 6 hours and below in percentage terms and also a sum of all solved requests of every employee

below and above 6 hours.

This is my SQL query but while it works it produces an error:

Incorrect syntax near '>' Incorrect syntax near 'tmp_table.'

SQL Query:

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 )

回答1:

SQL Server does not have a real boolean data type and thus does not support boolean expressions like cast ( tline as int) > 6

You need to rewrite that into a case statement:

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


回答2:

Use CASE WHEN for ( cast ( tline as int) > 6 )

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