È questa sintassi SQL corretta?
-
22-09-2019 - |
Domanda
Posso applicare SUM()
all'interno di un ISNULL()
.... Prendere in considerazione la mia seguente dichiarazione prescelta di SQL Server
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
Questa istruzione funziona bene .... Ma quando applico SUM()
all'interno di un 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
ho ottenuto l'errore,
La colonna 'EMPLOYEE.EMP_ID' non è valido in la lista di selezione perché non è contenute in entrambi un aggregato funzione o la clausola GROUP BY.
Ogni suggerimento ....
Soluzione
È necessario del gruppo dalle altre colonne nel selezionare. Qualcosa di simile
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
Date un'occhiata alla definizione qui
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow