ISNULL语法挑战

时间:2016-04-06 19:20:49

标签: sql sql-server

我似乎无法弄清楚这里的语法问题。

这样可行,但会返回null;

SELECT jo.Job_Operation, jo.Job, jo.Work_Center, jo.Operation_Service, jo.Est_Total_Hrs,
    (SELECT SUM(jot.Act_Run_Hrs)
    FROM PRODUCTION.dbo.Job_Operation_Time jot
    WHERE jot.Job_Operation = jo.Job_Operation) AS Cost
FROM PRODUCTION.dbo.Job_Operation jo
WHERE jo.Job = 'A5076027'

所以我在这里尝试使用ISNULL,但是我收到了一个错误:

SELECT jo.Job_Operation, jo.Job, jo.Work_Center, jo.Operation_Service, jo.Est_Total_Hrs,
    (ISNULL(SELECT SUM(jot.Act_Run_Hrs)
    FROM PRODUCTION.dbo.Job_Operation_Time jot
    WHERE jot.Job_Operation = jo.Job_Operation,0)) AS Cost
FROM PRODUCTION.dbo.Job_Operation jo
WHERE jo.Job = 'A5076027'

错误是: Msg 156,Level 15,State 1,Line 2 关键字'SELECT'附近的语法不正确。 Msg 102,Level 15,State 1,Line 4 ','。

附近的语法不正确

谁能看到我在这里缺少的东西?

谢谢!

3 个答案:

答案 0 :(得分:2)

试试这个:

password: {
                trigger: 'blur',
                container: '#passwordMessage',              

                validators: {
                    notEmpty: {
                        message: 'You cant leave this empty'
                    },

                    callback:{
                        message: 'The password is not valid',
                        callback: function(value, password, $field){
                            if(value===''){
                                return true;
                             }

                            // Check the password strength
                            if (value.length < 6) {
                                return {
                                    valid: false,
                                    message: 'It must be more than 6 characters long'
                                };
                            }

                            // The password doesn't contain any uppercase character
                            if (value === value.toLowerCase()) {
                                return {
                                    valid: false,
                                    message: 'It must contain at least one upper case character'
                                }
                            }

                            // The password doesn't contain any uppercase character
                            if (value === value.toUpperCase()) {
                                return {
                                    valid: false,
                                    message: 'It must contain at least one lower case character'
                                }
                            }

                            // The password doesn't contain any digit
                            if (value.search(/[0-9]/) < 0) {
                                return {
                                    valid: false,
                                    message: 'It must contain at least one digit'
                                }
                            }

                            if(value.search(/[_~\-!@#\$%\^&\*\(\)]+$/) < 0) {
                                return {
                                    valid: false,
                                    message: 'It must contain atleast one special character'
                                }
                            }
                            return true;
                        }               

                    }

                }          
            },

答案 1 :(得分:2)

我不认为你需要一个相关的子查询。在我看来,这就像标准的左连接一样是必需的。

SELECT jo.Job_Operation
    , jo.Job
    , jo.Work_Center
    , jo.Operation_Service
    , jo.Est_Total_Hrs
    , SUM(isnull(jot.Act_Run_Hrs, 0)) AS Cost
FROM PRODUCTION.dbo.Job_Operation jo
left join PRODUCTION.dbo.Job_Operation_Time jot ON jot.Job_Operation = jo.Job_Operation
WHERE jo.Job = 'A5076027'
GROUP BY
    jo.Job_Operation
    , jo.Job
    , jo.Work_Center
    , jo.Operation_Service
    , jo.Est_Total_Hrs

答案 2 :(得分:2)

在我看来,这更容易辨认

SELECT
   jo.Job_Operation, jo.Job, jo.Work_Center, jo.Operation_Service, jo.Est_Total_Hrs
   ,ISNULL(jot.[SumAct_Run_Hrs],0) AS Cost
FROM PRODUCTION.dbo.Job_Operation jo
LEFT JOIN (
    SELECT Job_Operation,SUM(Act_Run_Hrs) [SumAct_Run_Hrs]
    FROM PRODUCTION.dbo.Job_Operation_Time
    GROUP BY Job_Operation
) jot ON jot.Job_Operation = jo.Job_Operation
WHERE jo.Job = 'A5076027'