0

I have a query like this

SELECT
  employee_tbl.emp_maxid,
  emp_name AS 'Employee Name',
  Designation_tbl.Des_Name AS Designation,
  emp_LabourID,
  emp_IBAN,
  emp_monthlysalary AS Salary,
  0 AS commission,
  ISNULL(emp_monthlysalary - sum(S.Paid), emp_monthlysalary) AS Total
FROM dbo.employee_tbl
INNER JOIN dbo.Designation_tbl
  ON Designation_tbl.Des_id = employee_tbl.Des_id
LEFT JOIN SalaryProcessLog_tbl S
  ON S.emp_maxid = employee_tbl.emp_maxid
WHERE (emp_deleted = 0
OR emp_deleted IS NULL)
AND employee_tbl.emp_maxid NOT IN (SELECT
  emp_maxid
FROM SalaryProcessLog_tbl
WHERE Balance = 0)
group by employee_tbl.emp_maxid,dbo.employee_tbl.emp_name,Designation_tbl.Des_Name,
employee_tbl.emp_LabourID, emp_IBAN,emp_monthlysalary

my out put getting like thisenter image description here

in my query i want to add filter result by

Where Total <> 0.. how i can do the same?

Any help is very appriciable

2
  • 1
    Wrap your query up in a derived table. Add Total condition to main query's WHERE. Commented Mar 26, 2018 at 11:01
  • One cannot specify a column alias in a WHERE clause because the SELECT column list is logically processed after the WHERE clause. Consequently, one must use other query constructs to do what you want. Commented Mar 26, 2018 at 11:05

2 Answers 2

2

Wrap your query up in a derived table. Add Total condition to outer query's WHERE.

select *
from
(
    SELECT
      employee_tbl.emp_maxid,
      emp_name AS 'Employee Name',
      Designation_tbl.Des_Name AS Designation,
      emp_LabourID,
      emp_IBAN,
      emp_monthlysalary AS Salary,
      0 AS commission,
      ISNULL(emp_monthlysalary - sum(S.Paid), emp_monthlysalary) AS Total
    FROM dbo.employee_tbl
    INNER JOIN dbo.Designation_tbl
      ON Designation_tbl.Des_id = employee_tbl.Des_id
    LEFT JOIN SalaryProcessLog_tbl S
      ON S.emp_maxid = employee_tbl.emp_maxid
    WHERE (emp_deleted = 0
    OR emp_deleted IS NULL)
    AND employee_tbl.emp_maxid NOT IN (SELECT
      emp_maxid
    FROM SalaryProcessLog_tbl
    WHERE Balance = 0)
    group by employee_tbl.emp_maxid,dbo.employee_tbl.emp_name,Designation_tbl.Des_Name,
    employee_tbl.emp_LabourID, emp_IBAN,emp_monthlysalary
) dt
WHERE Total <> 0
Sign up to request clarification or add additional context in comments.

Comments

1

To filter grouping results you should use HAVING.

SELECT
  employee_tbl.emp_maxid,
  emp_name AS 'Employee Name',
  Designation_tbl.Des_Name AS Designation,
  emp_LabourID,
  emp_IBAN,
  emp_monthlysalary AS Salary,
  0 AS commission,
  ISNULL(emp_monthlysalary - sum(S.Paid), emp_monthlysalary) AS Total
FROM dbo.employee_tbl
INNER JOIN dbo.Designation_tbl
  ON Designation_tbl.Des_id = employee_tbl.Des_id
LEFT JOIN SalaryProcessLog_tbl S
  ON S.emp_maxid = employee_tbl.emp_maxid
WHERE (emp_deleted = 0
OR emp_deleted IS NULL)
AND employee_tbl.emp_maxid NOT IN (SELECT
      emp_maxid
    FROM SalaryProcessLog_tbl
    WHERE Balance = 0)
group by employee_tbl.emp_maxid,dbo.employee_tbl.emp_name,Designation_tbl.Des_Name,
employee_tbl.emp_LabourID, emp_IBAN,emp_monthlysalary

HAVING
    ISNULL(emp_monthlysalary - sum(S.Paid), emp_monthlysalary) <> 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.