我可以申请SUM()ISNULL()内....考虑我下面的SQL Server的SELECT语句

SELECT e.Emp_Id,e.Identity_No,e.Emp_Name,case WHEN e.SalaryBasis=1 
THEN 'Weekly' ELSE 'Monthly' end as SalaryBasis,e.FixedSalary,
    ISNULL(Adv.Daily_Wage,0) as Advance from Employee as e 
    inner join Designation as d on e.Desig_Id=d.Desig_Id
    Left Outer Join Payroll as Adv on e.Emp_Id=Adv.Emp_Id where e.Is_Deleted=0 

此声明工作正常....但是,当我施加SUM()ISNULL()

SELECT e.Emp_Id,e.Identity_No,e.Emp_Name,case WHEN e.SalaryBasis=1 
    THEN 'Weekly' ELSE 'Monthly' end as SalaryBasis,e.FixedSalary,
        ISNULL(SUM(Adv.Daily_Wage),0) as Advance from Employee as e 
        inner join Designation as d on e.Desig_Id=d.Desig_Id
        Left Outer Join Payroll as Adv on e.Emp_Id=Adv.Emp_Id 
        where e.Is_Deleted=0 

我得到了错误,

  

列“Employee.Emp_Id”是无效的   选择列表,因为它不是   包含在聚合   函数或GROUP BY子句。

任何建议...

有帮助吗?

解决方案

您需要GROUP BY在选择其他列。类似

SELECT  e.Emp_Id,
        e.Identity_No,
        e.Emp_Name,
        case 
            WHEN e.SalaryBasis=1  THEN 'Weekly' 
            ELSE 'Monthly' 
        end as SalaryBasis,e.FixedSalary, 
        ISNULL(SUM(Adv.Daily_Wage),0) as Advance 
from    Employee as e  inner join 
        Designation as d on e.Desig_Id=d.Desig_Id Left Outer Join 
        Payroll as Adv on e.Emp_Id=Adv.Emp_Id  
where   e.Is_Deleted=0 
GROUP BY e.Emp_Id, --This section is what you are missing
        e.Identity_No,
        e.Emp_Name,
        case 
            WHEN e.SalaryBasis=1  THEN 'Weekly' 
            ELSE 'Monthly' 
        end,
    e.FixedSalary

在这里看一看定义

GROUP BY(处理SQL)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top